awk - command line works / script doesn't



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@xxxxxxxxxxxxxxx
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



Relevant Pages

  • SUMMARY: awk - command line works / script doesnt
    ... When you enter a command line ... and awk gets exactly the same string handed to it in argv. ... Within a shell script, you need to be careful not to use single ... Packing database blah blah blah ...
    (SunManagers)
  • Re: Perl and IIS - script runs but The page cannot be displayed
    ... >I've never run a perl script at the command line. ... # closeor die $!; ... the 'blah blah blah' is displayed as are the file contents. ...
    (comp.lang.perl.misc)
  • Re: DSMOD - Read from TXT or CSV file
    ... You need to build 100 command lines for the disabling ... of users using dsmod. ... by admin blah blah blah -disabled yes ...
    (microsoft.public.windows.server.active_directory)
  • Re: Files older than ...
    ... under our /home/staff diectory that are older than 18-24 months, ... Of coarse they would be given fair warning of the impending ... blah blah blah> dlist ... warning and the time you ran this script? ...
    (comp.unix.shell)
  • RE: Hacked NT/2K box
    ... using a UNIX box you can obtain a remote console on an effected Win2K box. ... I've pasted my copy of the script below my sig. ...
    (Focus-Microsoft)