26

I know how to edit a file using sed one step at a time, but how would I create an actual sed script and then use it on the file to do the same things that the individual sed commands would do? For example these 3 sed commands:

sed -i '4i\ ' baconFile

sed -i 's/,/\t\t/' baconFile

sed -i 's/,/ /' baconFile

John
  • 3,459
  • 13
  • 29
  • 25

4 Answers4

32

Putting commands in a file

Put these lines in a script file named script.sed.

4i\ 
s/,/\t\t/
s/,/ /

Then run it like this:

$ sed -i -f script.sed baconFile

Making a standalone sed script

if you want it to be a single executable then do this:

#/bin/bash

sed -i '
4i\ 
s/,/\t\t/
s/,/ /
' "$@"

Put the above lines into a file called script.bash, make it executable (chmod +x script.bash, and run it like this:

$ script.bash baconFile

Creating the script file

You can use this method to make the file I mentioned above.

$ cat > script.bash <<EOF
#!/bin/bash

4i\ 
s/,/\t\t/
s/,/ /
EOF

This will make the script file. You can confirm using this command:

$ cat script.bash
#!/bin/bash

4i\ 
s/,/\t\t/
s/,/ /
slm
  • 363,520
  • 117
  • 767
  • 871
  • Sorry, still a bit confused as to how to actually create a sed file, say sed1, then put the commands inside of it, then use it on a file, say baconFile. Sorry. – John Oct 14 '13 at 04:43
  • 3
    It is possible to use the shebang line `#!/usr/bin/sed -f -` to write a sed script as a callable script. EDIT: Actually that seems to depend on the implementation of sed. GNU sed does not appear to support `-f -`, and that is what most people are probably using these days. Actually, BSD sed doesn't support this either, but AIX sed does. Go figure! – Greg Hewgill Oct 14 '13 at 04:44
  • @GregHewgill - that's what I thought, I took that last statement out, thanks! – slm Oct 14 '13 at 04:46
  • @John - see my example above, I made a file called script.bash, put the contents as I mentioned into it, make it executable, and run it like this, `script.bash baconFile`. – slm Oct 14 '13 at 04:49
  • I am still a bit confused as to how the sed1 script is made. What is this doing? #/bin/bash Also I have tried using these commands: #/bin/bash sed -i ' 4i\ s/,/\t\t/ s/,/ / ' "$@" but I get error message: sed: no input files – John Oct 14 '13 at 15:06
  • @John - that is called a shebang line. That makes this file a script, and tells the shell what interpreter to use, in this case we're using Bash. – slm Oct 14 '13 at 15:10
  • Just to clarify, I do know how to use a created sed script file on a file, I just do not know exactly how to create the sed script and put the commands in it =/ Sorry guys. – John Oct 14 '13 at 15:56
  • @John - Sorry I thought you might be asking that but didn't want to offend you. You can use a editor such as `nano` to create this file. Also I updated my answer showing how you can make the file using the `cat` command. – slm Oct 14 '13 at 16:07
  • Alright thanks! How would I made a sed script called sed1 though? I see yours is called script.bash. Is the .bash extension required? – John Oct 14 '13 at 16:20
  • @John - the extensions in Linux are irrelevant, they're just so that operators of the computer know what the file is 8-). To make it `sed1`, change this line like so: `cat > sed1 < – slm Oct 14 '13 at 16:32
6
sed -i '4i\ 

s/,/\t\t/

s/,/ /' baconFile

sed works like most interpreters, multiple commands can be on separate lines or delimited by ;. if you don't have GNU sed your first line may cause an error.


To create a sed script, you just give the absolute path to your sed interpreter in your shebang. and make sure the file is executable.

#!/bin/sed -f
s/foo/bar/

chmod u+x foo
./foo <<<foo
bar

or the script could be called along with the sed command. sed -i -f foo file

llua
  • 6,760
  • 24
  • 30
  • I meant how would I make a script of the commands so I would not have to type out all of the commands every time I wanted to do it to a file? – John Oct 14 '13 at 03:55
  • 5
    +1 For using the right shebang... (Why would you start bash just to start sed???) – fstamour Jun 28 '15 at 05:28
4

You need the -f option sed -f sed-script

loki
  • 215
  • 2
  • 9
3

You can write a bash or korn shell script to do this. If you want to make it dynamic, you can make it so it takes in a file name as a parameter and you can run these sed commands on any file you want.

Create a new file, say updatefile.sh

#!/bin/bash
#$1 is the first parameter being passed when calling the script. The variable filename will be used to refer to this.
filename=$1

sed '4i\ ' filename
sed 's/,/\t\t/' filename
sed 's/,/ /' filename

You can then run updatefile.sh baconFile which will run all three sed commands for the file baconFile.

Also, the g option in sed will update all occurrences of the variable in the file. For example:

sed 's/,/ /g' filename
Thomas Nyman
  • 29,912
  • 10
  • 65
  • 77
mousumis
  • 131
  • 1