grep multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep multiple lines
# 22  
Old 02-02-2005
For fun, watch how easy this is on Linux with GNU grep and the -A option:

Quote:
www# cat file
111
222
333
444
555
666
777
888
999
000

Quote:
www# grep -A1 333 file
333
444
www#
Quote:
www# grep -A3 444 file
444
555
666
777
www#
That is why I always used to build these GNU utilities everywhere I worked in my sys-admin days. (seems so long ago...LOL)

Smilie
# 23  
Old 02-02-2005
Quote:
Originally Posted by cdunavent
Does that make more sense???
Quite a bit! (Why not do that in the first post?)
Code:
#! /usr/bin/ksh

print -n  "Enter Line - "
read line
print -n "Enter Vps - "
read vps
sed -n '/Line '${line}' .*Vps '${vps}'/{N;p;}'   < inputfile
exit 0

# 24  
Old 02-03-2005
That did the trick. Thanks for all the help, guys.
# 25  
Old 03-08-2005
this is a little cleaner on one line

awk '/91030/ {print $0} {getline;print $0}' logxx
Smilie
# 26  
Old 03-22-2005
Quote:
Originally Posted by Perderabo
Quite a bit! (Why not do that in the first post?)
Code:
#! /usr/bin/ksh

print -n  "Enter Line - "
read line
print -n "Enter Vps - "
read vps
sed -n '/Line '${line}' .*Vps '${vps}'/{N;p;}'   < inputfile
exit 0

This is almost exactly what I need, but I need to get the line before the match as well as some lines after it. How would I get the line before the match? I also do not have the -B and -A options or ggrep.
Thanks.

EDIT: I continued playing around with it and beating my head against the wall. I got this and it seems to work:

Code:
sed -n -e /regex/ {=;x;p;g;N;p;D;}' -e h searchfile


Last edited by exharrison; 03-23-2005 at 10:24 AM..
# 27  
Old 03-25-2005
Here is a usefull script that might be what you need...
It's a perl script that is a windowing grep that is useful for grabbing X number of lines before and after a match.

http://sysunconfig.net/unixtips/wgrep.txt


Smilie
# 28  
Old 03-27-2005
Here is a working solution

All you need is this command:

awk 'c==1{print $0; c=0} /Search_String/{print $0; c=1}' File_Name.txt

---------------------------------------------------------------------

Here is how it works in action:

> cat file.txt

Apples1: 111
Product Code = 01

Oranges1: 222
Product Code = 02

Grapes1: 333
Product Code = 03

Apples2: 111
Product Code = 04

Oranges2: 222
Product Code = 05

Grapes2: 333
Product Code = 06

> awk 'c==1{print $0; c=0} /Oranges/{print $0; c=1}' file.txt
Oranges1: 222
Product Code = 02
Oranges2: 222
Product Code = 05
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