0

I am trying to run sed command to display the text from a file by excluding lines starting from "This". I am able to perform the same operation with grep but having trouble with sed. I tried the following command :

sed -n '/[^This]/p' example

It is printing out the whole file as it is.

This is first line of example text
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
This is a line not containing any erorrs.
This is the last line.

Thanks in advance for any help!

2 Answers2

2

like this:

sed '/^This.*$/d' example 
AnJo
  • 74
  • 6
0

One option would be to skip printing the lines that start with This.

sed -n 's/^This// ; t end; p; :end'

What it does is as follows:

  1. Similar to your attempt it uses the -n option to avoid automatic printing.
  2. Next it tries to substitute This at the beginning of the line with an empty string.
  3. We need to perform the substitution only to activate the following t command that branches straight to the following :end label. The line would be discarded anyway.
  4. If the substitution did not occur and therefore the t command was not activated, the p command prints the line out.

The result would be:

It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76