Q:

Replace string in a database file with an input file list - Terminal

I'd like to replace some file extension in an SQL file when I match strings from an input file using terminal.
I have an input.txt containing a list of file paths.
/2014/02/haru-sushi_copertina_apdesign-300x300.png 
/2014/02/haru-sushi_copertina_apdesign.png 
/2014/02/harusushi_01_apdesign-300x208.png
ect ect

Then I have a WordPress.sql file
What I'd like to do, whenever I find a match between the 2 files, is to replace the extension from .png to .jpg in the database file of that matching.
I hope I've made myself clear.
Should I use sed with regular expressions? Something like
cat input.txt | while read -r a; do sed -i 's/$a/.jpg/g' wordpress.sql; done 

Any suggestions? Even for the RegEx.

A:

sed is for simple substitutions on individual lines, that is all, and you should never write a shell loop just to manipulate text, see http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice.
Try this (uses GNU awk which I assume you have since you were using GNU sed):
awk -i inplace 'NR==FNR { paths[$0]; next }
{
    for (path in paths) {
        gsub(path,gensub(/png$/,"jpg",1,path))
    }
    print
}
' input.txt wordpress.sql

It has some caveats related to partial matching but no worse than if you were trying to use sed and easily fixable if there's a problem (unlike with sed).

