The replacement function of SED is quite similar to the VIM syntax.
Replace 1 to 10 rows of Nologin with login
[[Email protected] ~]# sed ' 1,10s/nologin/login/g ' 1.txt
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/login
Daemon:x:2:2:daemon:/sbin:/sbin/login
....
Vcsa:x:69:69:virtual Console Memory Owner:/dev:/sbin/nologin
saslauth:x:499:76: "SASLAUTHD user":/var/empty/saslauth:/sbin/nologin
Postfix:x:89:89::/var/spool/postfix:/sbin/nologin
$
Change the/sbin/nologin from 1 to 10 to login.
[[Email protected] ~]# sed ' 1,10s/\/sbin\/nologin/login/g ' 1.txt
[[Email protected] ~]# sed ' 1,10s#/sbin/nologin#login#g ' 1.txt
[[Email protected] ~]# sed ' 1,[email protected]/sbin/[email protected]@g ' 1.txt
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:login
Daemon:x:2:2:daemon:/sbin:login
......
Vcsa:x:69:69:virtual Console Memory Owner:/dev:/sbin/nologin
saslauth:x:499:76: "SASLAUTHD user":/var/empty/saslauth:/sbin/nologin
Postfix:x:89:89::/var/spool/postfix:/sbin/nologin
.......
$
Change all the lines to login
[[Email protected] ~]# sed ' s#^.*$ #login #g ' 1.txt//////s means that the substitution does not specify the number of rows that are the beginning of all rows//////^. * is any arbitrary character $ end meaning is any arbitrary character from the beginning to the end, or
Login
Login
Login
.....
$
Add a login after each line
[[Email protected] ~]# sed ' s#^.*$#&login#g ' 1.txt
Root:x:0:0:root:/root:/bin/bashlogin
Bin:x:1:1:bin:/bin:/sbin/nologinlogin
Daemon:x:2:2:daemon:/sbin:/sbin/nologinlogin
......
Add a space after each line and add login
[[Email protected] ~]# sed ' s#^.*$#& login#g ' 1.txt
Root:x:0:0:root:/root:/bin/bash Login
Bin:x:1:1:bin:/bin:/sbin/nologin Login
Daemon:x:2:2:daemon:/sbin:/sbin/nologin Login
......
Erase every line so the number
[[Email protected] ~]# sed ' s#[0-9]# #g ' 1.txt/////replaced with a space will be equal to delete
Root:x:::root:/root:/bin/bash
Bin:x:::bin:/bin:/sbin/nologin
DAEMON:X:::d aemon:/sbin:/sbin/nologin
......
Delete the letters of each line
[Email protected] ~]# sed ' s#[a-z]# #g ' 1.txt
:: 0:0::/://
:: 1:1::/://
:: 2:2::/://
......
To delete a line of digital letters
[[Email protected] ~]# sed ' s#[^0-9a-za-z]# #g ' 1.txt////Reverse selection
Rootx00rootrootbinbash
Binx11binbinsbinnologin
Daemonx22daemonsbinsbinnologin
......
Take the first paragraph with a colon as a delimiter to change the position of the last section
[Email protected] ~]# sed-r ' ^.*$# (^[a-z0-9]:) (. *:) (. *$) #\3\2\1#g ' 1.txt (Error)
[[email protected] ~]# sed-r ' s# (^[a-z0-9]+) (:. *:) (. *$) #\3\2\1#g ' 1.txt/////s is the meaning of the substitution (^[a-z0-9]+) with letters and numbers preceded by 1 or any of the + The character preceding the number (:. *:) begins with a colon, with a colon ending with any arbitrary character (. *$) Any character after the last colon, to the end of the \3\2\1 meaning to swap 3 and 1 positions
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
.....
Search for lines containing the keyword root and aming, with two keywords displayed two times in a row
[Email protected] ~]# sed-rn '/user/p;/root/p ' 1.txt
Root:user X:0:0:root:/root:/bin/bash
Root:user X:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
saslauth:x:499:76: "SASLAUTHD user":/var/empty/saslauth:/sbin/nologin
Or search for a line containing the keyword root and aming, one line with two keywords to display once
[Email protected] ~]# sed-n-R '/user|root/p ' 1.txt
Root:user X:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
saslauth:x:499:76: "SASLAUTHD user":/var/empty/saslauth:/sbin/nologin
/////////////////////////////////////////////////////////////////////////////////////////////
Summary: Replacement of the SED specifies the number of lines to replace/specify substituted characters/specify new characters/global substitution plus g replace the first occurrence substituted character
Shell Getting Started-sed-2 replace function