[Bash] sed command

The sed command is a stream editor used for filtering and transforming text.

sed 'command' file

Create and view the initial content of sample.txt:

echo -e "Hello World\nThis is a sample file\nBash scripting is powerful\nLearning grep, awk, and sed\nAnother line with the word World" > sample.txt
cat sample.txt

Output:

Hello World
This is a sample file
Bash scripting is powerful
Learning grep, awk, and sed
Another line with the word World

Substitute a string in the file:

sed 's/World/Universe/g' sample.txt

Hello Universe
This is a sample file
Bash scripting is powerful
Learning grep, awk, and sed
Another line with the word Universe

Edit files in place:

sed -i '' 's/sample/example/g' sample.txt

Delete lines containing the word "Bash":

sed '/Bash/d' sample.txt

Hello World
This is a example file
Learning grep, awk, and sed
Another line with the word World
posted @ 2024-05-17 14:00  Zhentiw  阅读(3)  评论(0编辑  收藏  举报