I have a file containing a list of regular expressions and replacement literal strings in the following format :
OLD_REGEXP_1 NEW_STRING_1
OLD_REGEXP_2 NEW_STRING_2
...
I want to replace all of the strings that match OLD_REGEXP_X
with NEW_STRING_X
in multiple files *.txt
.
I believe that this is a common question and someone should have already done something similar before, but I just couldn't find an existing solution written in bash.
For example :
Tom Thompson
Billy Bill&Ted
goog1e\.com google.com
https?://www\.google\.com https://google.com
Input :
Tom and Billy are visiting http://www.goog1e.com
Expected output :
Thompson and Bill&Ted are visiting https://google.com
The major challenges are :
/
which is often used as a regexp delimiter by some tools, must be treated as literal.&
and \1
that are often used as backreference metacharacters in replacement strings but must be literal in this case.You can convert your substitution list file into a sed
script file, then let sed
do the job for you.
give this a try with gnu sed:
sed -i -f <(sed -r 's/^(\S*) (.*)/s@\1@\2@/g' listfile) *.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With