I am trying to delete a line from below file


using sed '/^localhost\b/d' file

I don't understand why only the two lines localhostr.com and localhost23 are printed. I want only the localhost line to be deleted...

  • 1
    Dash is not a word character. – Kusalananda Jun 25 '18 at 13:12
  • yes, I understand. I only want the first line to be deleted ignoring the "-" or "." or any further character in the boundary – ArjunCh Jun 25 '18 at 13:15
  • @steeldriver that comment of yours is the answer. – ctrl-alt-delor Jun 25 '18 at 13:15
  • @ArjunCh please consider accepting ikkachu's answer below – steeldriver Jun 25 '18 at 14:43

\b matches a word boundary in GNU sed, i.e. the point between a "word character" and a non-word character. Letters, digits and underscores are the former, dots and dashes (among others) the latter, so there's a word boundary at the end of localhost at end of line, as well before the dots and dashes. But not between t and 2, or t and r.

If you want to remove the line with the word localhost only, just use sed -e '/^localhost$/d', or even grep -vFx 'localhost' (-v for inverted match, -F for fixed string match, -x for full line match).

Or, if you may have trailing whitespace: sed -e '/^localhost[[:blank:]]*$/d'

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.