sed (stream editor) is an editor used, not interactively on text files (like vi or emacs), but on streams. This allows it to transform text input from a pipe or the command line or a file (if it is piped to sed or given as an argument to sed).
With sed, you can quickly replace strings in a stream:
$ cat test hello world $ cat test | sed "s/world/moon/g" hello moon
Here the sed command is
"s" stands for "substitute". world is the string that shall be replaced, moon is the string by which it shall be replaced. The "g" suffix causes sed to make more than one replacement per input line as is usually intended.
To replace a text in an existing file, use the -i argument:
$ cat test hello world $ sed -i "s/world/moon/g" test $ cat test hello moon
In this example, the file test is modified to contain moon whereever it contained world before.
$ sed 's/<.*>//g' foobar.html
An html tag is any string enclosed in brackets (defined as a regex: <.*>). This is replaced by nothing (what is between the two slashes //).
You regex can also contain a backreference. For example, the following command will transform strings from inputfile.txt to outputfile.txt.
foo something will be replaced by
bar 54321 something will be replaced by
$ sed -e 's/foo \([^ ]*\)/foo("\1")/g' -e 's/bar \([0-9]*\) \([^ ]*\)/bar(\1, "\2")/' < inputfile.txt > outputfile.txt
Examining the above command in more detail: the -e flags specify a new expression, allowing more than one replacement rule in the same command. The escaped-parentheses \( and \) tell sed to remember the string that was enclosed. The \1 and \2 commands tell sed to insert a remembered string.
If you want to use a lot of commands you can save them as a sed_script.txt:
# this is a sed script # usage: $ sed -f sed_script.txt textfile.txt > new_textfile.txt # start replacing all things that must be replaced # replace / with _ s/\//_/g
Running the script on textfile.txt:
$ sed -f sed_script.txt textfile.txt > new_textfile.txt
#!/usr/bin/sed -f # save your commands in this script.sh; chmod u+x script.sh # call it like so: $ ./script.sh textfile.txt > new_textfile.txt # start replacing all things that must be replaced # replace / with _ s/\//_/g
To execute the script on your textfile.txt:
$ chmod u+x script.sh $ ./script.sh textfile.txt > new_textfile.txt
- sed man page
- GNU sed online manual
- Sed, the Stream Editor by example
- An introduction to sed, with an example for sed -f and executable scripts