-3

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 
Jens
  • 69,818
  • 15
  • 125
  • 179
Melvin
  • 383
  • 1
  • 13
  • 40
  • Add execute permission to the file, `chmod +x file`, add a `bash` interpreter `#!/bin/bash` (optional but recommended) and run as `./file` – Inian Jul 21 '16 at 10:08
  • sorry can you make it more understandable – Melvin Jul 21 '16 at 10:12
  • @Inian Make that `#!/bin/sh` - you don't know if there is a bash on the OP's system. Not all the world's a Linux system. – Jens Jul 21 '16 at 10:12
  • what about `sh my-txt-file` ? – JJoao Jul 21 '16 at 10:14
  • Possible duplicate of [Multiple sed search/replace command for urls](http://stackoverflow.com/questions/38480920/multiple-sed-search-replace-command-for-urls) – shellter Jul 21 '16 at 16:17
  • Possible duplicate of [combining 2 sed commands](http://stackoverflow.com/questions/7657647/combining-2-sed-commands) – tripleee Jul 22 '16 at 10:00

3 Answers3

1

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
SLePort
  • 15,211
  • 3
  • 34
  • 44
-1

Maybe this one helps:

for i in $(cat yourfile.txt)
do
    sudo $i
done

EDIT:

Why downvote?

There are multiple solutions to your problem.

SOLUTION 1

Running each command in your .txt file:

Make your .txt file executable by giving command:

chmod +x yourfile.txt

And then executing it by giving a command:

./yourfile.txt

SOLUTION 2

Creating a script

#!/bin/sh
FILE=$1
while read line; do
     $line
done < $FILE

making the script executable:

chmod +x yourscriptfile.sh

And then executing your script by providing your .txt file as argument:

./yourscriptfile.sh yourfilewithcommands.txt

Hope this helps.

Community
  • 1
  • 1
lkallas
  • 1,310
  • 5
  • 22
  • 36
  • just run this on shell window ? – Melvin Jul 21 '16 at 10:16
  • No this is the loop that runs each command in your .txt file. I Assume that you don't want to edit your .txt file in any way. To achieve what you're looking for you should create a script that contains this loop. And then execute this script. If you need more explanations just let me know :) – lkallas Jul 21 '16 at 10:20
  • How to create script – Melvin Jul 21 '16 at 10:45
  • solution 1 gives no such file or directory :( – Melvin Jul 21 '16 at 11:11
  • are you in the same directory as your .txt file? – lkallas Jul 21 '16 at 11:13
  • changed the txt file to unix format and ran the command,Now we got this, first line in txt "sed -i `s|http://www.myoldsite/url/category/blah?pagetype=brand&prefn1=brand&prefv1=adidas|http:///www.mynewsite.com|g` ./db.sql" we got this error : error ./links.txt: line 1: s: command not found ./links.txt: line 1: amp: command not found ./links.txt: line 1: http://www.myoldsite/url/category/blah?pagetype=brand: No such file or directory ./links.txt: line 1: amp: command not found ./links.txt: line 1: http:///www.mynewsite.com: No such file or directory – Melvin Jul 21 '16 at 11:35
  • Do your sed commands work correctly if you run them in terminal separately? In my examples I used ln -lah and echo commands for testing purposes not sed command. – lkallas Jul 21 '16 at 12:00
  • @Melvin I tried solution 1 and with exact same sed commands and for me it worked OK. What OS do you have? I'm running Debian GNU/LInux 8 (jessie) Command for checking your OS: cat /etc/*-release – lkallas Jul 21 '16 at 12:43
  • Debian 14.04 ubuntu – Melvin Jul 21 '16 at 12:52
  • @Melvin I tried different EOL conversions in my .txt files and this is what i found out: Windows format caused "no such file or directory" Unix/OSX format: EVERYTHING OK Old Mac format: got same error as you. So your .txt file should have correct EOL format. I am using notepad++ for correct formatting: Edit -> EOL Conversions. – lkallas Jul 21 '16 at 12:53
-1

You can turn the file into a script by adding

#!/bin/sh

as the first line, than making the file executable with

chomd u+x file

and then just run it with

./file

BUT, this will be very slow. It's much faster to run sed only once with all the expressions, i.e. changing it into

sed -i~ 's|http://www.myoldsite/url/category/another/blah|http://www.mynewsite.com|g;
         s|http://www.myoldsite/url/category/blah/blah|http://www.mynewsite.com|g;
         s|http://www.myoldsite/url/category/blah|http://www.mynewsite.com|g
' ./db.sql 
choroba
  • 231,213
  • 25
  • 204
  • 289