awk beginners question

hi,

i start using awk and have a very basic problem. here's my code:

#! /usr/bin/awk -f
# 2010, scz
#


{
	$1 == "test" { print $2 } 
}

this works on the command line but not as "program" - what is the difference between awk programs on the command line and executing awk commands from a file?

rgds,
Sven

Are you sure it works from the command line?

 $ awk '{ $1 == "test" { print $2 } }' file1
 Syntax Error The source line is 1.
 The error context is
                { $1 == "test" >>>  { <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
 Syntax Error The source line is 1.

Try removing the outer braces:

#! /usr/bin/awk -f
# 2010, scz
#
    $1 == "test" { print $2 } 
1 Like

You have a syntax error becuase you are embedding a pattern:action pair inside another.
Just remove the enclosing { }...

$ cat script1.awk
#! /usr/bin/awk -f
# 2010, scz
#

        $1 == "test" { print $2 }


$ chmod 775 script1.awk

$ echo test 123 > file1

$ ./script1.awk file1
123

$

thanks scott,

you are right - i've left out the braces on the command line. the script file also worked after removing them.

i was using braces like in c ... my 1st lesson!