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

Common commands for dealing with text in sed

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Sed is a text processing tool that can replace, add, select, and other specific tasks.

Format

Sed option Action File name

Replace a field in a field in a file with sed

For example, if there is a field in the middle of a yaml file that needs to be modified, you can use sed to replace it on the outside command line.

Format: sed's / original character / new character / 'file name

[root@zhaocheng ~] # sed's Universe systemctlUniverse ServiceBfW.vTZKpm7mrra9WMsxvBfW.VTZKpm7mrra9WMsxvBfW.VTZKm7mrra9WMsxvBfW.VTZKpm7bind999DUBfW.VTZKpm7bind99999VTZKpm0BfWW.VTZKm7mrra9WMsxvBfW.VTZKpm7mrra9WMsxvBfW.VT

The output above will only be output to the screen, and the file will not be modified. If you replace it directly, you can add-I

[root@zhaocheng] # sed-I's system system ctlhandle ServiceBfW.vTZKpm7mrra9WMsxvBfW.VTZKpm7mrra9WMsxvBfW.18241WMsxvBfW.VTZKpm7mrra9WMsxvBfW.18834DATA 18297:dadaservice start mysqlddadad:nginx:sdada

Sed parameter meaning instruction

Common commands: an add c replace d delete I insert p print s replace

Delete a row

If you need to delete some lines when processing text, you can use d for deletion

Do not add sed-I will not delete, do not add will only display on the screen

[root@zhaocheng] # sed '1d' filetest / / Delete the first line [root@zhaocheng] # sed' $d' filetest / / Delete the last line [root@zhaocheng] # sed'1 2d' filetest / / Delete the first line to the second line [root@zhaocheng] # sed '3d' filetest / / delete the third line to the last line

Show a row

For example, when dealing with text, you need to extract a certain line of text.

[root@zhaocheng] # sed-n '1p' filetest / / display the first line [root@zhaocheng] # sed-n' $p' filetest / / display the last line [root@zhaocheng] # sed-n'1 line 2p' filetest / / display the first line to the second line [root@zhaocheng] # sed-n'3 line filetest / / display the third line to the last line

Use schema for query

For example, if you want to find out this field by dealing with large text, you can directly use the-n parameter to display, the action to specify the keywords to be extracted, and p to print.

[root@zhaocheng] # sed-n'/ bin/p' filetest / / query includes all lines where the keyword bin is located [root@zhaocheng] # sed-n'/\ $/ p' filetest service start mysqld$// query includes all lines where the keyword $is located, and uses backslash\ to mask the special meaning, otherwise it will not be recognized

Add one or more lines of string

For example, if you add a field by processing text and specify that it is added to a line, you can use a, which is added, and 1a is added below the first line. If there are more fields, you can use set nu in vim to display the line number, or directly use cat-n to display the line number on the command line, and use sed 'lines a xxxxxx file name on the command line.

Add a new line below the first line

[root@zhaocheng ~] # sed'1a sed is very useful' filetest bin:*:17834:0:99999:7:dadsed is very usefullp start mysqld$dadad:nginx:sdada% 7adasyncDadaservice start mysqld$dadad:nginx:sdada%

Add sed is very useful to lines 2 and 3

[root@zhaocheng ~] # sed '2pr 3a sed is very useful' filetest bin:*:17834:0:99999:7:dadlp:*:17834:0:99999:7adased is very usefulsync:*:17834:0:99999:7:::ggsed is very useful

To replace the line, here I replace the first line with kuberntes,1c, which is the first line, and c is the replacement

[root@zhaocheng] # sed'1c kubernetes' filetest KuberneteslpParParticipator 178341c kubernetes' filetest KuberneteslpGroud 178341c KuberneteslslpGV 178341c kuberneteslslpGV 178341c kuberneteslslpGV 178341c Fsdanginx Dadaservice start mysqld$dadad:nginx:sdada% 8289 Dadaservice

Adding the last line of this text to jenkins,$ is the last line, and an is adding

[root@zhaocheng ~] # sed-I'$a jenkins' filetest [root@zhaocheng ~] # cat filetesthellpsync:*:17834:0:99999:7:::ggshutdown:*:17834:0:99999:7::dahalt:*:17834:0:99999:7::fsdadadad:nginx:sdada%jenkins

Delete the matching row, and delete the matching jenkins line.

[root@zhaocheng ~] # sed-I'/ jenkins/d' filetest [root@zhaocheng ~] # cat filetesthellpsync:*:17834:0:99999:7:::ggshutdown:*:17834:0:99999:7::dahalt:*:17834:0:99999:7::fsdadadad:nginx:sdada%

Match the # in the text and delete it. Here, ^ starts with # / d is deleted.

[root@zhaocheng ~] # cat filetest#hellp#sync:*:17834:0:99999:7:::gg#shutdown:*:17834:0:99999:7::dahalt:*:17834:0:99999:7::fsdadadad:nginx:sdada% [root@zhaocheng ~] # sed-I'/ ^ # / d' filetest [root@zhaocheng ~] # cat filetesthalt:*:17834:0:99999:7::fsdadadad:nginx:sdada%

Delete the first number of each line in the text

[root@zhaocheng ~] # cat filetest1fghjkl2ghjkl;'3fghjkl;4dfghjkl; [root@zhaocheng ~] # sed-I's / ^ [0-9] / / g'filetest [root@zhaocheng ~] # cat filetestfghjklghjkl;'fghjkl;dfghjkl

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