sed delete lines not containing specific string
Solution 1:
This might work for you:
sed '/text\|blah/!d' file
some text here
blah blah 123
some other text as well
Solution 2:
You want to print only those lines which match either 'text' or 'blah' (or both), where the distinction between 'and' and 'or' is rather crucial.
sed -n -e '/text/{p;n;}' -e '/blah/{p;n;}' your_data_file
The -n
means don't print by default. The first pattern searches for 'text', prints it if matched and skips to the next line; the second pattern does the same for 'blah'. If the 'n' was not there then a line containing 'text and blah' would be printed twice. Although I could have use just -e '/blah/p'
, the symmetry is better, especially if you need to extend the list of matched words.
If your version of sed
supports extended regular expressions (for example, GNU sed
does, with -r
), then you can simplify that to:
sed -r -n -e '/text|blah/p' your_data_file
Solution 3:
You could simply do it through awk,
$ awk '/blah|text/' file
some text here
blah blah 123
some other text as well