![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to access values of awk/nawk variables outside the awk/nawk block? | saniya | Shell Programming and Scripting | 5 | 05-13-2008 04:37 AM |
| nawk & awk | sd12 | UNIX for Advanced & Expert Users | 5 | 05-05-2008 07:46 AM |
| nawk/ksh help | DeltaX | Shell Programming and Scripting | 0 | 03-06-2008 12:54 PM |
| nawk -v to awk | kamel.seg | Shell Programming and Scripting | 2 | 12-18-2007 04:30 AM |
| nawk | whatisthis | Shell Programming and Scripting | 3 | 09-29-2004 10:44 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
'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 05:34 AM. Reason: fix some typos |
|
#3
|
|||
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |