sedsed: Powerful UNIX filter that parses and transforms text using a simple, compact programming language.
sedHistory:
- Developed from 1973 — 1974 by Lee E. McMahon of Bell Labs.
General Idea:
sed script are applied in order to each line.sed is a filter (the input file is unchanged).stdout.Flow of Control:
sed starts at the first line of the input file and runs it through the script file.sed reads the next line of the input and restarts from the beginning of the script file.When sed Copies a Line into Pattern Space…:
sed ScriptsFormat:
[address[, address]][!]command [arguments]
A script is a file of commands
An address can be either a line number or a pattern, enclosed in slashes (/pattern/)
$)Most command accept two addresses:
1,10 applies through lines 1—10 inclusively.The ! operator can be used to negate an address.
address!command applies command to every line that doesn’t match address.Commands are written as single letters.
Examples: Using the
delete command$ sed 'd' < file.txt
- Delete all lines (print nothing)
$ sed '6d' < file.txt
- Delete line 6 (print everything except line 6).
{})Braces ({}) can be used to apply multiple commands to an address.
Format:
[/pattern/[,/pattern/]] { command1 command2 ... }
Syntax Notes:
- Opening brace (
{) must be the last character on a line- Closing brace (
}) must be on a line by itself- Spaces cannot follow the braces
Tip: Commands can be separated from the
[address]by spaces for readability.
- e.g.,
sed '1,3p'andsed '1,3 p'do the same thing
d: deleteFormat:
[address1][,address2]d
stdout.sed moves onto the next line and begins applying commands from the top of the script again.delete$ sed '1,10d' < file.txtfile.txt without the first ten lines).p: printFormat:
[address1][,address2]p
stdout.sed is run with -n to suppress automatic printing of pattern space.)print$ sed -n '1,10 p' < file.txts: substituteFormat:
[address(es)]s/pattern/replacement/[flags]
/…/…/: Delimiters.pattern: Search pattern.replacement: Replacement string forpattern.- optional
flags:
n: Number between 1—512 indicating which occurrence of pattern should be replaced.g: Global, replace all occurrences ofpatternin the pattern space.
- If
nandgaren’t set, thensedwill only match the first occurrence ofpatternper line (behaves like-n1).p: Print contents of pattern space.
Note on
substitution delimiters: The/delimiter can be escaped with\if you want to use it literally in a pattern (e.g.,sed 's/\/usr\/local\/bin/\/common\/bin/’ < file.txt),
- Or, you could use any other character as a delimiter, it just has to show up thrice and be escaped if you want to use it literally.
- e.g.,
sed 's|github.com|codeberg.org|g' README.md
substitute$ echo "I love Linux" | sed 's|Linux|GNU/Linux|g'
I love GNU/LinuxLinux” with “GNU/Linux” on every line.a: appendFormat:
[address]a\ text
Note: append doesn’t accept address ranges.
append$ echo "Hello" | sed '/Hello/ a\World'
Hello
WorldWorld” after every line containing “Hello”i: insertFormat:
[address]i\ text
Note: insert doesn’t accept address ranges.
insert$ echo "World" | sed '/World/ i\Hello'
Hello
WorldHello” before every line containing “World”c: changeFormat:
[address(es)]c\ text
change$ echo "REPLACEME" | sed '/REPLACEME/ c\bingus'
bingusy: transformFormat:
[address[,address]] y/<string1>/<string2>/
<string1> and <string2>: Any character that matches a character in string1 is translated into the corresponding character in string2.Operates like tr. Does character-to-character translation.
y)$ sed 'y/abc/xyz/'a with x, b with y, and c with ztr 'abc' 'xyz'q: quitFormat:
[address]q
sed. No more input lines are read, nothing goes to stdout.Note: quit takes at most one address.
quitsed '100 q' file.txtfile.txt to stdout.p, d, and !Examples: Using the
delete command (more advanced)$ sed '11,$ d' < file.txt
- Delete all lines between line 11 and last line (print the first 10 lines of a file).
$ sed '1,/^$/ d' < file.txt
- Delete all lines between line 1 and the first blank line (
^$) (print a mail message with the header chopped off).
- Tip:
^$means “start of line immediately followed by end of line”, aka an empty line.$ sed '/^$/' < file.txt
- Delete empty lines (print non-empty lines).
$ sed '/^$/,$d' < file.txt
- Deletes from the first blank line through the last line of the file
$ sed '/^$/,10d' < file.txt
- Deletes from the first blank line through line 10
$ sed '/^ya*y/,/[0-9]$/d' < file.txt
- Deletes from the first line that begins with
yay,yaay,yaaay, etc. through the first line that ends with a digit.
Examples: Using the
print command$ sed 'p' < file.txt
- Print every line in
file.txttwice.$ sed -n 'p' < file.txt
- Print every line in
file.txtonce.
- Remember:
-nsuppresses automatic printing of pattern space!$ sed '/^$' < file.txt
- Print every line in
file.txt, printing double of every empty line.$ sed -n '1,10 p' < file.txt
- Print lines 1 through 10.
$ sed -n '/^$/,$ p' < file.txt
- Print lines from the first blank line to the last line of
file.txt
Examples: Reversing restrictions with
!$ sed -n '/match/ p' < file.txt
- Acts like
grep$ sed -n '/match/ !p' < file.txt
- Acts like
grep -v$ sed -n '1,10 p’ < file.txt $ sed -n '11,$ !p’ < file.txt $ sed '1,10 !d’ < file.txt $ sed '11,$ d' < file.txt
- All four of these commands print the exact same thing!
- This is to demonstrate how
print anddelete are opposite functions.
substitutionExamples: Using
substitution$ sed 's/one/ONE/g' < file.txt
- Replace every occurrence of “
one” with “ONE”.$ sed 's/false/true/g' < file.txt
- Replace every occurrence of “
false” with “true”.
substitution
(important!)Suppose a file named file.txt with this content:
one two three, one two three four
three two one
one hundredIf you ran sed 's/one/ONE/' < file.txt, the output would be:
$ sed 's/one/ONE/' < file.txt
ONE two three, one two three four
three two ONE
ONE hundredone” gets affected on each line (if you want to apply substitution to all occurrences use the global flag)If you ran sed 's/one/ONE/2' < file.txt, the output would be:
$ sed 's/one/ONE/2' < file.txt
one two three, ONE two three four
three two one
one hundredExamples: Using
substitution (cont.)$ sed ‘s/nice/bazinga/’ < file.txt
- Replace first occurrence of “
nice” with “bazinga” on every line.$ sed ‘s/Tom/Jerry/2’ < file.txt
- Replace second occurrence of “
Tom” with “Jerry” on every line.$ sed -n ‘s/one/ONE/2 p’ < file.txt
- Replace second occurrence of “
one” with “ONE” on every line and print only modified lines (p).$ sed '1,100 s/A/a/g’ < file.txt
- Substitute all occurrences of “
A” with “a” on lines 1 through 100.
!)Examples: Using
!$ cat file.txt The brown cow The black cow $ sed '/black/ !s/cow/horse/' < file.txt The brown horse The black cow
- Substitute “
horse” for “cow” on all lines that don’t contain “black”.$ sed '1,5 !d' < file.txt
- Delete all lines except 1 — 5 (print lines 1 through 5).
append, insert, and change with {}append,
insert, and change with curly braces
({}) $ cat file.txt That person looks pretty fishy. $ sed
‘/fishy/ { iPonyo, Ponyo, Ponyo fishy in the sea Tiny little fishy, who could you really be?
y)y)$ echo "TESTING" | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
testing