Objectmix
Tags Register Mark Forums Read

Search vs. Action and searching for strings : awk

This is a discussion on Search vs. Action and searching for strings within the awk forums in Programming Languages category; I have awk embedded in a shell script and I am trying to search for a string of text after a certain point in the file and I need to return number of lines with that string and number of lines in the input file. I have the following: Code: 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 In the script, those variables are currently set as: $lineslastrun = 8 $searchpattern = "test data" The file ...


Object Mix > Programming Languages > awk > Search vs. Action and searching for strings

Reply

 

LinkBack Thread Tools
  #1  
Old 06-02-2009, 07:58 PM
Junior Member
 
Join Date: Jun 2009
Posts: 0
mglenney is on a distinguished road
Default Search vs. Action and searching for strings

I have awk embedded in a shell script and I am trying to search for a string of text after a certain point in the file and I need to return number of lines with that string and number of lines in the input file.

I have the following:

Code:
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
In the script, those variables are currently set as:
$lineslastrun = 8
$searchpattern = "test data"

The file being searched:

Code:
[a.mjglenne@plaxafm1001 ~]$ cat testlog
one
two
three
test data
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
test data
seventeen
eighteen
test data
testdata
So I want to check each line of the file, starting at line 9 and each time I find a line with "test data" somewhere in it I want to increase the counter by 1. At the end I want to output the total number of lines in the input file and the count.

If I run the bash script with 'set -x' I see the following output

Code:
++ 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
Looks like awk is returning lines that don't match AND it's not able to search for my pattern. It is returning the number of lines (20) and the count (0) but it seems to be returning everything. The "echo nine" and "echo ten" you see there are 'echo $1' and 'echo $2' in the bash script. This would work great if I could get awk to only output NR and count.

Appreciate any help. Thanks
Reply With Quote
Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
Searching two tables - one blank search field usenet Macromedia Dreamweaver 1 10-20-2008 06:41 AM
Google search result to be URL-limited when searching site, but notwhen searching Web usenet Javascript 2 02-04-2008 04:01 AM
Re: perl DB (pgsql)I: problems searching text strings with ' symbol (es d'ambrose) usenet Perl 0 04-14-2007 04:03 AM
porblems with search action usenet Graphics 0 06-25-2003 12:22 PM
Search Engine searching in different sites usenet Net Objects Fusion 0 09-28-2000 10:22 AM


All times are GMT -5. The time now is 09:48 AM.