Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing lines between two patterns (not inclusive) with sed

Tags:

bash

sed

Ok

I know that this is trivial question but: How can i remove lines from files that are between two known patterns/words:

pattern1
garbage
pattern2

to obtain:

pattern1
pattern2

And does anyone known good(simple written!) resources for studying sed?? With many clear examples?

like image 405
kasper Avatar asked Feb 21 '11 22:02

kasper


People also ask

How do I delete a sed matching line?

To begin with, if you want to delete a line containing the keyword, you would run sed as shown below. Similarly, you could run the sed command with option -n and negated p , (! p) command. To delete lines containing multiple keywords, for example to delete lines with the keyword green or lines with keyword violet.

How do I remove a specific pattern from a Unix file?

N command reads the next line in the pattern space. d deletes the entire pattern space which contains the current and the next line. Using the substitution command s, we delete from the newline character till the end, which effective deletes the next line after the line containing the pattern Unix.

How do you insert a line break in sed?

The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a new line after a match is found. The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found.


1 Answers

This might work for you:

sed '/pattern1/,/pattern2/{//!d}' file
like image 147
potong Avatar answered Sep 28 '22 09:09

potong