Use sed to remove line containing a string

The sed command can be used to remove lines containing a keyword.

Remove line containing string from standard input:

$ echo -e "1\n2\n3"
1
2
3
$ echo -e "1\n2\n3" | sed "/2/d"
1
3

Remove line containing string from a file:

$ echo -e "1\n2\n3" > myfile.txt
$ cat myfile.txt
1
2
3
$ sed -i "" "/2/d" myfile.txt
$ cat myfile.txt
1
3

Comments

Leave a Reply