1. On sed
The grep tool function can only realize the search, and cannot replace the searched content.
sed itself is a pipeline command, which is mainly processed in behavioral units. It can search, delete, replace characters or strings in data lines, exchange string positions , directly modify file content and other specific tasks, and operate on document lines. For example .txt.
[root@davery ~]# mkdir sed
[root@davery ~]# cd sed
[root@davery sed]# cp ../grep/passwd 0.txt
[root@davery sed]#
[root@davery sed]# pwd
/root/sed
[root@davery sed]# ls
0.txt
[root@davery sed]#
Find: sed -n '/keywords/'p filename
[root@davery sed]# sed -n '2'p 0.txt displays the second line
[root@davery sed]# sed '15,$'p 0.txt display 15 lines to the last line
[root@davery sed]# sed -n '1,$'p 0.txt show all
[root@davery sed]#sed -n '/^1/'p 0.txt
[root@davery sed]#sed -n '/in$/'p 0.txt
[root@davery sed]#sed -n '/r..o/'p 0.txt
[root@davery sed]#sed -n '/oo*/'p 0.txt
[root@davery sed]#sed -n '/oo*/'Ip 0.txt Add uppercase I, case insensitive
Delete: sed -n 'keyword' p file name, just list the lines that are not deleted on the screen, in fact, the original file has not been deleted.
[root@davery sed]#sed -n '5'p 0.txt
[root@davery sed]#sed -n '1,5'p 0.txt
[root@davery sed]#sed -n '1,$'p 0.txt
[root@davery sed]#sed -n '/root/'p 0.txt
[root@davery sed]#sed -n '/^1/'p 0.txt
[root@davery sed]#sed -n '/in$/'p 0.txt
[root@davery sed]#sed -n '/r..o/'p 0.txt
[root@davery sed]#sed -n '/oo*/'p 0.txt
Replacement: s represents the replacement action, g represents the global replacement of this line, in addition to using / as a separator, special characters #, @ can also be used
[root@davery sed]# sed '1,2s/ot/to/g' 0.txt The first and second lines, replace ot with to
[root@davery sed]# sed -r '1,2s/ot+/to/g' 0.txt
[root@davery sed]# sed 's#ot#to#g' 0.txt
[root@davery sed]# sed 's@ot@to@g' 0.txt
[root@davery sed]# sed 's/[0-9]//g' 0.txt
[root@davery sed]# sed 's/[a-zA-Z]//g' 0.txt
Swap two character positions
[root@davery sed]# sed 's/\(root\)\(.*\)\(bash\)/\3\2\1/' 0.txt
[root@davery sed]# sed -r 's/(root)(.*)(bash)/\3\2\1/' 0.txt Adding -r expression is clearer
bash:x:0:0:root:/root:/bin/root
[root@davery sed]# sed -r 's/(uaer)(.*)(bash)/\3\2\1/' 0.txt
bash1:x:1001:1004::/home/uaer1:/bin/uaer
[root@davery sed]# sed 's/^.*$/123&/' 0.txt adds 123 to the front
123root:x:0:0:root:/root:/bin/bash
123bin:x:1:1:bin:/bin:/sbin/NOLOGIN
Modify the file content directly
[root@davery sed]# sed -i 's/root/toor/g' 0.txt
[root@davery sed]# cat 0.txt
toor:x:0:0:toor:/toor:/bin/bash
example
[root@davery sed]# sed '/root/'p 0.txt will show all lines
root:x:0:0:root:/root:/bin/bash
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
sync:x:5:0:sync:/sbin:/bin/sync
[root@davery sed]# sed -n '/root/'p 0.txt only displays the keyword target line
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator: /root:/sbin/nologin
[root@davery sed]# sed -nr '/root|bus/'p 0.txt 加入r
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@davery sed]# sed -nr '/o+t/'p 0.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
[root@davery sed]# sed -nr '/o{2}/'p 0.txt
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin