Use of SED commands
-N: Option does not print all lines of the file
[[email protected] ~]# sed ' 1 ' p 1 //without the-n option, sed prints the matching rows, root:x:0:0:root:/ The line root required by the root:/bin/bash //: x:0:0:root:/root:/bin/bash and print the entire text file bin:x:1:1:bin:/bin:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/synchalt:x:7:0:halt:/sbin:/ Sbin/haltuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/roo*:/sbin/nologinphp-fpm:x : 502:502::/home/php-fpm:/sbin/nologinrpc:x:32:32:rpcbind daemon:/var/cache/rpcbind:/sbin/nologinrpcuser:x :29:29:rpc service user:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:anonymous nfs User:/var/lib/nfs:/sbin/nologinnginx:x:495:492:nginx web server:/var/lib/nginx:/sbin/nologinntp:x : 38:38::/etc/ntp:/sbin/nologinnagios:x:494:491::/var/spool/nagios:/sbin/nologinnrpe:x:493:490:nrpe user for the nrpe service:/var/run/nrpe:/sbin/nologinzabbix:x:492:489:zabbix monitoring system:/var/lib/ zabbix:/sbin/nologinroot[[email protected] ~]# sed -n ' 1 ' p 1 After //plus-n, only the matching rows are printed root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# sed-n ' 3,6 ' P 1//print out a file of 3, 6 lines sync:x:5:0:sync:/sbin:/bin/synchalt:x:7:0:halt:/sbin:/sbin/h Altuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/roo*:/sbin/nologin[[email protected ] ~]#[[email protected] ~]# sed-n '/root/' P 1//print out rows containing root root:x:0:0:root:/root:/bin/bashroot[[email protected] ~ ]#
-E option: To perform multiple editing commands
[[email protected] ~]# sed -n -e '/ root/' p -e '/nginx/' p 1 //is equivalent to executing two commands root:x:0:0:root:/root:/bin/bashnginx:x:495:492:nginx web server:/var/ lib/nginx:/sbin/nologinroot[roo[email protected] ~]# sed -n '/root/' p 1 ; sed -n '/nginx/' p 1 // The result of the two command is the same as the result of the above plus-e root:x:0:0:root:/root:/bin/bashrootnginx:x:495:492:nginx web server:/var/ Lib/nginx:/sbin/nologin
[[Email protected] ~]# sed '/root/a\wang ' 1//After ROOT Chase plus Wang Root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbi N/nologinzabbix:x:492:489:zabbix Monitoring System:/var/lib/zabbix:/sbin/nologinroot ROOTwang
-F option: only works if the SED script is invoked
[[email protected] ~]# vim 2.sed //Creating an SED script #!/bin/sed -f // Plus-F option: /root/a\ //Here the letter a means to add. The letter I is inserted, the letter C modifies the contents of the text usrlocal.~~[[email protected] ~]# chmod u+x 2.sed //Add Execute Permissions [[email protected] ~]# ./2.sed 1 //executes the script file, followed by the file to be executed root:x:0:0:root:/ root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/synchalt:x:7:0:halt:/sbin:/sbin/ Haltuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/roo*:/sbin/nologinzabbix:x : 492:489:zabbix monitoring system:/var/lib/zabbix:/sbin/nologinroot rootusr //added content local.
Sed text positioning
$: Represents the last line in the SED command
[[email protected] ~]# sed-n ' $p ' 1 root[[email protected] ~]# sed-n ' 1,16! ' P 1//Text total 17 lines, ' x,y! ' Represents a row ROOT that matches no longer between this range
Match a keyword with a line number
[[email protected] ~]# sed-n '/ntp/,$ ' P 1//print the line from match to NTP to the last line ntp:x:38:38::/etc/ntp:/sbin/nologinnagios:x:494:491:: /var/spool/nagios:/sbin/nologinnrpe:x:493:490:nrpe user for the NRPE service:/var/run/nrpe:/sbin/nologinzabbix:x : 492:489:zabbix monitoring system:/var/lib/zabbix:/sbin/nologinroot root[[email protected] ~]#
SED represents the replacement file:
s/replaced character/new character/[option] {option g indicates all occurrences of the substituted string in the replacement text; p means that only the replacement character is printed, and the-n is combined; W means that the output is directed to a file}
[[email protected] ~]# sed -n ' S/ROOT/root /P ' 1 //replace root in file with root root[[email protected] ~]#[[email protected] ~]# touch 22 //Create a file with 3 root [[email protected] ~]# vim 22 1 root written in the file wang root 2 root 3 root 4~
[[email protected] ~]# sed -n ' s/root/user/p ' 22 //so just replace the first character of each line with the one that matches user wwang rootuseruser[[email protected] ~]# sed -n ' s/root/user/g ' p 22 //Replace all matching characters user wwang user useruser[[email protected] ~]#[[ email protected] ~]# sed -n ' s/root/user/w 33 ' 22 //Plus w means that the modified file is written to another file, and SED automatically creates the file if it does not exist [[email protected] ~]# cat 33user wwang rootuseruser[[email protected] ~]#[[email protected] ~]# sed ' 5 q ' 1 //match to the first 5 lines, then exit root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/synchalt:x:7:0 : Halt:/sbin:/sbin/haltuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin[[email protected] ~]#
[[email protected] ~]# sed-n-E '/root/= ' 1//print match to the line number where the keyword is located 116[[email protected] ~]# sed-n-E '/root/= '-e '/root/' P 1//print lines and line numbers matching to keywords 1root:x:0:0:root:/root:/bin/bash16root[[email protected] ~]# sed-n '/root/{p;=} ' 1// Add curly braces To specify the command group to execute on the row again Root:x:0:0:root:/root:/bin/bash1root16[[email protected] ~]#
This article is from the "Custom" blog, so be sure to keep this source http://zidingyi.blog.51cto.com/10735263/1710704
Use of SED commands