You are on page 1of 18

The sed command

sed

sed (stream editor) is a non-interactive command-line text editor

https://www.gnu.org/software/sed/

I Performs basic text transformations (substitutions, deletions,


etc.) on the input stream
I Makes only one pass over the input, processing the text line
by line
I Able to filter text in a pipeline

sed OPTIONS... [SCRIPT] [INPUTFILE...]

2
sed
I Example: replace each appearance of dog with cat

sed ’s/dog/cat/g’ input.txt

I By default sed writes to the standard output


I This behavior can be changed with the -i option to edit the
file “in place”

sed -i ’s/dog/cat/g’ input.txt

I By default sed uses the first non option parameter as the


script
I This behavior can be changed with the -e and -f options

3
sed options
-e script
Use the commands in script
-f script-file
Read the commands from the file script-file
-i
Edit the file in place
-n
Suppress automatic output
-E
Use extended regular expressions
4
sed options

The following commands are equivalent:


I No options

sed ’s/dog/cat/g’ input.txt

I Using the -e option

sed -e ’s/dog/cat/g’ input.txt

I Using the -f option

echo ’s/dog/cat/g’ > script.sed


sed -f script.sed input.txt

5
sed scripts

A sed script is a set of commands separated by semicolons (;) or


newlines.

A sed command has the following syntax:

[addr]X[options]

I [addr] is an optional address that can be a line number, a


range of lines or a regular expression
I X is a single-letter command to be executed on the lines that
match the address pattern
I Some commands have [options] that modify their default
behavior

6
sed scripts

sed ’10,20s/dog/cat/g’ input.txt

I 10,20 is the address: lines 10 to 20


I s/dog/cat/ is the command: replace dog by cat
I g is an option to the s command: replace all appearances in
each line

sed ’s/dog/cat/’ input.txt

I No address: perform substitution in all lines


I No options: replace only the first ocurrence in each line

7
Basic sed commands

s/regexp/string/[flags]
Substitute regular expression regexp by string, flags are optional:
I g, replaces all matches, not just the first.
I N, where N is a number, replace only the Nth match
I p, prints the new line after replacement

q[exit_code]
Exit sed without processing any more commands or input

8
Basic sed commands

d
Delete the current line and start next cycle ignoring any other
commands
p
Print the current line, usually used together with the -n option
n
If automatic output is not disabled, print the current line and
replace it with the next input line; useful to skip lines

#
Start a comment until the end of line, no address allowed

9
sed addresses

I The address determines the line or lines on which a sed


command will be executed
I If the address is missing, the command is run on all lines
I The address can be a line number, a line range or a regular
expression
I The symbol ! at the end of an address means complement:
all lines except those matching the address pattern

10
Numeric addresses

I A single number indicates the line where the command should


be executed

sed ’125s/dog/cat/’ input.txt

I The $ symbol refers to the last line in the input

sed ’$s/dog/cat/’ input.txt

11
Range addresses

N,M
where N an M are integers, matches all lines between N and M
(inclusive)
I Replace dog by cat in lines from 30 to 40

sed ’30,40s/dog/cat/’ input.txt

N~M
where N and M integers, matches lines N, N+M, N+2M, ...
I Replace dog by cat in all even lines

sed ’2~2s/dog/cat/’ input.txt

12
Regular expression addresses

/regexp/
where regexp is a regular expression, refers to all lines that match
regexp
I Replace dog by cat in all lines starting with a capital letter

sed ’/^[A-Z]/s/dog/cat/’ input.txt

I sed accepts the same regular expressions as grep


I Option -E for extended regular expressions

13
Examples
I Delete lines 10 to 20

sed ’10,20d’ input.txt

I Print all lines containing the regular expression regexp (grep)

sed -n ’/regexp/p’ input.txt

I Replace dog by cat in all lines except 10-20

sed ’10,20!/s/dog/cat/’ input.txt

I Replace dog by cat in all lines not matching regexp

sed ’/regexp/!/s/dog/cat/’ input.txt

14
Command grouping

{ commands }
A group of commands enclosed in braces {} may be triggered by a
single address

sed -n ’10,20{s/dog/cat/ ; p}’

I The -n option disables automatic printing


I The two commands s/dog/cat/ and p are executed on
matching lines
I s/dog/cat/ replaces dog by cat (only the first occurrence)
I After that, p prints the line

15
Exercises

Which is the output of the following commands?

seq 1 20 | sed -n ’n;n;p’

seq 1 20 | sed -n ’/[26]/{n;n;p}’

16
Exercises

I Ex. 1: What is the output of the last command if we remove


the braces?

17
Exercises

I Ex. 2: Write a sed command to display lines from 100 to 200


(inclusive) of file adult.data

18

You might also like