mikeage.net Logo

mikeage.net @ י״ד שבט תשע״ט

Posts Tagged ‘AWK’

Careful of AWK Comments

‍‍ז׳ מנחם אב תשס״ה - Friday, August 12th, 2005

Passing a script with comments from a shell script.

If you're like me, you write awk scripts by creating a file, let's say script.sh, and it looks something like this (let's say.. count lines where the 2nd comma-delimited number is greater than 3):
#!/bin/sh<br />
awk -F, 'BEGIN {<br />
# Minimum threashold<br />
}<br />
{<br />
# Let's do this thang<br />
if $2 > THRESHHOLD <br />
COUNT++ <br />
} <br />
END { <br />
print "COUNT = " COUNT<br />

(Quick note: I know the IF statement can be eliminated, but I left it in so even non-AWK programmers can benefit from this example.)
Now, this code will not run. Why not? Let's try and see what happens:
~/tips/linux > ./badscript.sh < testfile<br />
awk: line 7: missing } near end of file<br />
./badscript.sh: line 10: syntax error near unexpected token `}'<br />
./badscript.sh: line 10: `} '<br />

Strange. Quite strange. Let's look at lines 7 and 10:
# Let's do this thang<br />and<br />
} <br />

One is a comment, one is just the closing brace. Count the braces-- they line up. And how is the } missing... it's on line 10 (ironically, the other error)! Think about it for a minute

Ok, here's the answer. The apostrophe in let's is really a single quote, which closes the script. Just because awk knows that # is a comment doesn't prevent the shell from getting confused. Solution: either write "let us" instead, or just pass the script as awk -f &lt;scriptfile&gt;.


Quick Map
Content +
Personal +
Archives +
Site Stuff +
RBS Weather +
Search +
Recent Images

Valid XHTML 1.1!
Printer Friendly Page

Last Modified: September 04, 2006 @ 02:11 CST

Memory(TRUE): 2097152/2097152
Memory(FALSE): 3247808/3257760