bash - Use SED to delete all lines between a single repeating pattern -


i'd use sed delete lines between pattern repeats throughout text file.

input

set eng_1 blah blah blah blah set eng_2 blah blah blah blah set test blah blah blah blah set eng_5 blah blah blah blah set open blah blah blah blah set eng_10 blah blah blah blah 

there multiple set eng_# lines, never know number on end be. want remove lines between set eng_ , next line starts set.

desired output

set eng_1 set eng_2 set test blah blah blah blah set eng_5 set open blah blah blah blah set eng_10 

i edit file in place, using -i option in sed.

my attempt

here have tried:

sed -i "/set eng_/,/set eng_/{//!d}" $myfile 

it works on first occurrence, output:

set eng_1 set eng_2 blah, blah blah blah 

how can change approach obtain desired output?

this might work (gnu sed):

sed -r '/set/!b;:a;$!{n;ba};s/((set)[^\n]*\n).*\n([^\n]*\2)/\1\3/' file 

this retain first , last patterns (in case set).

this alternative remove first , last patterns well:

sed -r '/set/!b;:a;$!{n;ba};s/[^\n]*(set).*\1[^\n]*\n?//' file 

on reading amendment question, perhaps might work you:

sed -ni ':a;/^set eng_[1-9]/{p;:b;$q;n;/^set/ba;bb};p' file 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -