grep multiple lines | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

grep multiple lines

Shell Programming and Scripting


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-02-2005
cdunavent cdunavent is offline
Registered User
 
Join Date: Sep 2001
Last Activity: 15 February 2012, 2:31 PM EST
Location: D.C.
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
grep multiple lines

Hey guys:

I've been meaning to post this question for awhile...it is regarding grep. Let's say for example that the following entry is in logxx:

Wed Feb 2 07:44:11 <vsm> 91030 Line 5 Severity 1 Vps 6
Call Answered - DN:8753101 CLID:5164665761 PI:83


If I do a grep 91030 logxx, I will get the following:

Wed Feb 2 07:44:11 <vsm> 91030 Line 5 Severity 1 Vps 6

However, what I really want are BOTH lines. Is there a way to grep and have it return both lines??

Thanks,

-cd
Sponsored Links
    #2  
Old 02-02-2005
thumsup9 thumsup9 is offline
Registered User
 
Join Date: Jan 2005
Last Activity: 3 November 2009, 12:04 PM EST
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Pls dont mind if this code seems stupid:

grep -i 91030 logxx >> file
grep -v 91030 logxx >> file
Sponsored Links
    #3  
Old 02-02-2005
cdunavent cdunavent is offline
Registered User
 
Join Date: Sep 2001
Last Activity: 15 February 2012, 2:31 PM EST
Location: D.C.
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the attempt, but that did not do the trick.

-cd
    #4  
Old 02-02-2005
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
 
Join Date: Sep 2000
Last Activity: 24 July 2014, 3:36 PM EDT
Location: Asia pacific region
Posts: 12,989
Thanks: 513
Thanked 868 Times in 397 Posts
Quote:
Originally Posted by cdunavent
Hey guys:

I've been meaning to post this question for awhile...it is regarding grep. Let's say for example that the following entry is in logxx:

Wed Feb 2 07:44:11 <vsm> 91030 Line 5 Severity 1 Vps 6
Call Answered - DN:8753101 CLID:5164665761 PI:83


If I do a grep 91030 logxx, I will get the following:

Wed Feb 2 07:44:11 <vsm> 91030 Line 5 Severity 1 Vps 6

However, what I really want are BOTH lines. Is there a way to grep and have it return both lines??

Thanks,

-cd
Please try:

grep -A1 91030 logxx

generic:

grep -A1 PATTERN [FILE...]
Sponsored Links
    #5  
Old 02-02-2005
cdunavent cdunavent is offline
Registered User
 
Join Date: Sep 2001
Last Activity: 15 February 2012, 2:31 PM EST
Location: D.C.
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
That did not work either. Here's the error:

grep: illegal option -- A
grep: illegal option -- 1
Usage: grep -hblcnsviw pattern file . . .


Here's my box info:

ROOT@arivrc: uname -a
SunOS arivrc 5.6 Generic_105181-35 sun4u sparc SUNW,UltraSPARC-IIi-Engine
Sponsored Links
    #6  
Old 02-02-2005
encrypted's Avatar
encrypted encrypted is offline Forum Advisor  
Registered User
 
Join Date: Feb 2004
Last Activity: 30 November 2011, 9:36 AM EST
Location: Oslo, Norway
Posts: 220
Thanks: 0
Thanked 1 Time in 1 Post
Assuming that the keyword in the second line is also fixed as is the one in the first line

/usr/xpg4/bin/grep -e <keyword1> -e <keyword2> <filename>

For your file, Let's say
/usr/xpg4/bin/grep -e 91030 -e Answered logxx
Sponsored Links
    #7  
Old 02-02-2005
cdunavent cdunavent is offline
Registered User
 
Join Date: Sep 2001
Last Activity: 15 February 2012, 2:31 PM EST
Location: D.C.
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
That did not work either. Let me see if I can be a little more specific.

Let's say I want to grep xx from logyy. I want not only the instance of xx to return, but the line directly below it as well (no matter what it is).

So basically, I am telling the shell..."find xx in my log yy. Every time you find it, show it to me, and while your at it, give me the line below it as well".

Does that make more sense??

-cd
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep multiple lines evelibertine UNIX for Dummies Questions & Answers 2 10-18-2011 01:13 AM
grep across multiple lines RDR UNIX for Advanced & Expert Users 4 03-28-2011 06:19 PM
grep command to find multiple strings in multiple lines in a file. ritikaSharma UNIX for Dummies Questions & Answers 2 12-17-2010 03:43 AM
grep multiple lines jOOc Shell Programming and Scripting 6 11-20-2006 07:45 PM
Grep on multiple lines gundu Shell Programming and Scripting 13 03-25-2005 01:43 PM



All times are GMT -4. The time now is 05:38 PM.