awk - command line works / script doesn't

From: Rob Ouellette (rouellet@ebmail.gdeb.com)
Date: Fri Apr 28 2006 - 15:13:16 EDT


Hello All,

I wanted a simple script to search for a key word in a file and print some
number of lines after it. The following example works on the command line
but not in the script shown below. The error is "syntax error near line
1".

I know this can be done easily in Perl. However, I'm trying to expand my
bourne and awk shell skills...

Also, the translation of the awk command from the script, when run with
the "-x" option, can be pasted onto the command line and run
successfully...

Any ideas on how to make this work?

Commands that work from the command line:
# look for "Packing database" , print that line, get and print the
following two lines from the file CMutil
awk '/Packing database/{print;getline;print;getline}' CMutil
awk /"Packing database"/'{print;getline;print;getline}' CMutil

Script:
#!/bin/sh -x
#search string/token/word(s)
srcStr=$1
#number of lines to print after search word is found
nLines=$2
#files to search
file=$3

cnt=0
lines="print;"

while [ $cnt -lt $nLines ]
do
   lines=$lines'getline;print;'
   cnt=`expr $cnt + 1`
done

#Doesn't work
awk "'/$srcStr/{$lines}'" $file

#This doesn't work either
awk /\"$srcStr\"/"'{$lines}'" $file

Sample text (CMutil file):
Packing database blah blah blah
Date blah blah
Database blah blah blah dump complete
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:39:42 EDT