nawk use


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users nawk use
# 1  
Old 01-03-2008
nawk use

I found a command who prints x lines before and after a line who contain a searched string in a text file.
The command is :
-------------------
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1

...where "b" and "a" are the number of lines to print before and after string "s".
-------------------

It works very well but I can't understand the syntax, too difficult with "man nawk". Is that some one who will be able to comment this syntax ?

Thank's in advance, best regards and happy new year.
# 2  
Old 01-10-2008
'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}'
has 3 pattern-action statements:
1) c-->0;
2) $0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}
3) b{r[NR%b]=$0}

The first one has no explicit action so the action is to simply print the entire record. But in this case the semicolon is needed so that it doesn't run in to the second staement. The second statement has an explicit action which is in braces and the braces are enough to separate it from the third. Now consider these statements in reverse order...

3) b{r[NR%b]=$0}
The pattern is b, which is asking if b is equal to zero. If b is non-zero we need to save records in case we need them later. But if b is zero, we can skip this since we do not want any "before" records displayed. If b is, say, 5, we will always have the last 5 records in the r array.

2) $0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}
The pattern $0~s simply asks if the record matches the search criteria we stored in s. If so and if b is non-zero, we print those records that we saved in step 3 above. Then we print the current record. Then c=a sets up the next step to be explained...

1) c-->0;
c gets set to a (number of "after" records) when we find a match. The c-- part decrements c after we use it. And we use it to see if it is greater than zero. This is how the "after" records are printed.

Last edited by Perderabo; 01-17-2008 at 08:34 AM.. Reason: fix some typos
# 3  
Old 01-17-2008
thank you Perderabo

I understand better ! And maybe "NR%b" means "NR modulo b" ...

I will take more time to analyse but thank's a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

2. Shell Programming and Scripting

Nawk help

I don't know whats wrong with the code here. Its giving double counts. nawk -F# ' { if( match($2, "= ") > 0) num=substr($2,RSTART+2,length($2)-1); if (match($20, "= ") > 0) res=substr($20,RSTART+2,length($20)-1); if(match(num,... (2 Replies)
Discussion started by: jagpreetc
2 Replies

3. Shell Programming and Scripting

help with nawk

hi guys, I am writing a code and have stuck at one point. Inside nawk I am storing my desired variable a, I just need to find if a is present in an external file error.log or not. If yes, print something. grep or for loop not working properly inside nawk. Sample code provided. nawk ' BEGIN... (5 Replies)
Discussion started by: shekhar2010us
5 Replies

4. Shell Programming and Scripting

using nawk

help out with code. two files aaa bbb contains some records..output file xyz should be like this..see below i/p file:aaa 08350|60521|0000|505|0000|1555|000|NYCMT|Pd_1 |-11878 i/p file: bbb 60521|60510 o/p file :xyz 60510|08350|60521|0000|505|0000|1555|000|NYCMT|Pd_1 |-11878 (5 Replies)
Discussion started by: Diddy
5 Replies

5. UNIX for Dummies Questions & Answers

Nawk help!!!

Hi, Please help me I want to filter all messages having a value less than a particular value..Please advice how to use <= in the below red marked script.. Getting the error as no such file or directory for the marked line no. Thanks in advance... Script is as under : read message gawk... (5 Replies)
Discussion started by: vanand420
5 Replies

6. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

7. Shell Programming and Scripting

nawk help

Hi Gurus, I am using a script as under : read string nawk -v search="$string" ' /a/,/z/ { block = (block ? block ORS : "") $0; } /z/ { if (block ~ search) print block; } ' <File> nawk -v search="$string" ' /b/,/z/ { ... (1 Reply)
Discussion started by: vanand420
1 Replies

8. Shell Programming and Scripting

nawk help

What am I doing wrong here? I get syntax error. I am trying to parse a file looking for the lines with "running" as the first field then print the 5th field, then looking at the 9th field for specific values and writing the whole line to a junk file. nawk '; {if ($1 == "running")... (10 Replies)
Discussion started by: beppler
10 Replies

9. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

10. Shell Programming and Scripting

nawk

Hi, I had this syntax and no matter what I do, I can't get it run. err message: run6: syntax error at line 121 : `(' unexpected I went to line 121 and it's comment out! All the variables passed to nawk are valid. There are two places I suspect have the problem: 1.... (3 Replies)
Discussion started by: whatisthis
3 Replies
Login or Register to Ask a Question