Tim
Tim

Reputation: 11

Linux: How to delete lines starting at a pattern to another pattern using sed

I am aware of how to delete certain patterns with the help of sed.

Here is an example:

exampleFile

One Two Three Four
Five Six Seven
Eight One Nine Four

If I apply the following, all 'One's will be deleted.

sed 's/\<One\>//g' exampleFile

But what if I wanted to delete everything starting from One until Four? The output I am looking for is:

Five Six Seven
Eight

I thought about writing the following, but it does not work:

sed 's/\<One*Four\>//g' exampleFile

(I thought that by putting an *, it should mean delete everything in between and including One and Four)... Is my request even possible in one line?

Thank you!

Upvotes: 1

Views: 76

Answers (3)

potong
potong

Reputation: 58568

This might work for you (GNU sed):

sed ':a;/\<One\>.*\<Four\>/{s/\<Four\>/\n/;s/\<One\>[^\n]*\n//;/^\s*$/d;ba}' file

This checks to see if the strings One and Four occur on the same line and if so replaces the second string with a marker (\n) and then removes from the first string to and including the marker. If the remaining line is empty or contains only white space the line is deleted otherwise the process is repeated till the first condition no longer applies.

N.B. This caters for lines where the second string may occur more than once. The method above can be applied to multiple lines however care must be taken to ensure that both strings exist.

Upvotes: 0

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5308

Adding to @that other guy's answer, removing blank line as well

sed -e 's/\<One\>.*\<Four\>//g' -e '/^$/d' exampleFile

Upvotes: 1

that other guy
that other guy

Reputation: 123680

In regex, . means "any character" and * means "any number of the previous element", so you'd do:

sed 's/\<One.*Four\>//g' exampleFile

You can further add appropriate angle brackets to ensure that the One and Four have to be separate words:

sed 's/\<One\>.*\<Four\>//g' exampleFile

Upvotes: 2

Related Questions