This section explains how to use the sed command for string replacement, which allows you to “insert and delete strings” in addition to “replace strings”.
Format
sed [Optional] ScriptCommand InputFile
Examples of Use
Replace first match
In each line of the test.txt file, replace the first match with aaa with AAA.
$ cat test.txt
111aaa222aaa
aaa222ccc444
ddd333aaa555
$
$ sed s/aaa/AAA/ test.txt
111AAA222aaa
AAA222ccc444
ddd333AAA555
Replace all
In each line of the test.txt file, replace all matches for aaa with AAA.
$ cat test.txt
111aaa222aaa
aaa222ccc444
ddd333aaa555
$
$ sed s/aaa/AAA/g test.txt
111AAA222AAA
AAA222ccc444
ddd333AAA555
Replace and Overwrite
The -i option can be used to overwrite with the replacement result.
$ cat test.txt
111aaa222aaa
aaa222ccc444
ddd333aaa555
$
$ sed -i s/aaa/AAA/ test.txt
111AAA222aaa
AAA222ccc444
ddd333AAA555
$
$ cat test.txt
111AAA222aaa
AAA222ccc444
ddd333AAA555
Delete specified line
Delete lines 3-5.
$ cat test.txt
1111
2222
3333
4444
5555
6666
7777
$
$ sed '3,5d' test.txt
1111
2222
6666
7777
Whitespace deletion
$ cat test.txt
<body>
<div class="main">
<p>Hello World!</p>
</div>
</body>
$
$ sed 's/ //g' test.txt
<body>
<divclass="main">
<p>HelloWorld!</p>
</div>
</body>
Blank line deletion
$ cat test.txt
1111
2222
3333
4444
5555
6666
7777
$
$ sed '/^$/d' test.txt
1111
2222
3333
4444
5555
6666
7777
Insert text before or after the specified line
- I
- Can be inserted before the specified position.
- a
- Can be inserted after the specified position.
$ cat test.txt
1111
2222
3333
4444
5555
6666
7777
$
$ sed '3i aaaaaaaaa' test.txt
1111
2222
aaaaaaaaa
3333
4444
5555
6666
7777
$
$ sed '3a aaaaaaaaa\nbbbbbbbbb' test.txt
1111
2222
3333
aaaaaaaaa
bbbbbbbbb
4444
5555
6666
7777
Insert text before and after specified keywords
$ cat test.txt
1111
2222
3333
4444
5555
6666
7777
$ sed '/4444/i aaaaaaaaa' test.txt
1111
2222
3333
aaaaaaaaa
4444
5555
6666
7777
$ sed '/4444/a aaaaaaaaa' test.txt
1111
2222
3333
4444
aaaaaaaaa
5555
6666
7777
Configuration file setting value change
By utilizing “sed,” the process of changing configuration values can also be written in scripts.
$ sed -i 's/^post_max_size.*/post_max_size = 100M/' /etc/php.ini
$
$ grep post_max_size /etc/php.ini
post_max_size = 100M
Replace multiple files at once
The following files exist
$ tree
.
├── aaa
│ ├── 1.md
│ ├── 1.txt
│ ├── 2.md
│ └── 2.txt
└── bbb
├── 1.md
└── 1.txt
2 directories, 6 files
$
$ cat aaa/1.md
aaa
$ cat aaa/2.md
aaa
$ cat bbb/1.md
aaa
This section shows how to replace all the files with the extension md
at once.
Multiple files can be manipulated at once by using the find and xargs commands as shown below.
$ find . -type f -name "*.md" | xargs sed -i s/aaa/AAA/
$
$ cat aaa/1.md
AAA
$ cat aaa/2.md
AAA
$ cat bbb/1.md
AAA
Using GNU version of sed on Mac
The standard version of sed stored on the Mac is not the GNU version, so the aforementioned options may not be available.
You can install the GNU version of sed with the following command
brew install gnu-sed
After installation, the gsed command is available.