Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiline pattern delete with single line command

Tags:

grep

sed

awk

perl

I would like to delete all empty segments in my file.

The empty segment can be specified by a pair of consecutive lines starting with START and ending with END. Valid segments will have some contents between lines starting with START and ending with END

Sample Input

Header

START arguments
END

Any contents

START arguments
...
something
...
END

Footer

Desired Output

Header


Any contents

START arguments
...
something
...
END

Footer

Here I'm looking for possible one liners. Any help would be appreciated.

Trials

I tried following awk. It works to some extent but it deletes START lines even in valid segments.

awk '/^START/ && getline && /^END$/ {next} 1' file
like image 876
jkshah Avatar asked Dec 12 '22 10:12

jkshah


1 Answers

perl -00 -pe 's/START .*?\nEND//g' file

this is a better one. the solution I gave earlier will discard whole paragraph if they are not separated by blank lines.

Earlier response below:

how about this perl one liner ?

perl -00 -ne 'print if not /START .*\nEND/' file

read-in file in paragraph mode and discard lines matching START <string><newline>END

like image 159
dpp Avatar answered Jan 06 '23 10:01

dpp