picknick
picknick

Reputation: 4007

How can I grep these lines

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?

Upvotes: 1

Views: 133

Answers (1)

PP.
PP.

Reputation: 10864

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.

Upvotes: 2

Related Questions