Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 12-16-2003, 10:21 AM   #1
Registered: Apr 2003
Location: Silicon Valley East, Northern Virginia
Distribution: FreeBSD,Debian, RH, ok well most of em...
Posts: 238

Rep: Reputation: 30
Searching a text file - complicated

have a question on how to grep (sed or awk) thru a file and pull back 2 lines. Line 1 has an error code and time stamp, line 2 which is directly below it has a string that i am searching for. How can I search and pull out line 2 to a file and also bring the line above it?
Old 12-16-2003, 10:43 AM   #2
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30

'man grep' would have answered this for you.

'grep -B 2 <regexp> <file>'

Old 12-16-2003, 10:45 AM   #3
LQ Newbie
Registered: Nov 2003
Distribution: Red Hat Linux
Posts: 7

Rep: Reputation: 0
grep -A2 'error_code' filename

ll show actual line and 1 line After-context that, same way you can try '-B1' - to see 1 line Before-context, and '-1' for Before & After context.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
text searching sopiaz57 Linux - Software 4 06-02-2005 01:00 PM
Searching text files by content will103 Linux - Software 1 01-24-2005 07:43 AM
searching inside text files minm Linux - Newbie 2 01-08-2005 11:56 PM
C help needed - reading from a complicated text file Keffin Programming 4 01-26-2004 09:32 PM
Complicated Text -> Numbers mickey717 Linux - Newbie 5 10-17-2003 01:41 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:38 PM.

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