LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2009, 12:22 AM   #1
poiuytrez
LQ Newbie
 
Registered: May 2009
Posts: 15

Rep: Reputation: 0
Smile grep line above and below


Hi,

I have to parse a very large file and I want to use the command grep (or any other tool).

I have to check in a log line the word "FAILED", print the line above and below.

For example:
---------
id : 15
Satus : SUCESS
Message : no problem
---------
id : 15
Satus : FAILED
Message : connection error
---------


And I need to print only
id : 15
Satus : FAILED
Message : connection error


Thank you for your help !
 
Old 07-02-2009, 12:34 AM   #2
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS, Manjaro
Posts: 179

Rep: Reputation: 27
Use grep -A1 -B1 ^id file_name


Quote:
Originally Posted by poiuytrez View Post
Hi,

I have to parse a very large file and I want to use the command grep (or any other tool).

I have to check in a log line the word "FAILED", print the line above and below.

For example:
---------
id : 15
Satus : SUCESS
Message : no problem
---------
id : 15
Satus : FAILED
Message : connection error
---------


And I need to print only
id : 15
Satus : FAILED
Message : connection error


Thank you for your help !
 
Old 07-02-2009, 12:45 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Code:
grep -C 1 FAILED name_of_the_file
or

Code:
grep -1 FAILED name_of_the_file
 
Old 07-02-2009, 01:29 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
See here (example 8) using awk
 
Old 07-02-2009, 02:03 AM   #5
poiuytrez
LQ Newbie
 
Registered: May 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks sycamorex !

It really works good !
 
Old 07-04-2009, 04:18 PM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
I know it's considered rude to tell you to RTM, the options suggested are in the 1st 6 in the man page:
Quote:
-A NUM, - -after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing - - between contiguous groups of matches.
-a, - -text
...
-B NUM, - -before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing - - between contiguous groups of matches.
-b, - -byte-offset
...
- -binary-files=TYPE
...
-C NUM, - -context=NUM
Print NUM lines of output context. Places a line containing - - between contiguous groups of matches.
Did you even bother to look?

BTW, grep is one of those man pages that should be re-read regularly.
 
Old 07-04-2009, 08:27 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by archtoad6 View Post
I know it's considered rude to tell you to RTFM, the options suggested are in the 1st 6 in the man page:
no, its not rude at all. Its just common sense to look there first, after that, google next if still in doubt. Further more, if the answer is exactly written in man page, its alright to tell him to RTFM

Last edited by ghostdog74; 07-04-2009 at 08:28 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep words and paste is on the same line say_hi_ravi Programming 7 10-31-2008 06:56 AM
Script to grep a line yusufs Linux - Newbie 1 04-20-2008 02:44 AM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
Blank line - grep. liguorir Linux - Software 1 04-24-2004 09:03 AM
grep a line in a file from dir bkeating Linux - Newbie 4 07-24-2003 02:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration