tags:

views:

161

answers:

3

I have a text file named compare.txt that I want to extract the single line that follows every line that contains the pattern "nmse_gain_constant". The following command gets me close:

grep -A 1 nmse_gain_constant compare.txt | grep -v nmse_gain_constant

But this includes a separator "--" line between every line of desired text. Any easy ideas how to get rid of the "--" lines?

I'd give you some example text here, but the "--" characters get erroneously interpreted by this web post as control characters and what you would see is not correct.

+1  A: 

One solution will be :

grep -A 1 nmse_gain_constant compare.txt | grep -v nmse_gain_constant  | grep -v "\-\-"
Am
This works fine. Thanks!
Michael
A: 

Well the A switch by default will add those characters, so its no mystery.

man grep states

   -A NUM

          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.

BUt you can use a simple sed to clean up the result

yourgrep | sed '/--/d'
Eddie
This works great. Thank you.
Michael
+2  A: 

no need to pipe to so many greps or use other tools (eg sed) if you use awk

awk '/nmse_gain_constant/{getline;print }' compare.txt
ghostdog74
for grep -B use:awk '/regex/{ print x; print }; { x=$0 }'
D W