Professional Documents
Culture Documents
sed
https://www.gnu.org/software/sed/
2
sed
I Example: replace each appearance of dog with cat
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
5
sed scripts
[addr]X[options]
6
sed scripts
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
10
Numeric addresses
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
N~M
where N and M integers, matches lines N, N+M, N+2M, ...
I Replace dog by cat in all even lines
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
13
Examples
I Delete lines 10 to 20
14
Command grouping
{ commands }
A group of commands enclosed in braces {} may be triggered by a
single address
15
Exercises
16
Exercises
17
Exercises
18