awk - command line works / script doesn't
- From: Rob Ouellette <rouellet@xxxxxxxxxxxxxxx>
- Date: Fri, 28 Apr 2006 15:13:16 -0400
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
- Prev by Date: SUMMARY: how to carry out E250 maintenance?
- Next by Date: migrating printers files
- Previous by thread: SUMMARY: E450 won't boot with APC SmartUPS
- Next by thread: migrating printers files
- Index(es):
Relevant Pages
|
|