0

I'm reading this regex tutorial and it says:

The pattern to search for the word "the" would be "\<[tT]he>".

How come this doesn't output bar bar?

○ → echo "foo bar" | sed -E 's/\<foo\>/bar/'
foo bar

Can \< and \> be used in sed?

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

1 Answers1

1

\< and \> are used for word boundaries but it is platform dependent.

On OSX sed you need to use [[:<:]] and [[:>:]] for the same:

echo "foo food bar" | sed -E 's/[[:<:]]foo[[:>:]]/bar/'

bar food bar

However if you can install gnu sed on OSX using brew package installer then you can use:

echo "foo food bar" | gsed -E 's/\bfoo\b/bar/'

bar food bar
anubhava
  • 761,203
  • 64
  • 569
  • 643