- d delete
lines from the file. All matched lines are not written to the output.
$ sed '1,4d' sedfile
baker new 300
baker blue 200
charlie old 100
charlie new 400
charlie blue 400
dog old 230
dog new 410
dog blue 700
$ sed '/blue/d' sedfile
able old 100
able new 200
baker old 300
baker new 300
charlie old 100
charlie new 400
dog old 230
dog new 410
- a append
Append to file.
$ cat data
$a\
edward new 410\
edward old 345
$ sed -f data sedfile
able old 100
able new 200
able blue 200
baker old 300
baker new 300
baker blue 200
charlie old 100
charlie new 400
charlie blue 400
dog old 230
dog new 410
dog blue 700
edward new 410
edward old 345
- i insert
Insert works the same as append except the data is written before
the matched line.
- c change
Change deletes lines and replaces them with text.
$ cat data
1,3c\
edward new 410\
edward old 345
$ sed -f data sedfile
edward new 410
edward old 345
baker old 300
baker new 300
baker blue 200
charlie old 100
charlie new 400
charlie blue 400
dog old 230
dog new 410
dog blue 700
- s substitute.
The substitute function changes part of selected lines.
$sed 's/blue/red/' sedfile
able old 100
able new 200
able red 200
baker old 300
baker new 300
baker red 200
charlie old 100
charlie new 400
charlie red 400
dog old 230
dog new 410
dog red 700
The substitute command uses the g, p and w flags.
- g
- The g is global. Replace all occurrences of the pattern in the line.
$ sed 's/0//g' sedfile
able old 1
able new 2
able blue 2
baker old 3
baker new 3
baker blue 2
charlie old 1
charlie new 4
charlie blue 4
dog old 23
dog new 41
dog blue 7
- p
- The p flag prints the line only if a replace is made.
$ sed -n 's/old/y2k/p' sedfile
able y2k 100
baker y2k 300
charlie y2k 100
dog y2k 230
- w
- The w flag write the line to a line if the line is altered.
$ sed 's/old/y2k/w new' sedfile
able y2k 100
able new 200
able blue 200
baker y2k 300
baker new 300
baker blue 200
charlie y2k 100
charlie new 400
charlie blue 400
dog y2k 230
dog new 410
dog blue 700
$ cat new
able y2k 100
baker y2k 300
charlie y2k 100
dog y2k 230