Remove exact matching word using sed

sed
28,944

In the above string, you don't need start-of-word/end-of-word markers and you may use:

echo 'foggy light' | sed 's/foggy//g'

For the additional question in the comment:

Indeeed, my sed version

sed --version
sed (GNU sed) 4.2.2

supports the Syntax with \<...\>

echo 'foggy foggylight' | sed 's/\<foggy\>//g'
foggylight

If it doesn't work for you, report your sed version and read its manpage. For my sed, this syntax works too:

echo 'foggy foggylight' | sed 's/\bfoggy\b//g'
foggylight

\b can be memorized as boundary.

Share:
28,944

Related videos on Youtube

Josef Klimuk
Author by

Josef Klimuk

Updated on September 18, 2022

Comments

  • Josef Klimuk
    Josef Klimuk over 1 year

    I want to remove word 'foggy' from the string. It fails. Why?

    echo 'foggy light' | sed 's/\<foggy\>//g'
    
    • muru
      muru about 6 years
      What version of Ubuntu? What's the output of sed --version?
    • muru
      muru about 6 years
      @userunknown of course: GNU sed, installed by default on all versions of Ubuntu, has \< and \> for matching start-of-word and end-of-word. I suspect this is yet another Android question. OP has been know to post a number of questions about some Android app that provides Unix commands, while pretending it is Ubuntu OP is using.
    • datacarl
      datacarl about 6 years
      For removing the word foggy in above scenario, you don't need start-of-word/end-of-word-markers.
    • Josef Klimuk
      Josef Klimuk about 6 years
      sed version 4.0
    • muru
      muru about 6 years
      Which it is not on any current version of Ubuntu: packages.ubuntu.com/search?keywords=sed, so yet again an off-topic question.
  • muru
    muru about 6 years
    It's not < and > but \< and\>. See gnu.org/software/sed/manual/sed.html#regexp-extensions
  • Josef Klimuk
    Josef Klimuk about 6 years
    Ok. And how to drop just the word foggy alone, not touching foggylight? echo 'foggy foggylight' | sed 's/foggy//g'
  • datacarl
    datacarl about 6 years
    @JosefKlimuk: Added extension