4

How to replace whole words in string sed whole word search and replace

with sed in HP-UX (B.11.31)?

$ echo "bar embarassment" | sed "s/bar/no bar/g";

"\bbar\b" does not work
"\<bar\>" does not work
"[[:<:]]bar[[:>:]]" does not work
Community
  • 1
  • 1
Toru
  • 41
  • 2
  • Install and use GNU `sed`? Use `perl` instead? Neither suggestion is wholly facetious. Historically, building GCC on HP-UX required a non-standard version of `sed` (effectively, GNU `sed`) because the one provided by HP had constraint limits that the GCC configuration script could not reasonably live within. I haven't built GCC on an HP-UX 11.x system (10.x was the last time), so I don't know if that is still a problem. – Jonathan Leffler Aug 19 '11 at 14:09

3 Answers3

1

Try the -r option of sed with \b:

echo "bar embarassment" | sed -r "s/\bbar\b/no bar/g";
codaddict
  • 445,704
  • 82
  • 492
  • 529
1

Tested on solaris, not hpux

echo "bar embarassment bar foobar bar" | 
sed -e 's/\([^[:graph:]]\)bar$/\1no bar/g'  \
    -e 's/\([^[:graph:]]\)bar\([^[:graph:]]\)/\1no bar\2/g' \
    -e 's/^bar\([^[:graph:]]\)/no bar\1/g'

emits

no bar embarassment no bar foobar no bar
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

if you are in vim, you can use this command:

:% s/\<old\>/new/g
arganzheng
  • 1,294
  • 15
  • 20