Lord Sed
Table of Contents
What is SED?
Sed short for stream editor. It reads input line by line, applies commands and output the results.
- find and replace text
- delete lines
- insert/append text
- extract specific lines
- batch editing files
Edit and Save
First we must discuss that in default behavior sed don’t save the output to the file. Below are some of the common method to ssave to the file.
Save to a new file
This is the recommended method, for safety.
1sed 's/old/new/g' file.txt > new.txt
Overwrite the original - in-place editing
If you are sure what you are doing.
1sed -i 's/old/new/g' file.txt
Print file
1sed '' file.txt
2
3Hello World!
4hello world!
5Hello World!
6Dunk it Jonathan!
7Nagrigat agbiyag ditoy lubong.
8I love you MOM!
Print specific line
1sed -n '4p' file.txt
2
3Dunk it Jonathan!
Print line range
1sed -n '3,6p' file.txt
2
3Hello World!
4Dunk it Jonathan!
5Nagrigat agbiyag ditoy lubong.
6I love you MOM!
Print matching lines
1sed -n '/I/p' file.txt
2
3I love you MOM!
Find and Replace
Replace first match
1sed 's/Hello/Goodbye/' file.txt
2
3Goodbye World!
4hello world!
5Goodbye World!
6Dunk it Jonathan!
7Nagrigat agbiyag ditoy lubong.
8I love you MOM!
Replace all matches
1sed 's/World/Hell/g' file.txt
2
3Hello Hell!
4hello world!
5Hello Hell!
6Dunk it Jonathan!
7Nagrigat agbiyag ditoy lubong.
8I love you MOM!
Case sensitive replace
1sed 's/Hello/Goodbye/gi' file.txt
2
3Goodbye World!
4Goodbye world!
5Goodbye World!
6Dunk it Jonathan!
7Nagrigat agbiyag ditoy lubong.
8I love you MOM!
Replace only on specific line
1sed '3s/Hello/Goodbye/' file.txt
2
3Hello World!
4hello world!
5Goodbye World!
6Dunk it Jonathan!
7Nagrigat agbiyag ditoy lubong.
8I love you MOM!
Delete
Delete a word
This is the same with replace, just set it to blank.
1sed 's/Dunk//g' file.txt
2
3Hello World!
4hello world!
5Hello World!
6 it Jonathan!
7Nagrigat agbiyag ditoy lubong.
8I love you MOM!
Delete a line
1sed '5d' file.txt
2
3Hello World!
4hello world!
5Hello World!
6Dunk it Jonathan!
7I love you MOM!
Delete Range
1sed '2,6d' file.txt
2
3Hello World!
Delete matching line
1sed '/I/d' file.txt
2
3Hello World!
4hello world!
5Hello World!
6Dunk it Jonathan!
7Nagrigat agbiyag ditoy lubong.
Insert and Appending
Insert before a line
1sed '2i Hey let me in!' file.txt
2
3Hello World!
4Hey let me in!
5hello world!
6Hello World!
7Dunk it Jonathan!
8Nagrigat agbiyag ditoy lubong.
9I love you MOM!
Append after a lin
1sed '3a Dunk it Jonathan!' file.txt
2
3Hello World!
4hello world!
5Hello World!
6Dunk it Jonathan!
7Dunk it Jonathan!
8Nagrigat agbiyag ditoy lubong.
9I love you MOM!
Insert before match
1sed '/Dunk/i Lalaland' file.txt
2
3Hello World!
4hello world!
5Hello World!
6Lalaland
7Dunk it Jonathan!
8Nagrigat agbiyag ditoy lubong.
9I love you MOM!
Regex
Let’s first change the content of file.txt to better demostrate regex. file.txt
1Hello World!
2hello world!
3<pusa>
41 2 3 takbo...
5pelepens; flood control;
6Dunk it Jonathan!
Word boundary match
1sed 's/\<pusa\>/aso/g' file.txt
2
3Hello World!
4hello world!
5<aso>
61 2 3 takbo...
7pelepens; flood control;
8Dunk it Jonathan!
Replace Number
1sed 's/[0-2]\+/baliw/g' file.txt
2
3Hello World!
4hello world!
5<pusa>
6baliw baliw 3 takbo...
7pelepens; flood control;
8Dunk it Jonathan!
Capture groups
1sed 's/\(hello\) \(world\)/\2, \1/gi' file.txt
2
3World, Hello!
4world, hello!
5<pusa>
61 2 3 takbo...
7pelepens; flood control;
8Dunk it Jonathan!
Multiple Commands
1sed -e 's/Dunk/Shoot/' -e 's/takbo/talon/' file.txt
2
3Hello World!
4hello world!
5<pusa>
61 2 3 talon...
7pelepens; flood control;
8Shoot it Jonathan!
Using semicolon
1sed -e 's/Dunk/Shoot/; s/takbo/talon/' file.txt
2
3Hello World!
4hello world!
5<pusa>
61 2 3 talon...
7pelepens; flood control;
8Shoot it Jonathan!
Conditional Execution
Run command only on matching line
1sed '/orld/s/Hello/bubble gum/gi' file.txt
2
3bubble gum World!
4bubble gum world!
5<pusa>
61 2 3 takbo...
7pelepens; flood control;
8Dunk it Jonathan!
Real world example
Removing blank lines
1sed '/^$/d'
Remove trailing spaces
1sed 's/[ \t]*$//'
Number lines
1sed '=' file.txt | sed 'N;s/\n/ /'
2
31 Hello World!
42 hello world!
53 <pusa>
64 1 2 3 takbo...
75 pelepens; flood control;
86 Dunk it Jonathan!
Extract between two patterns
1sed -n '/1/,/flood/p' file.txt
2
31 2 3 takbo...
4pelepens; flood control;
Scripts
Create file script.sed.
1s/hello/world/g
2s/\<pusa\>/aso/g
Run.
1sed -f script.sed file.txt
2
3Hello World!
4world world!
5<aso>
61 2 3 takbo...
7pelepens; flood control;
8Dunk it Jonathan!