LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   grep query to list 1 line [which is fixed] and the next line after it [variable text] (https://www.linuxquestions.org/questions/linux-software-2/grep-query-to-list-1-line-%5Bwhich-is-fixed%5D-and-the-next-line-after-it-%5Bvariable-text%5D-857491/)

Glenn D. 01-20-2011 06:07 AM

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

syg00 01-20-2011 06:19 AM

what did your reading of the manpage indicate ?

AlucardZero 01-20-2011 06:20 AM

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.


druuna 01-20-2011 06:21 AM

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.


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