Say we have some arbitrary literals in a file that we need to replace with some other literal.
Normally, we'd just reach for sed(1) or awk(1) and code something like:
sed "s/$target/$replacement/g" file.txt
But what if the $target and/or $replacement could contain characters that are sensitive to sed(1) such as regular expressions. You could escape them but suppose you don't know what they are - they are arbitrary, ok? You'd need to code up something to escape all possible sensitive characters - including the '/' separator. eg
t=$( echo "$target" | sed 's/./\./g; s/*/\*/g; s/[/\[/g; ...' ) # arghhh!
That's pretty awkward for such a simple problem.
perl(1) has Q ... E quotes but even that can't cope with the '/' separator in $target
.
perl -pe "s/Q$targetE/$replacement/g" file.txt
I just posted an answer!! So my real question is, "is there a better way to do literal replacements in sed/awk/perl?"
If not, I'll leave this here in case it comes in useful.
See Question&Answers more detail:os