You're viewing a comment by RenaudD and its responses.

RenaudD Permalink
October 08, 2008, 23:57

I am a sed addict and your nice site won't help me to cure. I love the

sed -n '$='

:).
Maybe this will interest you :
Sometimes you have to print blocks of lines that contains a special string. For instance if filename contains :

foo
baz
bar

bar
foo

goo
baz

and you would like to get only the blocks that contain 'baz' that is :

foo
baz
bar

goo
baz

I do sthg close to it that way :

sed -n 'H;/^$/x;/baz.*\n/p' filename

I said 'close to it' because unfortunately this command prints a bit too many empty lines before the selected blocks but generally I don't mind. Anyway if you find a way to straighten it I would be delighted to know it.

Note : you have to make sure the last line of filename is an empty line otherwise the last block won't be processed :(

Thanks again for the site !

PS sorry I am not very talented for HTML, I hope all this will print correctly

Comment Responses

Anirudh Permalink
February 02, 2016, 18:56
/./!{ $!d; g; /./q; d; }

:loop
   $bend; N
/\n$/!bloop
s///

$!{
   /baz/{ x; /./s/$/\n/p; }
   d
}

:end
#${
   /baz/{ x; /./s/$/\n/p; g; q; }
   g; /./q; d
#}

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "0day_95": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.