Gawk Find Pattern Print Lines Before and After


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Gawk Find Pattern Print Lines Before and After
# 1  
Old 10-31-2013
Gawk Find Pattern Print Lines Before and After

Using grep I can easily use:

Code:
cvs log |grep -iB 10 -A 10 'date: 2013-10-30'

to display search results and 10 lines before and after. How can this be accompished using gawk?
# 2  
Old 10-31-2013
Use an indexed array to store the lines before search pattern and print when pattern is matched.

Something like this should work:
Code:
cvs log | gawk -v L=10 '
        !/date: 2013-10-30/ {

                A[++c] = $0
        }
        /date: 2013-10-30/ {
                for ( i = (c - (L-1)); i <= c; i++ )
                        print A[i]
                print
                for ( j = 1; j <= L; j++ )
                {
                        getline
                        print
                }
                delete A
                c = 0
        }
'


Last edited by Yoda; 10-31-2013 at 07:15 PM.. Reason: delete array
# 3  
Old 10-31-2013
Try:
Code:
gawk 'c-->0; {p=p RS $0}; $0~s{print p; c=A} NR>=B{sub(/[^\n]*\n/,x,p)}' IGNORECASE=1 A=10 B=10 s='date: 2013-10-30' file


--
IGNORECASE is used because the OP's example was using the -i option and is looking for a GNU awk example...

Last edited by Scrutinizer; 10-31-2013 at 11:38 PM.. Reason: Corrected A and B, thanks RudiC
# 4  
Old 10-31-2013
As has been proposed many many times before: use a circular buffer, e.g.
Code:
awk     '               {T[NR%(B+1)]=$0}
         $0~s           {for (i=NR+1; i<=NR+B; i++) print T[i%(B+1)]
                         L=NR+A+1}
         NR<L
        ' A=10 B=10 s='date: 2013-10-30' file

@Scrutinizer: Nice and curt! But I think you mixed grep's A(fter) and B(efore), and you implied that RS equals \n.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-31-2013
Thanks, yes I reversed the meaning of "A" and "B" , corrected it in my post. Yes RS always equals "\n" in this case because this is meant to imitate grep's behavior, where this is always the case..

Last edited by Scrutinizer; 10-31-2013 at 11:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Find key pattern and print selected lines for each record

Hi, I need help on a complicated file that I am working on. I wanted to extract important info from a very huge file. It is space delimited file. I have hundred thousands of records in this file. An example content of the inputfile as below:- ## ID Ser402 Old; 23... (2 Replies)
Discussion started by: redse171
2 Replies

3. AIX

Grep a pattern and print following n lines

Hi all, I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing. I have used the below code grep -A 3 "pattern" filename But it is throwing error as below. grep: illegal option -- A Can... (14 Replies)
Discussion started by: ssk250
14 Replies

4. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

5. Shell Programming and Scripting

Print lines before after pattern meacted

Hi All, Am trying to print pervious lines of the pattern matched, I was able to print pattern matched and the required data but don't know how to get the pervious line. ifconfig -a | grep 10.118.67.33 | sed -e 's/^]*//' -e 's/]*$//' | awk '{FS=" "; print $6}' output I... (2 Replies)
Discussion started by: Optimus81
2 Replies

6. Shell Programming and Scripting

need to print lines between repeated pattern

Hi all, I have a file that looks like this: uid=bessemsj version: 1 dn: cn=Desk SpecialAdminDesk, ou=Desks, dc=DSS,c=nl,o=Vodafone dn: cn=DSS Advisors, ou=Groups, dc=DSS,c=nl,o=Vodafone dn: cn=DSS Dispatcher,ou=Groups,dc=DSS,c=nl,o=Vodafone dn: cn=Desk Retention Desk,ou=Desks,... (13 Replies)
Discussion started by: Eman_in_forum
13 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

print lines up to pattern excluding pattern

11 22 33 44 55 66 77 When pattern 55 is met, print upto it, so output is 11 22 33 44 (1 Reply)
Discussion started by: anilcliff
1 Replies

9. Shell Programming and Scripting

gawk print some special lines

Hi every body, i have this file example : TD1 TD2 TD3 . . .TDn <DIE_1> xxxxxx <\DIE_1> <TD1> information 1 inormation n <\TD1> <TDq> information (0 Replies)
Discussion started by: kamel.kimo
0 Replies

10. Shell Programming and Scripting

print certain pattern from 2 lines

i have 2 lines comming out of a script o/p.below the line. 2008-10-14 05:47:05,551 INFO - LPBatch: 2008-10-14 05:47:05,575 INFO - Number of Intervals Not Inserted: 1 / 95 -------------------------------------------------------------------------- How to print the below o/p from the... (2 Replies)
Discussion started by: ali560045
2 Replies
Login or Register to Ask a Question