Search code examples
bashsedsunos

sunOS's sed in bash script - replace pattern: nothing changes


ANSWER:

as mlv and sorontar mentioned, my SED version is BRE and doesn't support | (pipe). so in my case is possible use something like:

sed "s/\( [namevlu]*=\"\)BASE\.$str1/\1BASE\.$str2/g"

which match name=" and value=" but not other=" and values=". regex ( [namevlu]) contains only characters what i need. ok, it is not as save, as can be, but i don't expect existence of something like valuenm=" or so. if someone needs exactly specified regex, it needs make two (or more) seds.

ORIGINAL QUESTION:

I need to replace one string with another, but for sure i need check specific context.

for example:

blah val1="BASE.OLD_TEXT_OR_SO" blabla
blah val2="BASE.OLD_SOMETHING" ...

i want change to

blah val1="BASE.NEW_TEXT_OR_SO" blabla
blah val2="BASE.NEW_SOMETHING" ...

this script doesn't change anything:

#!/bin/bash

...

str1="OLD_"
str2="NEW_"

sed "s/\(name=\"|value=\"\)BASE\.$str1/\1BASE\.$str2/g" input.file > output.file

but later similiar sed works ok:

sed "s/\(<Tag>\)[A-Z0-9\-\._|]*\(<\/Tag>\)/\1$otherStr\2/g" input.file > output.file

output file has still BASE.OLD_ :/

also when i try it on console, i get same (none) result. i think there is something wrong in "looking for" pattern, but i havent idea what.

$ str0='blah name="BASE.OLD_TEXT_OR_SO" blabla 
> blah value="BASE.OLD_SOMETHING" ...
> blah other="BASE.OLD_SOMETHING" ...
> blah values="BASE.OLD_SOMETHING" ...'
$ echo $str0  | sed "s/\(val1=\"|val2=\"\)BASE\.$str1/\1BASE\.$str2/g"

regex was tested on online tester where it works fine.

(name="|value=")BASE\.OLD_
\1BASE\.NEW_

system:
SunOS 5.11
GNU bash 4.1.11(1)-release
Sed 4.2.1

Thanks in advance.


Solution

  • as mlv and sorontar mentioned, my SED version is BRE and doesn't support | (pipe). so in my case is possible use something like:

    sed "s/\( [namevlu]*=\"\)BASE\.$str1/\1BASE\.$str2/g"
    

    which match name=" and value=" but not other=" and values=". regex ( [namevlu]) contains only characters what i need. ok, it is not as save, as can be, but i don't expect existence of something like valuenm=" or so. if someone needs exactly specified regex, it needs make two (or more) seds.