Get line before and after string - awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get line before and after string - awk
# 1  
Old 05-08-2015
Get line before and after string - awk

so below is the command i'm using to get X number of lines BEFORE and AFTER a specific string is found:

Code:
gawk '{a[NR]=$0} NR>1157 && NR<=2414 && /CALL.*EDI.*PROD.*SUPPORT/ && /CALL.*EDI.*PROD.*SUPPORT/ {for(i=NR-0;i<=NR;i++){print a[i]}for(i=1;i<2;i++){getline;print}exit}' /tmp/rolls.log


Sample log:

Code:
05/03/2015 00:45:29 unitran19:KIX0331I Trace dump written to file: TRACE0031.dmp
05/03/2015 01:00:50 unitran12:KIX0331I Trace dump written to file: TRACE0032.dmp
05/03/2015 01:15:15 unitran34:KIX0331I Trace dump written to file: TRACE0033.dmp
05/03/2015 01:30:19 unitran8 :KIX0331I Trace dump written to file: TRACE0034.dmp
05/03/2015 01:45:24 unitran26:KIX0331I Trace dump written to file: TRACE0035.dmp
05/03/2015 02:00:46 unitran7 :KIX0331I Trace dump written to file: TRACE0036.dmp
05/03/2015 02:15:14 unitran34:KIX0331I Trace dump written to file: TRACE0037.dmp
05/03/2015 02:30:25 unitran32:[WROP] : CALL EDI PROD SUPPORT                      OEii88CQX99 : 001.EQ38848P01 .200
05/03/2015 02:30:25 unitran32:[WROP] : 0.MiQCONNOP.0008002088858..PROCESS                 .OPEN ERROR FOR MQ CONN
05/03/2015 02:30:25 unitran32:[WROP] : ECTION
05/03/2015 02:30:25 unitran32:[WROP] : CALL EDI PROD SUPPORT                      OECQX9119 : 001.EQ34P2301 .200
05/03/2015 02:30:25 unitran32:[WROP] : 0.CICS    .00000    .                         .CICS ABEND
05/03/2015 02:30:25 unitran32:[WROP] :

The problem here is, when i run the command, it only outputs the first instance it finds:

Code:
BEFORE=0
AFTER=2
bash-4.1#  gawk '{a[NR]=$0} NR>1157 && NR<=2414 && /CALL.*EDI.*PROD.*SUPPORT/ && /CALL.*EDI.*PROD.*SUPPORT/ {for(i=NR-'"${BEFORE}"';i<=NR;i++){print a[i]}for(i=1;i<'"${AFTER}"';i++){getline;print}exit}' /tmp/rolls.log
05/03/2015 02:30:25 unitran32:[WROP] : CALL EDI PROD SUPPORT                      OEii88CQX99 : 001.EQ38848P01 .200
05/03/2015 02:30:25 unitran32:[WROP] : 0.MiQCONNOP.0008002088858..PROCESS                 .OPEN ERROR FOR MQ CONN

How can i modify the awk command so it shows every instance of the "CALL EDI PROD SUPPORT" instance?
# 2  
Old 05-08-2015
Since you have gawk, you'll also have GNU grep. Try:
Code:
sed -n 1157,2424p file | grep -C 1 'CALL.*EDI.*PROD.*SUPPORT'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-08-2015
Quote:
Originally Posted by Scrutinizer
Since you have gawk, you'll also have GNU grep. Try:
Code:
sed -n 1157,2424p file | grep -C 1 'CALL.*EDI.*PROD.*SUPPORT'

thank you.

it's not a guaranteed that the hosts on which this script runs on will have gawk. matter of fact, i should have just changed it to awk. i want this script to be able to be used on basically any modern unix variant.

also, the awk command i provided gives users the option of specifying how many lines BEFORE and AFTER a string is found that they want to grab as well.

any ideas?
# 4  
Old 05-08-2015
OK, then try:
Code:
awk -v m=1157 -v n=2414 'NR==m,NR==n{if(/CALL.*EDI.*PROD.*SUPPORT/){if(NR>m)print p; print; if(NR<n && (getline)>0)print} p=$0}' file

or

Code:
sed -n '1157,2414{/ALL.*EDI.*PROD.*SUPPORT/{1157!x;1157!G;2414!N;p;};h;}' file


--
or, using shell variables ..
Code:
m=1157 n=2414; sed -n $m,$n'{/ALL.*EDI.*PROD.*SUPPORT/{'$m'!x;'$m'!G;'$n'!N;p;};h;}' file


--
As usual, on Solaris use /usr/xpg4/bin/awk

Last edited by Scrutinizer; 05-08-2015 at 04:05 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-08-2015
Quote:
Originally Posted by Scrutinizer
OK, then try:
Code:
awk -v m=1157 -v n=2414 'NR==m,NR==n{if(/CALL.*EDI.*PROD.*SUPPORT/){if(NR>m)print p; print; if(NR<n && (getline)>0)print} p=$0}' file

or

Code:
sed -n '1157,2414{/ALL.*EDI.*PROD.*SUPPORT/{1157!x;1157!G;2414!N;p;};h;}' file


--
or, using shell variables ..
Code:
m=1157 n=2414; sed -n $m,$n'{/ALL.*EDI.*PROD.*SUPPORT/{'$m'!x;'$m'!G;'$n'!N;p;};h;}' file


--
As usual, on Solaris use /usr/xpg4/bin/awk
Thank you for this!

how can the awk command be modified to allow for the passing of BEFORE and AFTER lines?

so, when a string is found, you can specify how many lines BEFORE that string and AFTER the string to show? (this should include the line itself that contains the string)
# 6  
Old 05-09-2015
You'll need a ring buffer, then. Based on Scrutinizer's awk approach:
Code:
awk -v m=1 -v n=24 -v B=2 -v A=2 '
NR==m,NR==n     {if (/CALL.*EDI.*PROD.*SUPPORT/)        {for (i=1; i<=B; i++) print RB[(CNT+i)%B]
                                                         PL=NR+A
                                                        }
                 RB[(++CNT)%B]=$0
                }
NR <= PL
' file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

awk delete line if $5 contains string from list

(5 Replies)
Discussion started by: chrisjorg
5 Replies

3. Shell Programming and Scripting

Awk, if line after string does not match insert

I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah. Sample file: interface atm 1/0.190158... (3 Replies)
Discussion started by: numele
3 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

awk new line issue, saying string can't contain new line character

Hi , I am doing some enhancements in an existing shell script. There it used the awk command in a function as below : float_expr() { IFS=" " command eval 'awk " BEGIN { result = $* print result exit(result == 0) }"' } It calls the function float_expr to evaluate two values ,... (1 Reply)
Discussion started by: mady135
1 Replies

6. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

7. Shell Programming and Scripting

sed or awk to replace a value in a certain line containing a string

hi experts , I have an input like following. R sfst 1000.0000 $ new time step for mass scaled calculation R dt2ms -4.000E-7 $ friction value for blank R mue ... (10 Replies)
Discussion started by: hamnsan
10 Replies

8. Shell Programming and Scripting

awk print last line returns empty string

hello I have a file with lines of info separated with "|" I want to amend the second field of the last line, using AWK my problem is with geting awk to return the last line this is what I am using awk 'END{ print $0 }' myFile but I get an empty result I tried the... (13 Replies)
Discussion started by: TasosARISFC
13 Replies

9. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

10. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies
Login or Register to Ask a Question