Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the sed command

2025-02-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report