Melvin Melvin - 4 months ago 19
Bash Question

Execute shell command line by line from a file

I have a .txt file which has sed replace commands on each line and has 1000+ entries. The file is on my server.
Can any one please help me how to run the commands in file line by line and execute till the end of file.

My txt file looks like

sed -i 's|http://www.myoldsite/url/category/another/blah|http://www.mynewsite.com|g' ./db.sql
sed -i 's|http://www.myoldsite/url/category/blah/blah|http://www.mynewsite.com|g' ./db.sql
sed -i 's|http://www.myoldsite/url/category/blah|http://www.mynewsite.com|g' ./db.sql

Answer

You can convert your file to a list of substitution commands by removing all occurrences of sed -i ' and ' ./db.sql.

Using process substitution, the list can then be processed as a file passed to the sed -f option.

sed -i -f <(sed "s/[^']*'//;s/'.*//" file) ./db.sql
Comments