I'm new to sed
and I have the following question. In this example:
some text here blah blah 123 another new line some other text as well another line
I want to delete all lines except those that contain either string 'text' and or string 'blah', so my output file looks like this:
some text here blah blah 123 some other text as well
Any hints how this can be done using sed
?
To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.
Sed Command to Delete Lines – Based on Pattern Match. In the following examples, the sed command deletes the lines in file which match the given pattern. ^ is to specify the starting of the line. Above sed command removes all the lines that start with character 'u'.
The sed command can remove the lines of any range. For this, we just have to enter 'minimum' and 'maximum' line numbers. In this example, we will remove the lines ranging from 4 to 7 numbers. After removing these ranges of lines, our file will look like this.
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
This might work for you:
sed '/text\|blah/!d' file some text here blah blah 123 some other text as well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With