acub acub - 1 year ago 83
Bash Question

Match word ignoring same word with extra character in sed

The following works in Javascript as a match:


However, in sed the regex does not match anything. I would like to replace a variable reference in a bash file while ignoring the variable identifier. The premise is that I have a default placeholder file that needs to be updated from a shell script with sed -i. I can't locate a reason as to why sed is having an issue with this expression.

Sed test example:

echo -e 'fileref=old\n./executable $fileref' | sed 's/[^\$]fileref.*/fileref=replaced/g'

Output from gnu sed (ubuntu or centOS) where no match is found:

fileref=old <br>
./executable $fileref

Desired output:

fileref=replaced <br>
./executable $fileref

Answer Source
echo -e 'fileref=old\n./executable $fileref' | sed 's/^fileref.*$/fileref=replaced/g'

gives the following

./executable $fileref

by putting in [^\$] you are saying to search for $ at the start of a line

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download