The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep -n lines before and after airman_ole Shell Programming and Scripting 5 07-05-2009 10:57 PM
To find the time difference between two lines of the same log file satyakam Shell Programming and Scripting 2 04-30-2008 10:46 AM
How to delete specific lines at the same time zanetti321 UNIX for Advanced & Expert Users 1 03-24-2008 07:00 AM
grep + lines after Janus Shell Programming and Scripting 4 11-03-2006 05:56 PM
Joining 3 lines at a time Sabari Nath S Shell Programming and Scripting 14 12-21-2005 01:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-26-2008
joshjimda joshjimda is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 3
Grep two lines at a time

Hello;

i have a log file which had Invalid, error, missing words in it.
I want to grab a line which matches either of the above words and one more line below the grepped line.

Can this be done?
I looked on other places on your forum, but there is nothing which is working.
I tried following things:

sed -n '/Invalid/ {p; n; p;}' r0035251.rpt <--- But this one does not have a case sensitive as well as multiple pattern matching.

nawk is not available on my system

grep -B <---- again not available.

egrep -i command gives me the matched line, but I am not sure how to combine it with sed to be able to display the next line too!

Can anyone help?

Thanks
Josh
thanks
  #2 (permalink)  
Old 03-26-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Code:
awk '/error/ || /Invalid/ {print;getline;print}' file
  #3 (permalink)  
Old 03-26-2008
joshjimda joshjimda is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 3
thanks it works good!
How to add a case sensitive option to it.
i.e.: Want to grab either "Error" or "error"

Thanks much for quick response!
Josh
  #4 (permalink)  
Old 03-26-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Code:
awk 'tolower($0) ~ /error/ || tolower($0) ~ /invalid/ {print;getline;print}' file
  #5 (permalink)  
Old 03-26-2008
joshjimda joshjimda is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 3
last one more thing....i want to put this output to a file.
When I use awk..... >> a.log

it does not work.

thanks!
  #6 (permalink)  
Old 03-26-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by joshjimda View Post
last one more thing....i want to put this output to a file.
When I use awk..... >> a.log

it does not work.

thanks!
Works alright for me...send its output to a.log w/o a problem

Code:
awk 'tolower($0) ~ /error/ || tolower($0) ~ /invalid/ {print;getline;print}' file > a.log
  #7 (permalink)  
Old 03-26-2008
wempy's Avatar
wempy wempy is offline
Registered User
  
 

Join Date: Jun 2006
Location: Harpenden, UK
Posts: 208
GNUgrep can also do this if you have it installed.

grep -i -A 1 error log.file >a.log

-i case insensitive
-A number of context lines after the found line

other switches that may be useful:

-B number of context lines before the found line
-C number of context lines both before and after the found line
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0