LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-20-2011, 06:07 AM   #1
Glenn D.
Member
 
Registered: May 2009
Location: ACT - Australia
Distribution: Opensuse x86_64 (Latest)
Posts: 132

Rep: Reputation: 26
grep query to list 1 line [which is fixed] and the next line after it [variable text]


Hello,
How can I list the following with grep.
I want to extract 2 lines fron a text file

The fixed known part if it exists will static text and the text line after it will change.


A sample file
.
.
textline1
.
.
build: extracting built packages...
somename-2.6.36.3-16.1.i586.rpm
anothername2.6.36.3-16.1.i586.rpm
.
<eof>

In the above case it would return
build: extracting built packages...
somename-2.6.36.3-16.1.i586.rpm



If the fixed part does Not exist how can I return error code 1
Thanks Glenn
 
Old 01-20-2011, 06:19 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
what did your reading of the manpage indicate ?
 
Old 01-20-2011, 06:20 AM   #3
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
grep -A1 "build: extracting built packages" foofile

Code:
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing
              a group separator (--) between contiguous  groups  of  matches.   With  the  -o  or
              --only-matching option, this has no effect and a warning is given.
 
Old 01-20-2011, 06:21 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

grep -A1 "build: extracting built packages..." infile

Grep already tells you if the grep succeeded or not (echo $?)
0 - lines are found
1 - lines are not found
2 - an error occurred

Hope this helps.
 
1 members found this post helpful.
  


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
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-04-2010 11:38 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 05:28 AM
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 03:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:37 PM.

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