grep multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep multiple lines
# 8  
Old 02-02-2005
If it works for your version of grep the -A and -B flags specify lines before and after a pattern match.
You can also do it with sed

sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

For other tricks with sed look here:

http://www.student.northpark.edu/pem...d/sed1line.txt
# 9  
Old 02-02-2005
Yes,

grep with -A is a Linux/GNU version of grep. If you know my posts, I am a big GNU fan. Part of their man page:

Quote:
GREP(1) GREP(1)

NAME
grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
Grep searches the named input FILEs (or standard input if no files are
named, or the file name - is given) for lines containing a match to the
given PATTERN. By default, grep prints the matching lines.

In addition, two variant programs egrep and fgrep are available. Egrep is
the same as grep -E. Fgrep is the same as grep -F.

OPTIONS
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a
line containing -- between contiguous groups of matches.

...

GNU Project 2002/01/22 GREP(1)

Edited Note: Sorry, I checked and the Solaris GNU version may not support the -A option either. - Neo
# 10  
Old 02-02-2005
BTW, I recall that GNU grep in Solaris is called:

ggrep

Do you have it?

Note: Sorry, I checked and the Solaris GNU version may not support the -A option either. - Neo
# 11  
Old 02-02-2005
Please note the OS X version of the MacOX supports the -A grep option:

MacOS X Man Page on Grep
# 12  
Old 02-02-2005
Quote:
Originally Posted by thumsup9
You can also do it with sed

sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
I kinda like that. But I think all he wants is just...
sed -n '/regexp/{N;p;}'
# 13  
Old 02-02-2005
From GOOGLE:

How do I grep a file for "pattern" so the output shows the 3 lines above and below each "pattern" line (including "pattern" line)? I used to have a simple grep/sed/awk to do it but I lost it. I thought it used grep and tail, but so far my attempts don't work.


Response Number 1

does your "man grep" discuss "context"?
if not:
ex -R file<<!
g/pattern/.-3,.+3p
q
!


Response Number 2

Colourised awk version:
awk 'BEGIN {c=3};/PATTERN/ {p=c+1;$0="\033[31m"$0"\033[m"};{if (p==0 || p==c+1) {buf=buf"\n"$0;b++};if (b>c+1) {b--;sub(/\n[^\n]*\n/,"\n",buf)};if (p==c+1) {sub(/^\n/,"",buf);print buf;b=0;buf="";p-- ;next};if (p>0) {print;p--}}' file

Replace 'c=3' for different amounts of context, replace 'PATTERN' with your pattern and replace '31m' with the colour you prefer or remove everything except 'p=c+1' in that action bracket if you don't want colours.


Response Number 3

grep -A3 -B3 <pattern> filename
A-After
B-Before
# 14  
Old 02-02-2005
The sed idea is definitely a step in the right direction. I may very well be able to use that. However, it may get a little tricky with the regexp portion as seen below in red:

sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

What if I have prompted users for input prior to this portion of the script, and I want to read in (for example) $NAME and $ADDRESS in addition to the regexp I told you guys about earlier?? I tried enclosing everything in double quotes, single quotes, and backticks.

M
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude multiple lines using grep

Hi, I'm working on a shell script that reports service status on a database server. There are some services that are in disabled status that the script should ignore and only check the services that are in Enabled status. I output the service configuration to a file and use that information to... (5 Replies)
Discussion started by: senthil3d
5 Replies

2. Shell Programming and Scripting

Grep and display multiple lines

Hi guys, I have a log file that generates multiple logs about a query. <query time='2016-04-13 13:01:50.825'> <PagingRequestHandler> <Before>brand:vmu</Before> <After>brand:vmu</After> </PagingRequestHandler> <GroupDeviceFilterHandler> <Before>brand:vmu</Before> ... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

Grep and exttract multiple lines

Hello: I am trying to use grep in cygwin to do the following, however I am unable to get the output in the desired format. Please see and let me know how to solve this Input log file 20140403 07:29:26 IN:CTRL=:TYP=TYP1:DCN=DCN1:DATA= 20140403 07:25:26 IN:CTRL=:TYP=TYP1:DCN=DCN2:DATA=... (4 Replies)
Discussion started by: wincrazy
4 Replies

4. Shell Programming and Scripting

grep from multiple lines in several gz files

Hello all, I have been struggling to get grep work to my requirements. Basically I have to filter out patterns spread across multiple lines over hundreds of .gz files in a folder. And the output needs to be piped to a file. Here is the example: folder name: logs files in this folder:... (4 Replies)
Discussion started by: mandhan
4 Replies

5. UNIX for Dummies Questions & Answers

Grep multiple lines

I want to grep multiple lines from a text file. I want to grep all lines containing X,Y and NA in a single command. How do I go about doing that? This is what my text files look like: rs1983866 0.0983 10 100016313 rs1983865 0.5994 X 100016339 rs1983864 0.3272 11 100017453 rs7077266... (2 Replies)
Discussion started by: evelibertine
2 Replies

6. UNIX for Advanced & Expert Users

grep across multiple lines

How do you grep 'select * from table_name' string from a script if the select * and from table_name are on 2 different lines ? like select * from table_name Any help would be greatly appreciated !!! Thanks RDR (4 Replies)
Discussion started by: RDR
4 Replies

7. UNIX for Dummies Questions & Answers

grep in multiple lines

hi i have kind of below text in a file. I want to get a complete paragraph starting with START and ending with before another START) which has a particular string say XYZ or ABC START XYZ hshjghkjh 45 ljkfd fldjlj d jldf START 3493u ABC 454 4545454 4545454 45454 4545454 START ...... (3 Replies)
Discussion started by: reldb
3 Replies

8. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

9. Shell Programming and Scripting

grep multiple lines

Hi. I have this format on a textfile: VG Name /dev/vg00 PV Name /dev/dsk/c16t0d0 PV Name /dev/dsk/c18t0d0 PV Name /dev/dsk/c16t4d0 VG Name /dev/vg01 PV Name ... (6 Replies)
Discussion started by: jOOc
6 Replies

10. Shell Programming and Scripting

Grep on multiple lines

I 'm trying to grep 2 fieldds on 2 differnt lines. Like this: psit > file egrep -e '(NS|ES)' $file. Not working. If this succeeds then run next cmd else exit. Pls Help Gundu (13 Replies)
Discussion started by: gundu
13 Replies
Login or Register to Ask a Question