AWK syntax /bailing script error when executing in UNIX

Hi I am trying to execute the following awk script in unix but getting
the following error

awk: syntax error near line 1
awk: bailing out near line 1

for i in `cat search`
 do
   grep -i -l $i *.sas | awk -v token=$i '{print token "\t" $0}'
 done

Please let me know what could be the reason.
And this is to pass some tokens and search in a directory to find particular tokens.

You get a useless use of cat award. :wink:

You also need to quote things more, probably $i is being split into two strings and awk is trying to use the second half of it as its script.

while read TOKEN
do
        grep -i -l "$i" *.sas | awk -v token="$i" '{print token "\t" $0}'
done < search

well!!! the same code I have been using since long time...and it never showed this error....I am executing this on diff flavour of unix.....let me know any other way

---------- Post updated at 12:01 PM ---------- Previous update was at 11:58 AM ----------

cat > abc
for i in `cat tks`
do
grep -i -l $i *.txt | awk -v token=$i '{print token "\t" $0}'
done

Control-D

cat > tks
India Australia America

Control-D

cat -> file1.txt

India and Australia are world champions in cricket

Control-D

cat -> file2.txt

America is NBA champion.

Control-D
Now all files created
at the unix prompt where you run all these files

sh abc

I'm reminded of something from space history... On opening an electronic module for inspection: "My god, this part passed all our tests and it's *garbage*!" Just because it works doesn't mean it's sensible. And if you use "for stuff in `cat file`" all over the place? You've suddenly got something to worry about.

The error's always been possible, but somehow you've never fed it lines with spaces before, and never fed it files larger than you can squeeze into a variable. You've been lucky.

Because $i wasn't quoted, your awk line ends up awk -v token=India and Australia are world champions in cricket '{print token "\t" $0}' token gets the "india" part. The rest of it -- "and", "australia", "are", "world", "champions", "in", and "cricket" -- awk tries to run as scripts or read as files.

I just did. Try it, it should work. It should be able to handle files of any size too, where your version will barf on files bigger than a few kilobytes on some systems.

Please provide the sample of search file and *.sas file, and also the expect O/P.

Now I have to guess, seems your system is solaris. need nawk.

all in one awk:

nawk 'NR==FNR{a[NR]=tolower($0);next}{ for ( i in a) {if (tolower($0) ~ a) print a,$0}}' OFS="\t" search *.sas