How would you remove a specific string from a text file (command line) e.g.
hello
goodbye
goodbye
hello
hello
hello
goodbye
In this case I would like to remove all occurrences of "goodbye"
Either linux or Windows, (as longs as the linux command is available in GNU)
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.
Just like in VIM, we will be using the d command to delete specific pattern space with SED. To begin with, if you want to delete a line containing the keyword, you would run sed as shown below. Similarly, you could run the sed command with option -n and negated p , (! p) command.
sed -i -e 's/goodbye//g' filename
To delete multiple words:
sed -i -e 's/\(goodbye\|hello\|test\|download\)//g' filename
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