For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2006 > simple function not working as expected









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author simple function not working as expected
tomv5001@hotmail.com

2006-04-05, 6:56 pm

given the script:

#!/bin/sh
cat tempfunct.txt |\
awk '{
func myprint(num)
{ printf "%6.3g\n", num }
if ($1>0) { myprint($1) }
}'

with this input file (tempfunct.txt )
1 5 23 8 16
44 3 5 2 8 26
256 291 1396 2962 100
-6 467 998 1101
99385 11 0 225

I get this output
0
0
0
0
0

I want this
1
44
256
9.94e+04

can someone correct my code please ?

news.t-online.de

2006-04-05, 6:56 pm

tomv5001@hotmail.com wrote:

Try this


awk '
function myprint(num) {
printf "%6.3g\n", num
}
if ($1>0) { myprint($1) }
'

> given the script:
>
> #!/bin/sh
> cat tempfunct.txt |\
> awk '{
> func myprint(num)
> { printf "%6.3g\n", num }
> if ($1>0) { myprint($1) }
> }'
>
> with this input file (tempfunct.txt )
> 1 5 23 8 16
> 44 3 5 2 8 26
> 256 291 1396 2962 100
> -6 467 998 1101
> 99385 11 0 225
>
> I get this output
> 0
> 0
> 0
> 0
> 0
>
> I want this
> 1
> 44
> 256
> 9.94e+04
>
> can someone correct my code please ?
>

news.t-online.de

2006-04-05, 6:56 pm

news.t-online.de wrote:
No sorry this


awk '
function myprint(num) {
printf "%6.3g\n", num
}
{if ($1>0) myprint($1) }
'
[color=darkred]
> tomv5001@hotmail.com wrote:
>
> Try this
>
>
> awk '
> function myprint(num) {
> printf "%6.3g\n", num
> }
> if ($1>0) { myprint($1) }
> '
>
Chris F.A. Johnson

2006-04-05, 6:56 pm

On 2006-04-05, tomv5001@hotmail.com wrote:
> given the script:
>
> #!/bin/sh
> cat tempfunct.txt |\
> awk '{
> func myprint(num)
> { printf "%6.3g\n", num }
> if ($1>0) { myprint($1) }
> }'


awk '
func myprint(num)
{ printf "%6.3g\n", num }
{
if ($1>0) { myprint($1) }
}'


> with this input file (tempfunct.txt )
> 1 5 23 8 16
> 44 3 5 2 8 26
> 256 291 1396 2962 100
> -6 467 998 1101
> 99385 11 0 225
>
> I get this output
> 0
> 0
> 0
> 0
> 0
>
> I want this
> 1
> 44
> 256
> 9.94e+04
>
> can someone correct my code please ?
>



--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
tomv5001@hotmail.com

2006-04-05, 6:56 pm

Now im getting this:
awk: syntax error near line 3
awk: bailing out near line 3
/etc/script.sh: ~: not found

This is the script:
#!/bin/sh
cat temp1.txt |\
awk '
func myprint(num)
{ printf "%6.3g\n", num }
{
if ($1>0) { myprint($1) }'

Chris F.A. Johnson

2006-04-05, 6:56 pm

On 2006-04-05, tomv5001@hotmail.com wrote:
> Now im getting this:
> awk: syntax error near line 3
> awk: bailing out near line 3
> /etc/script.sh: ~: not found
>
> This is the script:
> #!/bin/sh
> cat temp1.txt |\


Why are you using cat?

> awk '
> func myprint(num)
> { printf "%6.3g\n", num }
> {
> if ($1>0) { myprint($1) }'


What system are you using? What version of awk? If you are on
Solaris, use nawk instead of awk.

Also please read <http://cfaj.freeshell.org/google>

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
tomv5001@hotmail.com

2006-04-06, 6:57 pm

I am in Solaris , nawk works .. thanks I guess Sun's awk doesn't do
functions

Patrick TJ McPhee

2006-04-07, 3:56 am

In article <1144333230.015980.67910@u72g2000cwu.googlegroups.com>,
<tomv5001@hotmail.com> wrote:
% I am in Solaris , nawk works .. thanks I guess Sun's awk doesn't do
% functions

Solaris has three awks.
/usr/bin/awk is the 'old' (pre-1987) awk, which is left in place so
that people's 20-year old shell scripts continue to work, even if
they use constructs which would cause syntax errors in the new (1987) awk.

For instance, in your script, the first line

func myprint(num)

is the concatenation of three variables in old awk. In new awk, it's
a syntax error because func is a reserved word, while in posix awk,
it's an error because it calls an undefined function, myprint.

/usr/bin/nawk is the 1987 version of awk

/usr/xpg4/bin/awk is the posix awk, which is supposed to follow the
single unix specification. The best thing to do on Solaris is to put
/usr/xpg4/bin first in your path.
--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com