Help with Bash script - set, awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Bash script - set, awk
# 1  
Old 06-03-2009
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:

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

The variables are set earlier in the script and are:
$lineslastrun=8
$searchpattern="test data"

The log file looks like this:

Code:
[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:

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

  • 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.
# 2  
Old 06-03-2009
The awk code should be:

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

# 3  
Old 06-03-2009
Thank you sir. That worked!
# 4  
Old 07-13-2009
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
# 5  
Old 07-13-2009
Code:
awk '/Processing/{f=1} f && /aaaa/{print;end}' file

# 6  
Old 07-13-2009
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
# 7  
Old 07-13-2009
Quote:
Originally Posted by prsshini
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:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Set password in bash script without manual entry-Solaris 10

Hi I have a root script which is setting up user and his dirs and so on. After I create user and set up all the necessary I have to manually set user password. I try all possible ways what google find me and nothing works for me. If maybe one of you have a solution for my problem it will be... (1 Reply)
Discussion started by: Jaffakeks
1 Replies

2. Shell Programming and Scripting

Bash/awk script problem

Hi, I have 100 files containing different values in single column, I want to split those files in two separate files (file2 and file3) based on average value of first column of each file, for those files I am working on the following script #bin/bash for memb in $(seq 1 100) do awk... (4 Replies)
Discussion started by: dsp80
4 Replies

3. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

4. Shell Programming and Scripting

Bash script set command to a variable

Hi, Will following set up work in bash script? I've got errors if assigning following binary command to a variable. But on the other hand, COMMAND="ls" works. Any explanation please? How can I assign binary command to a variable COMMAND then I can just call ${COMMAND}? COMMAND="rsync"... (3 Replies)
Discussion started by: hce
3 Replies

5. Shell Programming and Scripting

awk script to filter the numbers which are around the set value

Hi All, I have one sensor output(over the same) for a set value of 20. Time(in Sec), Data 1, 16 2, 20 3, 24 4, 22 5, 21 6, 20 7, 19.5 8, 20 9, 20.5 10, 20 11, 20 12, 19.5 Here we can see like after 5 sec of time the data value reaches to 20+-0.5 range. So I... (7 Replies)
Discussion started by: ks_reddy
7 Replies

6. Shell Programming and Scripting

AWK/Bash script

I would like to write a script to extend this command to a general case: BEGIN {s_0=0;n_0=0}{n_0++;s_0+=($51-$1)^2}END {print sqrt(s_0/n_0)} i.e. so that BEGIN {s_0=0;n_0=0}{n_0++;s_0+=($51-$1)^2}END {print sqrt(s_0/n_0)} BEGIN {s_1=0;n_1=0}{n_1++;s_1+=($51-$2)^2}END {print... (3 Replies)
Discussion started by: chrisjorg
3 Replies

7. Shell Programming and Scripting

Help: How to convert this bash+awk script in awk script only?

This is the final first release of the dynamic menu generator for pekwm (WM). #!/bin/bash function param_val { awk "/^${1}=/{gsub(/^${1}="'/,""); print; exit}' $2 } echo "Dynamic {" for CF in `ls -c1 /usr/share/applications/*.desktop` do name=$(param_val Name $CF) ... (3 Replies)
Discussion started by: alexscript
3 Replies

8. Shell Programming and Scripting

Using AWK in a bash script

So I am a newbie obviously but I need some help. I am trying to run a script to check that the number of fields in a database is equal to 4, if not they should try again and run an appropriate database file. The fields are separated by a semi colon. I've tried a lot of things, this what I'm... (3 Replies)
Discussion started by: mb001
3 Replies

9. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies
Login or Register to Ask a Question