Help with Bash script - set, awk

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.

Thanks,

Mike G.

The awk code should be:

awk -v lines=$lineslastrun -v pattern="$searchpattern" '
  BEGIN { count=0 }
  NR > lines && $0 ~ pattern{ count++ }
  END { print NR; print count }
' $logfile

Thank you sir. That worked!

Hello sir,

I have a similar example in which I need to search for a pattern from a particular point of the file. Say for ex,

The file has

asdsf

aaaa

adsajd

asda

Processing

sdfdkjf

aaaa

dflkjd

dfjkhdk

aaaa

I need to search for "aaaa" after the point "Processing" occurs in the file.

Can you please help!

Thanks

awk '/Processing/{f=1} f && /aaaa/{print;end}' file

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

If you're using shell variables, the code should looks like:

awk '/p1/{f=1} f && /p2/{print;end}' p1=$pat1 p2=$pat2 test.dat

Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.

Sorry for buggin u..
I still get the same error

$ echo $pat1
Processing
$ echo $pat2
aaaa
$ cat test
asdsf

aaaa

adsajd

asda

Processing

sdfdkjf

aaaa

dflkjd

dfjkhdk

$ awk '/p1/{f=1} f && /p2/{print;end}' p1=$pat1 p2=$pat2 test
awk: syntax error near line 1
awk: bailing out near line 1
$

---------- Post updated at 08:23 AM ---------- Previous update was at 08:20 AM ----------

Sorry, I dont get the error, But I dont get the desired output

$ echo $pat1
Processing
$ echo $pat2
aaaa
$ cat test
asdsf

aaaa

adsajd

asda

Processing

sdfdkjf

aaaa

dflkjd

dfjkhdk

$ nawk '/p1/{f=1} f && /p2/{print;end}' p1=$pat1 p2=$pat2 test
$ /usr/xpg4/bin/awk '/p1/{f=1} f && /p2/{print;end}' p1=$pat1 p2=$pat2 test

I'm sorry, the code should be:

nawk '$0 ~ p1{f=1} f && $0 ~ p2{print;end}' p1=$pat1 p2=$pat2 test.dat

Hi Thanks a lot.
$ /usr/xpg4/bin/awk '$0 ~ p1{f=1} f && $0 ~ p2{print;end}' p1=$pat1 p2=$pat2 subha
input file "Started"aaaa
aaaa
$

I get " input file "Started" " in the output. Can we omit this and just print aaaa??

Thanks for your help!

Strange..try this, assuming subha is the filename:

/usr/xpg4/bin/awk -v p1=$pat1 -v p2=$pat2 '$0 ~ p1{f=1} f && $0 ~ p2{print;end}' subha 

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

I am not sure either..

I don't know what's wrong, the code seems to be correct, post a part of the original file and the desired output within code tags.

Regards