Trying to search a log file for a string, starting from a certain point in the log file. I want to return the number of lines that contain the search string and the total number of lines in the log file. Here's the part of the script I'm having problems with:
set -- $(awk -v lines=$lineslastrun -v pattern="$searchpattern" '
BEGIN { count=0 }
NR > lines
/pattern/ { count++ }
END { print NR; print count }
' $logfile)
echo $1
echo $2
The variables are set earlier in the script and are:
$lineslastrun=8
$searchpattern="test data"
The log file looks like this:
[root ~]$ more testlog
one
two
three
test data
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
test data
seventeen
eighteen
test data
testdata
When I run the script with 'set -x' I get the following:
++ awk -v lines=8 -v 'pattern=test data' '
BEGIN { count=0 }
NR > lines
/pattern/ { count++ }
END { print NR; print count }
' ./testlog
+ set -- nine ten eleven twelve thirteen fourteen fifteen test data seventeen eighteen test data testdata 20 0
+ echo nine
nine
+ echo ten
ten
I would like awk to only return the data specified in the END statement but it is returning every line after 8. If it does that then 'echo $1' and 'echo $2' will return Total lines and Number of hits.
I can't seem to get awk to search for strings with spaces. The search pattern above should have returned '2' but it's returning '0'. If I search for a pattern without a space it works fine.
Hi, Thanks for the response.
But I get a syntax error
$ echo $pat1
Processing Started
$ echo $pat2
EDI
$ awk '/$pat1/{f=1} f && /$pat2/{print;end}' test.dat
awk: syntax error near line 1
awk: bailing out near line 1
$ awk '/Processing/{f=1} f && /EDI/{print;end}' test.dat
awk: syntax error near line 1
awk: bailing out near line 1
When I run the command, it prompts as below...
$ /usr/xpg4/bin/awk -v p1=$pat1 -v p2=$pat2 '$0 ~ p1{f=1} f && $0 ~ p2{print;end}' file
input file "-v"$ Started
ksh: Started: not found