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


Putting commands in a file

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

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:


sed -i '
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

s/,/ /

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

$ cat script.bash

s/,/ /
  • 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 4: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 4:44
  • @GregHewgill - that's what I thought, I took that last statement out, thanks! – slm Oct 14 '13 at 4: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 4: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
sed -i '4i\ 


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

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

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

  • 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 3:55
  • 1
    +1 For using the right shebang... (Why would you start bash just to start sed???) – fstamour Jun 28 '15 at 5:28

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


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

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.