-1

As per sed whole word search and replace I have tried the following.

OS: HP-UX B.11.31 U ia64

$ echo "bar bar2 embarassment" | sed "s/\<bar\>/test/g"
bar bar2 embarassment

$ echo "bar bar2 embarassment" | sed "s/\b[bar]\b/test/g"
bar bar2 embarassment

$ echo "bar bar2 embarassment" | sed "s/\[[:<:]]bar[[:<:]]\/test/g"
sed: Function s/\[[:<:]]bar[[:<:]]\/test/g cannot be parsed.

$ echo "bar bar2 embarassment" | sed "s/bar/test/g"
test test2 embraassment

None of the above are helping me to match the exact values.

Note: I can't install GNU sed since I don't have permission to do so.

Community
  • 1
  • 1
Chamara Keragala
  • 5,627
  • 10
  • 40
  • 58
  • This one `sed -E 's/([ ]|^)bar([ ]|$)/\1test\2/g'` – perreal Jan 27 '16 at 07:35
  • @perreal yes... that does work.. please put it answer :) thanks – Chamara Keragala Jan 27 '16 at 07:39
  • @perreal oops.. wait it gives me an error... `Illegal Option -- E` – Chamara Keragala Jan 27 '16 at 07:40
  • does it work if you remove `E`? – perreal Jan 27 '16 at 07:41
  • @perreal I get this `sed: Function s/([ ]|^)bar([ ]|$)/\1test\2/g cannot be parsed.` when i remove E – Chamara Keragala Jan 27 '16 at 07:43
  • do you have `gsed` on that system? – perreal Jan 27 '16 at 07:50
  • What version of sed do you have ? – 123 Jan 27 '16 at 08:25
  • 1
    The version of `sed` on HP-UX is rather limited; when GCC is installed, the configuration process detects and rejects the HP-UX `sed` as inadequate. When you say "you can't install GNU `sed`", does that mean you have no C compiler (and build system) on the machine, or just that you can't install it in the system directories? If you have the compiler and `make`, you can configure `sed` to be installed under `$HOME/gnu` (the binary would be `$HOME/gnu/bin/sed`) with `--prefix=$HOME/gnu`, and you're surely allowed to create files under your home directory. – Jonathan Leffler Jan 27 '16 at 15:43
  • @ChamaraKeragala, your third attempt should work if you remove the backslashes. `[[:<:]]` is a character class, and no part of it should be "escaped". `echo "bar bar2 embarassment" | sed 's/[[:<:]]bar[[:<:]]/test/g' ` works for me in an ancient sed (albeit not HP/UX). – ghoti Jan 27 '16 at 16:48

1 Answers1

1

Most portable method :)

sed 's/^bar$/test/;s/ bar / test /g;s/^bar /test /;s/ bar$/ test/'

You can also allow all non-alphanum characters:

sed 's/([^a-zA-Z0-9])bar([^a-zA-Z0-9])/\1test\2/g;s/^bar([^a-zA-Z0-9])/test\1/'
perreal
  • 94,503
  • 21
  • 155
  • 181