Linux / Unix sed: Delete Word From File / Input

To delete a word from a file or input using the sed command in Linux or Unix, you can use the following syntax:

 
sed 's/word_to_delete//g' file.txt

This will search for all instances of the word word_to_delete in the file file.txt and delete them. The g at the end of the expression specifies that all instances of the word should be deleted, not just the first one on each line.

If you want to modify the contents of the file in place, you can add the -i option:

 
sed -i 's/word_to_delete//g' file.txt

If you want to perform the deletion on input from stdin rather than a file, you can omit the file name:

 
echo "text with word_to_delete" | sed 's/word_to_delete//g'

This will replace the input "text with word_to_delete" with the output "text with ".

Leave a Comment