71

I have a file r. I want to replace the words File and MINvac.pdb in it with nothing. The commands I used are

sed -i 's/File//g' /home/kanika/standard_minimizer_prosee/r

and

sed -i 's/MINvac.pdb//g' /home/kanika/standard_minimizer_prosee/r

I want to combine both sed commands into one, but I don't know the way. Can anyone help?

The file looks like this:

-6174.27    File10MINvac.pdb
-514.451    File11MINvac.pdb
4065.68     File12MINvac.pdb
-4708.64    File13MINvac.pdb
6674.54     File14MINvac.pdb
8563.58     File15MINvac.pdb
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
kanika
  • 895
  • 1
  • 6
  • 11
  • For more complex cases, perhaps see https://stackoverflow.com/questions/74046304/multiple-sed-e-commands – tripleee Oct 12 '22 at 19:04

2 Answers2

103

sed is a scripting language. You separate commands with semicolon or newline. Many sed dialects also allow you to pass each command as a separate -e option argument.

sed -i 's/File//g;s/MINvac\.pdb//g' /home/kanika/standard_minimizer_prosee/r

I also added a backslash to properly quote the literal dot before pdb, but in this limited context that is probably unimportant.

For completeness, here is the newline variant. Many newcomers are baffled that the shell allows literal newlines in quoted strings, but it can be convenient.

sed -i 's/File//g
    s/MINvac\.pdb//g' /home/kanika/standard_minimizer_prosee/r

Of course, in this limited case, you could also combine everything into one regex:

sed -i 's/\(File\|MINvac\.pdb\)//g' /home/kanika/standard_minimizer_prosee/r

(Some sed dialects will want this without backslashes, and/or offer an option to use extended regular expressions, where they should be omitted. BSD sed, and thus also MacOS sed, demands a mandatory argument to sed -i which can however be empty, like sed -i ''.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks for the tip about using multiple -e flags, one per statement in the script. Having a separate -e flag for the 'i' command is a nice way to make sure the 'i' command gets terminated when writing a oneliner sed script. – IllvilJa Oct 01 '13 at 13:43
  • Tangentially see also https://stackoverflow.com/questions/35253663/linux-find-replace-on-a-folder-of-files-using-a-list-of-items-for-replacement which extends this to handling a list of substitution pairs. – tripleee Jan 06 '19 at 09:47
50

Use the -e flag:

sed -i -e 's/File//g' -e 's/MINvac.pdb//g' /home/kanika/standard_minimizer_prosee/r

Once you get more commands than are convenient to define with -es, it is better to store the commands in a separate file and include it with the -f flag.

In this case, you'd make a file containing:

s/File//g
s/MINvac.pdb//g

Let's call that file 'sedcommands'. You'd then use it with sed like this:

sed -i -f sedcommands /home/kanika/standard_minimizer_prosee/r

With only two commands, it's probably not worthwhile using a separate file of commands, but it is quite convenient if you have a lot of transformations to make.

Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
  • whatis the idead behind -f flag? – kanika Oct 05 '11 at 07:06
  • 5
    He certainly tried to explain it. With `-f` you can put your script in a file instead of on the `sed` command line. (In this case, it is probably not very useful, though.) – tripleee Oct 05 '11 at 07:41