The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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 08:37 AM
so difficult question about using grep shrimpy UNIX for Dummies Questions & Answers 2 03-22-2008 12:19 AM
A difficult script (for me) DNAx86 Shell Programming and Scripting 2 01-07-2008 04:04 PM
difficult sed command Dave724001 UNIX for Advanced & Expert Users 17 09-29-2006 06:11 AM
Difficult Filtering Problem Piyush UNIX for Advanced & Expert Users 1 06-30-2003 05:06 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-29-2008
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 228
Unhappy difficult nawk to understand

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

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.
  #2 (permalink)  
Old 10-29-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,124
See: nawk use
  #3 (permalink)  
Old 10-29-2008
chihung chihung is offline
Registered User
  
 

Join Date: Jun 2008
Location: Singapore
Posts: 48
I will try to 'decipher' that one-liner awk code
Code:
awk -v before=4 -v after=4 -v search="string" '
--current > 0 {
        print
}
$0 ~ search{
        if ( before ) {
                for ( current=before+1 ; current>1 ; current-- ) {
                        print rec[(NR-current+1)%before]
                }
        }
        print
        current=after
}
before{
        rec[NR%before]=$0
}' file1
I think the above code is a lot easier to understand and pretty self explanatory, hopefully.
The original one-liner is taking a lot of shortcut and default.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0