aix :grep to get lines before and after string | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


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

aix :grep to get lines before and after string

Shell Programming and Scripting


Tags
aix, unix

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-03-2012
PhAnT0M PhAnT0M is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 3 May 2012, 10:55 AM EDT
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
aix :grep to get lines before and after string

am using AIX and I have a string "There is no process to read data written to a pipe". I want to get the output 2 lines before and 4 lines after this string. The string is present like more than 100 times in the log and I want to output, the last result in the log with this string

I tried using :


Code:
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="There is no process to read data written to a pipe" File.log

The output from this command is that I am getting all the 100 plus results where the above string is present

The -A number -B number command is not working in AIX
Sponsored Links
    #2  
Old Unix and Linux 05-03-2012
hfreyer hfreyer is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 18 March 2014, 9:53 PM EDT
Posts: 40
Thanks: 0
Thanked 6 Times in 6 Posts
You need to retain the output until the end and reset it at every match.
Here is a sample code, though not very elegant:

Code:
nawk 'c-->0{res=sprintf("%s%s\n",res,$0)};$0~s{res="";if(b)for(c=b+1;c>1;c--)res=sprintf("%s%s\n",res,r[(NR-c+1)%b]);res=sprintf("%s%s\n",res,$0);c=a}b{r[NR%b]=$0}END{printf("%s",res)}' b=2   a=4 s="There is no process to read data written to a pipe" File.log

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep a string from input file and delete next three lines including the line contains string in xml greet_sed Shell Programming and Scripting 4 09-28-2011 06:18 AM
Print lines between two lines after grep for a text string jbruce Shell Programming and Scripting 7 09-29-2010 03:37 PM
Like grep -v for a string over 2 lines? Sed? jake657 Shell Programming and Scripting 1 03-31-2009 02:42 PM
grep string & a few lines after ashterix Shell Programming and Scripting 7 12-07-2008 09:20 AM
grep string & next n lines ashterix Shell Programming and Scripting 8 11-21-2005 11:38 PM



All times are GMT -4. The time now is 02:45 PM.