I want to delete lines that contain a certain string, for example "Cola", but only if the line matches exactly and has the same number of characters. In other words, don't delete lines with the word "Cola" inside but that are larger.

sed '/^Cola$/d' < input


grep -Fxv Cola < input


awk '$0 != "Cola"' < input

This the way to delete a line containing and only containing the string "Cola":

cat somefile.txt | sed '/^Cola$/d' > newfile.txt

  • You don't need to use the cat command there, the following would be enough sed '/^Cola$/d' somefile.txt > newfile.txt. – ojs Nov 29 '16 at 19:00
  • 1
    It's a bad kind of example. Never do that. – Satō Katsura Nov 29 '16 at 19:09
  • Or use --in-place if it should just edit the same file: sed -i '/^Cola$/d' file. The example with fgrep does not delete lines, it filter out the match. – hschou Nov 29 '16 at 20:57
  • @hschou than you very much – Smeterlink Nov 30 '16 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.