Skip to main content
added 15 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

Assuming it is more important to keep lines that contain something- than it is to delete lines that contain something:

sed '/something/ { /something-/!d; }' file

This finds all lines containing the string something, and deletes all of those lines unless they also contain something-.

Testing:

$ cat file
something something blah blah
something something- blah blah
somethingelse
something
something-
$ sed '/something/ { /something-/!d; }' file
something something- blah blah
something-

Assuming it is more important to keep lines that contain something- than it is to delete lines that contain something:

sed '/something/ { /something-/!d; }' file

This finds all lines containing the string something, and deletes all of those lines unless they also contain something-.

Testing:

$ cat file
something something blah blah
something something- blah blah
something
something-
$ sed '/something/ { /something-/!d; }' file
something something- blah blah
something-

Assuming it is more important to keep lines that contain something- than it is to delete lines that contain something:

sed '/something/ { /something-/!d; }' file

This finds all lines containing the string something, and deletes all of those lines unless they also contain something-.

Testing:

$ cat file
something something blah blah
something something- blah blah
somethingelse
something
something-
$ sed '/something/ { /something-/!d; }' file
something something- blah blah
something-
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

Assuming it is more important to keep lines that contain something- than it is to delete lines that contain something:

sed '/something/ { /something-/!d; }' file

This finds all lines containing the string something, and deletes all of those lines unless they also contain something-.

Testing:

$ cat file
something something blah blah
something something- blah blah
something
something-
$ sed '/something/ { /something-/!d; }' file
something something- blah blah
something-