sed
sed
: Powerful UNIX filter that parses and transforms text using a simple, compact programming language.
sed
History:
- 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
d
elete 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.Example: Using
d
elete$ sed '1,10d' < file.txt
- Delete lines 1 through 10 (print
file.txt
without the first ten lines).
p
: printFormat:
[address1][,address2]p
stdout
.sed
is run with -n
to suppress automatic printing of pattern space.)Example: Using
p
rint$ sed -n '1,10 p' < file.txt
- Print lines 1 through 10.
s
: 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 ofpattern
in the pattern space.
- If
n
andg
aren’t set, thensed
will only match the first occurrence ofpattern
per line (behaves like-n1
).p
: Print contents of pattern space.
Note on
s
ubstitution 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
Example: Using
s
ubstitute$ echo "I love Linux" | sed 's|Linux|GNU/Linux|g' I love GNU/Linux
- Replace first occurrence of “
Linux
” with “GNU/Linux
” on every line.
a
: appendFormat:
[address]a\ text
Note: append doesn’t accept address ranges.
Example: Using
a
ppend$ echo "Hello" | sed '/Hello/ a\World' Hello World
- Appends “
World
” after every line containing “Hello
”
i
: insertFormat:
[address]i\ text
Note: insert doesn’t accept address ranges.
Example: Using
i
nsert$ echo "World" | sed '/World/ i\Hello' Hello World
- Inserts “
Hello
” before every line containing “World
”
c
: changeFormat:
[address(es)]c\ text
Example: Using
c
hange$ echo "REPLACEME" | sed '/REPLACEME/ c\bingus' bingus
y
: 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.
Example: Using transform (
y
)$ sed 'y/abc/xyz/'
- Replaces every instance of
a
withx
,b
withy
, andc
withz
- Functionally equivalent to
tr 'abc' 'xyz'
q
: quitFormat:
[address]q
sed
. No more input lines are read, nothing goes to stdout
.Note: quit takes at most one address.
Example: Using
q
uitsed '100 q' file.txt
- Print first 100 lines of
file.txt
tostdout
.
p
, d
, and !
Examples: Using the
d
elete 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
p
rint command$ sed 'p' < file.txt
- Print every line in
file.txt
twice.$ sed -n 'p' < file.txt
- Print every line in
file.txt
once.
- Remember:
-n
suppresses 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
p
rint andd
elete are opposite functions.
s
ubstitutionExamples: Using
s
ubstitution$ 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
”.
Example: Behavior of
s
ubstitution (important!)Suppose a file named
file.txt
with this content:one two three, one two three four three two one one hundred
If 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 hundred
- Note how only the first instance of “
one
” gets affected on each line (if you want to apply substitution to all occurrences use the global flag)
- Remember: Each line gets processed separately, and substitute defaults to substituting the first occurrence.
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 hundred
- Note how only the second occurrence is modified.
Examples: Using
s
ubstitution (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).
a
ppend, i
nsert, and c
hange with {}
Example: Using
a
ppend,i
nsert, andc
hange with curly braces ({}
) $ cat file.txt That person looks pretty fishy. $ sed ‘/fishy/ { i
, Ponyo, Ponyo fishy in the sea a
who could you really be? c
Tiny little fishy, }’ < file.txtPonyo, Ponyo, Ponyo fishy in the sea Tiny little fishy, who could you really be?
y
)Example: Using transform (
y
)$ echo "TESTING" | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' testing
- Transform uppercase to lowercase.