Grep a pattern and print following n lines


 
Thread Tools Search this Thread
Operating Systems AIX Grep a pattern and print following n lines
# 8  
Old 02-06-2015
Perhaps something like:
Code:
/usr/xpg4/bin/awk '
function pprev(		i) {
	for(i = (NR > LINES) ? NR - LINES + 1 : 1; i <= NR; i++) 
		print l[i % LINES]
	print ""
}
{	l[NR % LINES] = $0
}
$0 ~ PAT {
	pprev()
}' LINES=3 PAT='9' file

If file contains 100 lines numbered from 100 to 001:
Code:
100
099
098
098
...
003
002
001

the output produced is:
Code:
100
099

100
099
098

099
098
097

098
097
096

097
096
095

096
095
094

095
094
093

094
093
092

093
092
091

092
091
090

091
090
089

081
080
079

071
070
069

061
060
059

051
050
049

041
040
039

031
030
029

021
020
019

011
010
009

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 02-06-2015
The following shell command emulates the ggrep -E -A4, and works on all Unix:
Code:
PATH=/usr/xpg4/bin:/bin:/usr/bin awk '$0~PAT {a=A+1} (a && a--)' A=4 PAT="ISEND" sqloutput1.log

Omit the A=x to have the normal grep -E behavior.
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 02-06-2015
Hi.

Some general alternatives, as opposed to single-purpose solutions:
Code:
Print lines above, below pattern-matched line (context, window); "only" string matching pattern
	1) GNU grep -A -B; ggrep on some Solaris

	2) peg, a perl script, does not use "-o" like GNU grep;
	allows -A, -B

	3) ack, a perl script, can use "-o" like GNU grep; allows -A,
	-B

	4) cgrep does not use "-o" like GNU grep; allows -nline,
	+nline for context like GNU grep -A, -B

	5) bool, prints context in bytes

	6) xtcgrep, extension of CPAN grep; allow -C context; allows -o;
	complicated calling sequence; http://freecode.com/projects/greppm

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 11  
Old 02-06-2015
The probably easiest way is to use good old sed instead of all the fancy tools:

Code:
sed -n '/<search-pattern>/ {;N;...N;p;}' /path/to/file

Put in x-1 "N"s to display x lines of text after the matched pattern.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 12  
Old 02-07-2015
Quote:
Originally Posted by drl
Hi.

Some general alternatives, as opposed to single-purpose solutions:
Code:
Print lines above, below pattern-matched line (context, window); "only" string matching pattern
    1) GNU grep -A -B; ggrep on some Solaris

    2) peg, a perl script, does not use "-o" like GNU grep;
    allows -A, -B

    3) ack, a perl script, can use "-o" like GNU grep; allows -A,
    -B

    4) cgrep does not use "-o" like GNU grep; allows -nline,
    +nline for context like GNU grep -A, -B

    5) bool, prints context in bytes

    6) xtcgrep, extension of CPAN grep; allow -C context; allows -o;
    complicated calling sequence; http://freecode.com/projects/greppm

Best wishes ... cheers, drl


Thanks ... ggrep is working on Solaris.
# 13  
Old 02-07-2015
Problem solved. But the previous sed solution needs a fix; (at least on Solaris) it does not print at the end of a file, because the N command terminates the script. Therefore, each N is to be preceded by a $p.
The following works with a number for the trailing lines (here: 3).
Code:
sed -e '/^<search-pattern>/!d;:L' -e '$p;N;s/\n/&/3;t' -e 'bL' file

---------- Post updated at 10:19 AM ---------- Previous update was at 10:14 AM ----------

Hmm interesting, GNU sed prints at the end of the file, so the $p would print twice...
Which sed is correct?
This User Gave Thanks to MadeInGermany For This Post:
# 14  
Old 02-09-2015
Quote:
Originally Posted by MadeInGermany
But the previous sed solution needs a fix; [...]
Hmm interesting, GNU sed prints at the end of the file, so the $p would print twice...
You objection is correct and it was my bad not to think my solution through to the end. GNU-sed - as it is so often is the case - gets it wrong (again).

bakunin
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. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. 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

4. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

8. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

9. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 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