1

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
 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.

4

Anchor the text in the regex

sed -i '/^name$/d'
| improve this answer | |
  • 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 '16 at 21:28
  • Before name? You can have spaces in the regexp, and also a ? after to make it optional. – Henrik supports the community May 3 '16 at 21:56
  • @Henrik Can you write the command please? Just before name there's a blank space – Zzrot May 3 '16 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 '16 at 0:22

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.