Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the sed command

Shulou Source: shulou.com Published: 2022-06-01 02:56:44 09月29日 Update

This article is about how to use the sed command. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Example of text file changes using Sed

Suppose you have a file named linuxidc.com.txt that contains the following lines:

1, linux idc, Title 639, Price $9.30

2, linuxidc ninhao, Title 761, Price $5.90

3, linuxmi nihao, Title 880, Price $9.30

4, linuxmi com, Title 198, Price $1.30

5, Johnny Cash, Title 582, Price $6.50

6, Elvis Presley, Title 370, Price $9.30

7, John Lennon, Title 571, Price $8.90

8, Michael Jackson, Title 673, Price $7.50

9, linuxidc com, Title 585, Price $1.80

You want to change all price changes from $9.30 to $8.88. To do this, you can use the sed command in the following ways:

linuxidc@Ubuntu:~/linuxidc.com$ sed 's/9.30/8.88/' linuxidc.com.txt > linuxidc.txt

This code makes changes and saves the modified file as linuxidc.txt. The new file generated contains:

linuxidc@ubuntu:~/linuxidc.com$ cat linuxidc.txt1, linux idc, Title 639, Price $8.882, linuxidc ninhao, Title 761, Price $5.903, linuxmi nihao, Title 880, Price $8.884, linuxmi com, Title 198, Price $1.305, Johnny Cash, Title 582, Price $6.506, Elvis Presley, Title 370, Price $8.887, John Lennon, Title 571, Price $8.908, Michael Jackson, Title 673, Price $7.509, linuxidc com, Title 585, Price $1.80

If you want to replace all occurrences of "linuxmi" with "linuxidc," you can also use sed this way:

linuxidc@ubuntu:~/linuxidc.com$ sed 's/linuxmi/linuxidc/' linuxidc.com.txt > linuxidc.txt

The resulting modification file linuxidc.txt will contain the following text:

linuxidc@ubuntu:~/linuxidc.com$ cat linuxidc.txt1, linux idc, Title 639, Price $9.302, linuxidc ninhao, Title 761, Price $5.903, linuxidc nihao, Title 880, Price $9.304, linuxidc com, Title 198, Price $1.305, Johnny Cash, Title 582, Price $6.506, Elvis Presley, Title 370, Price $9.307, John Lennon, Title 571, Price $8.908, Michael Jackson, Title 673, Price $7.509, linuxidc com, Title 585, Price $1.80

Filter with Sed command

Sed is also often used to filter lines in files. For example, if you only want to see lines that contain "linuxidc," you can use the following sed command:

linuxidc@ubuntu:~/linuxidc.com$ sed -n '/linuxidc/p' linuxidc.com.txt > linuxidc.txt

The new linuxidc.txt file will contain the following lines:

linuxidc@ubuntu:~/linuxidc.com$ cat linuxidc.txt2, linuxidc ninhao, Title 761, Price $5.909, linuxidc com, Title 585, Price $1.80

Thank you for reading! About "sed command how to use" this article will be shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Tags: Files commands content text methods more articles generated good practical for this code price can be passed articles for see knowledge examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Shulou Tech Info NVidia Microsoft