acub acub - 4 months ago 17
Bash Question

Match word ignoring same word with extra character in sed

The following works in Javascript as a match:

[^\$]fileref.*


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
echo -e 'fileref=old\n./executable $fileref' | sed 's/^fileref.*$/fileref=replaced/g'

gives the following

fileref=replaced
./executable $fileref

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