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

What are the ways to use the sed command

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the use of the sed command". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the use of the sed command?"

Sed is an online editor that processes one line of content at a time. When processing, the currently processed line is stored in a temporary buffer, called "pattern space" (pattern space), and then the contents of the buffer are processed with the sed command, and when the processing is completed, the contents of the buffer are sent to the screen.

Sed usage

Most of the functions of grep can be achieved with sed, and you can also find and replace them.

Syntax:

Sed [options] 'command' filename

Sed [options]-f scriptfile filename

Options:

The-nRAPR Murn option is used with the p command to represent printing

-e: equivalent to-expression, multi-point editing, executing multiple commands in one line

Match (grep) function with sed = print lines with root strings

[root@cham2 sed] # sed-n'/ root/'p test.txtroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin [root@cham2 sed] # sed-nr'/ r+o/'p test.txtroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinsystemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/ Sbin/nologintss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologin [root@cham2 sed] # sed-nr'/ o*t/'p test.txtroot:x:0:0:root:/root:/bin/bashshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halo1t:/sbin:/sbin/haltoperator: X:11:0:operator:/root:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinsystemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologinsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinpolkitd:x:998:996:User for polkitd:/:/sbin/nologintss:x:59:59:Account used by the trousers Package to sandbox the tcsd daemon:/dev/null:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin ignores case (I) [root@cham2 sed] # sed-n'/ nologin/'Ip test.txtbin:x:1:1:bin:/bin:/sbin/NOLOGINdaemon:x:2:2:daemon:/sbin:/sbin/nologinad11om:x: 3:4:ad1om:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spoool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spoooooool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/ Sbin/nologinsystemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologinsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinpolkitd:x:998:996:User for polkitd:/:/sbin/nologintss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologinpostfix:x:89:89::/var/spool/postfix: / sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologin prints the specified line (p) [root@cham2 sed] # sed-n'2p test.txtbin:x:1:1:bin:/bin:/sbin/NOLOGIN [root@cham2 sed] # sed-n '2line 5p test.txtbin:x:1:1:bin:/bin:/sbin/NOLOGINdfsdfdsfsdfs : 1241124:jksdfhdskjf:sfdsf:rao:325345345222222:r.o:dfdsfdsgdaemon:x:2:2:daemon:/sbin:/sbin/nologin [root@cham2 sed] # sed-n'15 $'p test.txtnobody:x:99:99:Nobody:/:/sbin/nologinsystemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologinsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinpolkitd:x:998:996:User for polkitd:/:/sbin/nologintss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/ The sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologin-e option allows you to execute multiple commands on the same line (print the first line and match bus) [root@cham2 sed] # sed-e '1roomp-e' / bus/'p-n test .txtroot: x:0:0:root:/root:/bin/bashsystemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologin [root@cham2 sed] # sed-e '1roomp-e' / root/'p-n test.txtroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin

The command "sed-e '1upload p-e' / bus/'p-n test.txt" means to print the first line, as well as a line that matches' bus'. When the printed content and the matching content are the same, they are displayed separately, that is, the two commands are carried out at the same time but relatively independently.

Delete the specified line (d) [root@cham2 sed] # sed'1 10 test.txtmail:x:8:12:mail:/var/spoooooool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinsystemd-bus-proxy:x:999: 997:systemd Bus Proxy:/:/sbin/nologinsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinpolkitd:x:998:996:User for polkitd:/:/sbin/nologintss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege -separated SSH:/var/empty/sshd:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologin [root@cham2 sed] # sed'1 Magi 18 test.txtpolkitd:x:998:996:User for polkitd:/:/sbin/nologintss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74: Privilege-separated SSH:/var/empty/sshd:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologin

Note: this method does not delete the contents of the file, and the content printed on the screen deletes the specified line. So how to delete the specified content in the file directly? As follows:

[root@cham2 sed] # wc-l test.txt23 test.txt [root@cham2 sed] # sed-I'1 test.txt23 test.txt 20 days test.txt [root@cham2 sed] # wc-l test.txt3 test.txt [root@cham2 sed] # cat test.txtpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/ Nologin [root@cham2 sed] # sed-I'/ SSH/'d test.txt [root@cham2 sed] # cat test.txtpostfix:x:89:89::/var/spool/postfix:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologin

Find replace (s) similar to find replace in vim

[root@cham2 sed] # sed'1 Magi 10s test.txttoor:x:0:0:toor:/toor:/bin/bashbin:x:1:1:bin:/bin:/sbin/NOLOGINdfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345222222:r.o:dfdsfdsgdaemon:x:2:2:daemon:/sbin:/sbin/nologinad11om:x:3:4:ad1om:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spoool/lpd:/sbin / nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halo1t:/sbin:/sbin/haltmail:x:8:12:mail:/var/spoooooool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/ Sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinsystemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologinsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinpolkitd:x:998:996:User for polkitd:/:/sbin/nologintss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologinpostfix : x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologin if you do not use the head command here The full contents of the test.txt will be displayed. [root@cham2 sed] # sed'1 3root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/NOLOGINdfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345 10s 3toor:x:0:0:toor:/toor:/bin/bashbin:x:1:1:bin:/bin:/sbin/NOLOGINdfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345 to test.txt | head-3toor:x:0:0:toor:/toor:/bin/bashbin:x:1:1:bin:/bin:/sbin/NOLOGINdfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345 [root@cham2 sed] # cat test.txt | head-3root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/NOLOGINdfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345

Replace the position of the first string and the last string of each line:

[root@cham2 sed] # head-3 test.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/NOLOGINdfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345 [root@cham2 sed] # head-3 test.txt | sed-r's / ([^:] +): (. *): ([^:] +) /\ 3:\ 2:\ 1/'/bin/bash:x:0:0:root:/root : root/sbin/NOLOGIN:x:1:1:bin:/bin:bin325345345:1241124:jksdfhdskjf:sfdsf:rao:dfsdfdsfsdfs

Note: the string in parentheses can be replaced by a combination of backslashes and numbers in order.

Add the specified character at the beginning of each line:

[root@cham2 sed] # head-3 test.txt | sed-r's / (. *) / AB:\ 1/'AB:root:x:0:0:root:/root:/bin/bashAB:bin:x:1:1:bin:/bin:/sbin/NOLOGINAB:dfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345 [root@cham2 sed] # head-3 test.txt | sed-r's / (. *) / AB:&/'AB:root:x:0:0: Root:/root:/bin/bashAB:bin:x:1:1:bin:/bin:/sbin/NOLOGINAB:dfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345

Note: here & represents the content in parentheses = "\ 1"

At this point, I believe you have a deeper understanding of "what is the use of the sed command?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report