tags:

views:

55

answers:

1

I want to grep lines starting with a @ and also lines starting with // followed by a line starting with @

Example:

//text1
@text2
text3

result:

//text1
@text2

How can I do this with grep or any other basic unix tool?

+3  A: 
perl -ne 'print( $z . $_ ) if m{^\@}; $z=(m{^//} ? $_ : "");'

This one-liner processes STDIN one line at a time.

If a line beginning with @ is found it dumps the contents of $z followed by the current line.

Then, if it detects a line beginning with // it saves the line in a variable $z. The $z variable is cleared if the line does not start with //.

I have given this a quick test and should do the job requested.

The grep tool does not remember state between lines.

PP
Works perfect. Thanks!
picknick