Detailed sed command

sed command

  • The sed command is more powerful than grep, in addition to having a find function, it also has a replace function

  • sed command format: sed -n 'n'p filename , the n in single quotes is a number, which means the number of lines;

  • sed can achieve most of the functions of grep, but does not display colors

  • sed -n is to print the line that meets the conditions, p is the meaning of print

  • sed -r misses

  • sed -e for multiple commands, and ; effect one line

  • sed '/[0-9]'d 2.txt d means to delete lines that do not contain numbers; it is not a real deletion, it is not displayed, which is equivalent to grep -v negation

  • sed -n '/root/'Ip "I" is not case sensitive

  • sed -i ' [0-9]'d 2.txt -i is to actually delete the lines that do not contain numbers in the document; half are not recommended.

  • Regular | means or, and sed twice means

  • sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/' 1.txt With : as the delimiter, the first paragraph Replace each other with the last paragraph, \1 represents the first () content, \2 represents the second () content, and \3 represents the third content.

  • sed -r 's/(.*)/aaa:&/g' 1.txt

sed instance 1:

  • sed -n '5'p test.txt prints line 5
  • sed -n '1,5'p test.txt prints lines 1-5
  • sed -n '1,$'p test.txt print all the content can be used
  • sed -n '/root/'p test.txt Find lines with root characters; characters should be enclosed in //
  • sed -n '/^m/'p test.txt find lines starting with m
  • sed -n '/\/login$/'p test.txt Find lines ending with /login, note: here you need to remove / before /login.
  • sed -n '/r..o/'p test.txt find lines containing r..o
  • sed -n 'oo*'p test.txt matches lines with oo*
  • sed -e 'root'p -e '/124/'p -n test.txt matches the 'root' line and the line matching '124'

Find the specified line

  • sed -n '5'p test.txt prints line 5; 
[root@yong-02 sed]# sed -n '5'p test.txt 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  • sed -n '1,5'p test.txt prints lines 1-5;
[root@yong-02 sed]# sed -n '1,5'p test.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  • sed -n '1,$'p test.txt can use (1,$) to print all the content. If there is too much content, the whole content will not be displayed. Print 20 - the last line.
[root@yong-02 sed]# sed -n '20,$'p test.txt 
yyl:x:1001:1002::/home/yyl:/bin/bash
user2:x:1009:1009::/home/user2:/bin/bash
user3:x:1010:1010::/home/user3:/bin/bash
user4:x:1011:1011::/home/user4:/bin/bash
user5:x:1012:1012::/home/user5:/bin/bash
user7:x:1014:1014::/home/user7:/bin/bash

Find the line with the specified string

  • sed -n '/root/'p test.txt Find lines with the root character; characters are enclosed in //.
[root@yong-02 sed]# sed -n '/root/'p test.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

The content of the query can also be regular

  • sed -n '/^m/'p test.txt find lines starting with m '/^m/'p

[root@yong-02 sed]# sed -n '/^m/'p test.txt 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  • sed -n '/\/login$/'p test.txt Find lines ending with /login; note: You need to remove the / before /login here.
[root@yong-02 sed]# sed -n '/\/login$/'p test.txt 
daemon:x:2:2:daemon:/sbin:/sbin/login
lp:x:4:7:lp:/var/spool/lpd:/sbin/login
  • sed -n '/r..o/'p test.txt find lines containing r.od
[root@yong-02 sed]# sed -n '/r..o/'p test.txt
operator:x:11:0:operator:/root:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
  •   sed -n 'oo*'p test.txt matches lines with oo*
[root@yong-02 sed]# sed -n '/oo*/'p test.txt |head 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/login
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/login
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  • print lines with numbers sed -n '/[0-9]/'p test.txt
[root@yong-02 sed]# sed -n '/[0-9]/'p test.txt |head 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/login
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/login
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
  • print lines starting with a number sed -n '/^[0-9]/'p test.txt
[root@yong-02 sed]# sed -n '/^[0-9]/'p test.txt
11systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
124
  • sed -e can execute multiple commands; sed -e '/root/'p -e '/124/'p -n test.txt 
[root@yong-02 sed]# sed -e '/root/'p -e '/124/'p -n test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
124

delete some lines

sed instance 2:

  • sed '1'd test.txt delete line 1
[root@yong-02 sed]# cat test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
11systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
addsfgasd
adsf
124
[root@yong-02 sed]# sed '1'd test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
11systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
addsfgasd
adsf
124
  • sed '1,3'd test.txt delete lines 1-3
[root@yong-02 sed]# cat test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
11systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
addsfgasd
adsf
124
[root@yong-02 sed]# sed '1,3'd test.txt
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
11systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
addsfgasd
adsf
124
  • sed '/mail/'d test.txt delete lines with mail characters
[root@yong-02 sed]# cat test.txt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
addsfgasd
adsf
124
[root@yong-02 sed]# sed '/mail/'d test.txt
addsfgasd
adsf
124

sed replace function

  • sed 's/[0-9]/a/g' 1.txt //Replace all numbers with a
  • sed 's/root/ROOT/g' 1.txt //Replace all root in file 1.txt with ROOT
  • sed '1,10s/root/ROOT/g' 1.txt //Replace root in the first 10 lines of file 1.txt with ROOT
  • paste 1.txt 2.txt //Concatenate the content of 1.txt with the content of 2.txt
  • sed -i '30,40s/^.*$/#&/g' 1.txt //Add # to comment out
  • sed -i 's/^/#/g' 1.txt //Add # to comment out
  • s is the meaning of replacement, g is global replacement, otherwise it will be replaced once, / can use #@ to replace the character

sed instance 3:

  • sed '1,2s/ot/to/g' test.txt
  • sed 's#ot#to#g' test.txt
  • sed 's/[0-9]//g' test.txt
  • sed 's/[a-zA-Z]//g' test.txt
  • sed -r 's/(rot)(.*)(bash)/\3\2\1/' test.txt
  • sed 's/^.*$/123&/' test.txt
  • sed -i 's/ot/to/g' test.txt

instance analysis

  • Replace ot in lines 1 to 2 with to sed '1,2s/ot/to/g' test.txt
[root@yong-02 sed]# sed '1,2s/ot/to/g' test.txt |head -5
roto:x:0:0:roto:/roto:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/login
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/login
  • / can be replaced with # or @
[root@yong-02 sed]# sed '1,2s#ot#to#g' test.txt |head -5
roto:x:0:0:roto:/roto:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/login
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/login
  • delete all numbers
[root@yong-02 sed]# sed 's/[0-9]//g' test.txt |head -5
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
daemon:x:::daemon:/sbin:/sbin/login
adm:x:::adm:/var/adm:/sbin/nologin
lp:x:::lp:/var/spool/lpd:/sbin/login
  • keep only numbers
[root@yong-02 sed]# sed 's/[^0-9]//g' test.txt |head -5
00
11
22
34
47
  • delete all letters
[root@yong-02 sed]# sed 's/[a-zA-Z]//g' test.txt |head -5
::0:0::/://
::1:1::/://
::2:2::/://
::3:4:://://
::4:7::///://
  • Use: as the division, and exchange the position of the first paragraph and the last paragraph; + is a special symbol in it, use -r to get rid of it
[root@yong-02 sed]# sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'g test.txt
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/login:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/login:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt

Enter image description

  • ; a semicolon can be matched multiple times
[root@yong-02 sed]# sed -n '/root/p;/adm/p' test.txt
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
  • -e make multiple commands, and ; effect one line
[root@yong-02 sed]# sed -n -e '/root/'p -e '/adm/'p  test.txt
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
  • Add aaa: & in front of each line to indicate the content of the previous ()
[root@yong-02 sed]# sed -r 's/(.*)/aaa:&/g' test.txt
aaa:root:x:0:0:root:/root:/bin/bash
aaa:bin:x:1:1:bin:/bin:/sbin/nologin
aaa:daemon:x:2:2:daemon:/sbin:/sbin/login
aaa:adm:x:3:4:adm:/var/adm:/sbin/nologin
aaa:lp:x:4:7:lp:/var/spool/lpd:/sbin/login
aaa:sync:x:5:0:sync:/sbin:/bin/sync
aaa:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
aaa:halt:x:7:0:halt:/sbin:/sbin/halt
  • Add aaa: \1 in front of each line to indicate the content of the previous ()
[root@yong-02 sed]# sed -r 's/(.*)/aaa:\1/g' test.txt
aaa:root:x:0:0:root:/root:/bin/bash
aaa:bin:x:1:1:bin:/bin:/sbin/nologin
aaa:daemon:x:2:2:daemon:/sbin:/sbin/login
aaa:adm:x:3:4:adm:/var/adm:/sbin/nologin
aaa:lp:x:4:7:lp:/var/spool/lpd:/sbin/login
aaa:sync:x:5:0:sync:/sbin:/bin/sync
aaa:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
aaa:halt:x:7:0:halt:/sbin:/sbin/halt
  • -i will directly change the content of the file itself, for example: sed -i 's/:/#/g' test.txt Change all ":" to "#"
[root@yong-02 sed]# sed -i 's/:/#/g' test.txt
[root@yong-02 sed]# cat test.txt
root#x#0#0#root#/root#/bin/bash
bin#x#1#1#bin#/bin#/sbin/nologin
daemon#x#2#2#daemon#/sbin#/sbin/login
adm#x#3#4#adm#/var/adm#/sbin/nologin
lp#x#4#7#lp#/var/spool/lpd#/sbin/login
sync#x#5#0#sync#/sbin#/bin/sync
shutdown#x#6#0#shutdown#/sbin#/sbin/shutdown
halt#x#7#0#halt#/sbin#/sbin/halt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897140&siteId=291194637