I want to delete a line which matches the word name but doesn't delete the lines have something following it. I can't seem to find an answer to this. I know sed -i '/name/d' * would delete the whole line matching the word name but this is not what I want. An example will clarify my question. If input is -

 name xav
 name lmn
 name abc

Then the output should be -

 name xav
 name lmn
 name abc

basically the line with name and nothing else should be deleted. The rest need to stay.

1 Answer 1


Anchor the text in the regex

sed -i '/^name$/d'
  • This is correct, but can you make a slight modification and tell me how to integrate it if there is a single space ` ` before name? Thanks!
    – Zzrot
    May 3, 2016 at 21:28
  • Before name? You can have spaces in the regexp, and also a ? after to make it optional. May 3, 2016 at 21:56
  • @Henrik Can you write the command please? Just before name there's a blank space
    – Zzrot
    May 3, 2016 at 22:22
  • 1
    sed -i '/^[[:blank:]]*name[[:blank:]]*$/d'. That will delete lines containing only name and any number (zero-or-more) of leading or trailing spaces or tabs.
    – cas
    May 4, 2016 at 0:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .