{"id":93457,"date":"2024-04-13T10:50:11","date_gmt":"2024-04-13T02:50:11","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=93457"},"modified":"2024-04-13T10:50:11","modified_gmt":"2024-04-13T02:50:11","slug":"sed-tools-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sed-tools-linux.html","title":{"rendered":"sed\u547d\u4ee4\u5de5\u5177\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nsed\u662f\u4e00\u79cd\u6d41\u7f16\u8f91\u5668\uff0c\u5b83\u662f\u6587\u672c\u5904\u7406\u4e2d\u975e\u5e38\u4e2d\u7684\u5de5\u5177\uff0c\u80fd\u591f\u5b8c\u7f8e\u7684\u914d\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528\uff0c\u529f\u80fd\u4e0d\u540c\u51e1\u54cd\u3002\u5904\u7406\u65f6\uff0c\u628a\u5f53\u524d\u5904\u7406\u7684\u884c\u5b58\u50a8\u5728\u4e34\u65f6\u7f13\u51b2\u533a\u4e2d\uff0c\u79f0\u4e3a\u201c\u6a21\u5f0f\u7a7a\u95f4\u201d\uff08pattern space\uff09\uff0c\u63a5\u7740\u7528sed\u547d\u4ee4\u5904\u7406\u7f13\u51b2\u533a\u4e2d\u7684\u5185\u5bb9\uff0c\u5904\u7406\u5b8c\u6210\u540e\uff0c\u628a\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u9001\u5f80\u5c4f\u5e55\u3002\u63a5\u7740\u5904\u7406\u4e0b\u4e00\u884c\uff0c\u8fd9\u6837\u4e0d\u65ad\u91cd\u590d\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u3002\u6587\u4ef6\u5185\u5bb9\u5e76\u6ca1\u6709 \u6539\u53d8\uff0c\u9664\u975e\u4f60\u4f7f\u7528\u91cd\u5b9a\u5411\u5b58\u50a8\u8f93\u51fa\u3002Sed\u4e3b\u8981\u7528\u6765\u81ea\u52a8\u7f16\u8f91\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\uff1b\u7b80\u5316\u5bf9\u6587\u4ef6\u7684\u53cd\u590d\u64cd\u4f5c\uff1b\u7f16\u5199\u8f6c\u6362\u7a0b\u5e8f\u7b49\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
sed\u7528\u6cd5<\/strong><\/div>\n

\u4f7f\u7528sed\u53ef\u4ee5\u5b9e\u73b0grep\u7684\u5927\u90e8\u5206\u529f\u80fd\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u67e5\u627e\u66ff\u6362\u3002<\/p><\/blockquote>\n

\u8bed\u6cd5\uff1a<\/strong><\/span><\/div>\n

sed [options] 'command' filename<\/p>\n

sed [options] -f scriptfile filename<\/p>\n

Options\uff1a<\/strong><\/span><\/div>\n

-n\uff1a-n\u9009\u9879\u548cp\u547d\u4ee4\u4e00\u8d77\u4f7f\u7528\u8868\u793a\u6253\u5370<\/p>\n

-e\uff1a\u7b49\u4ef7\u4e8e--expression\uff0c\u591a\u70b9\u7f16\u8f91\uff0c\u5728\u4e00\u884c\u6267\u884c\u591a\u6761\u547d\u4ee4<\/p>\n

\u7528sed\u5b9e\u73b0\u5339\u914d\uff08grep\uff09\u529f\u80fd=\u6253\u5370\u6709root\u5b57\u7b26\u4e32\u7684\u884c<\/p>\n

[root@cham2<\/span> sed]# sed -n '\/root\/'p test.txt <\/span>\r\nroot:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\noperator:<\/span>x:<\/span>11<\/span>:<\/span>0<\/span>:operator<\/span>:\/root<\/span>:\/sbin\/nologin<\/span>\r\n\r\n[root@cham2<\/span> sed]# sed -nr '\/r+o\/'p test.txt <\/span>\r\nroot:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\noperator:<\/span>x:<\/span>11<\/span>:<\/span>0<\/span>:operator<\/span>:\/root<\/span>:\/sbin\/nologin<\/span>\r\nsystemd-bus-proxy:<\/span>x:<\/span>999<\/span>:<\/span>997<\/span>:systemd<\/span> Bus Proxy:<\/span>\/:\/sbin\/nologin<\/span>\r\ntss:<\/span>x:<\/span>59<\/span>:<\/span>59<\/span>:Account<\/span> used by the trousers package to sandbox the tcsd daemon:<\/span>\/dev\/null:<\/span>\/sbin\/nologin\r\nchrony:<\/span>x:<\/span>997<\/span>:<\/span>995<\/span>:<\/span>:\/var\/lib\/chrony<\/span>:\/sbin\/nologin<\/span>\r\n\r\n[root@cham2<\/span> sed]# sed -nr '\/o*t\/'p test.txt <\/span>\r\nroot:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\nshutdown:<\/span>x:<\/span>6<\/span>:<\/span>0<\/span>:shutdown<\/span>:\/sbin<\/span>:\/sbin\/shutdown<\/span>\r\nhalt:<\/span>x:<\/span>7<\/span>:<\/span>0<\/span>:halo1t<\/span>:\/sbin<\/span>:\/sbin\/halt<\/span>\r\noperator:<\/span>x:<\/span>11<\/span>:<\/span>0<\/span>:operator<\/span>:\/root<\/span>:\/sbin\/nologin<\/span>\r\nftp:<\/span>x:<\/span>14<\/span>:<\/span>50<\/span>:FTP<\/span> User:<\/span>\/var\/ftp:<\/span>\/sbin\/nologin\r\nsystemd-bus-proxy:<\/span>x:<\/span>999<\/span>:<\/span>997<\/span>:systemd<\/span> Bus Proxy:<\/span>\/:\/sbin\/nologin<\/span>\r\nsystemd-network:<\/span>x:<\/span>192<\/span>:<\/span>192<\/span>:systemd<\/span> Network Management:<\/span>\/:\/sbin\/nologin<\/span>\r\ndbus:<\/span>x:<\/span>81<\/span>:<\/span>81<\/span>:System<\/span> message bus:<\/span>\/:\/sbin\/nologin<\/span>\r\npolkitd:<\/span>x:<\/span>998<\/span>:<\/span>996<\/span>:User<\/span> for<\/span> polkitd:<\/span>\/:\/sbin\/nologin<\/span>\r\ntss:<\/span>x:<\/span>59<\/span>:<\/span>59<\/span>:Account<\/span> used by the trousers package to sandbox the tcsd daemon:<\/span>\/dev\/null:<\/span>\/sbin\/nologin\r\npostfix:<\/span>x:<\/span>89<\/span>:<\/span>89<\/span>:<\/span>:\/var\/spool\/postfix<\/span>:\/sbin\/nologin<\/span>\r\nsshd:<\/span>x:<\/span>74<\/span>:<\/span>74<\/span>:Privilege-separated<\/span> SSH:<\/span>\/var\/empty\/sshd:<\/span>\/sbin\/nologin\r\n\r\n<\/pre>\n

\u5ffd\u7565\u5927\u5c0f\u5199\uff08I\uff09<\/strong><\/p>\n

[root@cham2<\/span> sed]# sed -n '\/nologin\/'Ip test.txt <\/span>\r\nbin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\ndaemon:<\/span>x:<\/span>2<\/span>:<\/span>2<\/span>:daemon<\/span>:\/sbin<\/span>:\/sbin\/nologin<\/span>\r\nad11om:<\/span>x:<\/span>3<\/span>:<\/span>4<\/span>:ad1om<\/span>:\/var\/adm<\/span>:\/sbin\/nologin<\/span>\r\nlp:<\/span>x:<\/span>4<\/span>:<\/span>7<\/span>:lp<\/span>:\/var\/spoool\/lpd<\/span>:\/sbin\/nologin<\/span>\r\nmail:<\/span>x:<\/span>8<\/span>:<\/span>12<\/span>:mail<\/span>:\/var\/spoooooool\/mail<\/span>:\/sbin\/nologin<\/span>\r\noperator:<\/span>x:<\/span>11<\/span>:<\/span>0<\/span>:operator<\/span>:\/root<\/span>:\/sbin\/nologin<\/span>\r\ngames:<\/span>x:<\/span>12<\/span>:<\/span>100<\/span>:games<\/span>:\/usr\/games<\/span>:\/sbin\/nologin<\/span>\r\nftp:<\/span>x:<\/span>14<\/span>:<\/span>50<\/span>:FTP<\/span> User:<\/span>\/var\/ftp:<\/span>\/sbin\/nologin\r\nnobody:<\/span>x:<\/span>99<\/span>:<\/span>99<\/span>:Nobody<\/span>:\/<\/span>:\/sbin\/nologin<\/span>\r\nsystemd-bus-proxy:<\/span>x:<\/span>999<\/span>:<\/span>997<\/span>:systemd<\/span> Bus Proxy:<\/span>\/:\/sbin\/nologin<\/span>\r\nsystemd-network:<\/span>x:<\/span>192<\/span>:<\/span>192<\/span>:systemd<\/span> Network Management:<\/span>\/:\/sbin\/nologin<\/span>\r\ndbus:<\/span>x:<\/span>81<\/span>:<\/span>81<\/span>:System<\/span> message bus:<\/span>\/:\/sbin\/nologin<\/span>\r\npolkitd:<\/span>x:<\/span>998<\/span>:<\/span>996<\/span>:User<\/span> for<\/span> polkitd:<\/span>\/:\/sbin\/nologin<\/span>\r\ntss:<\/span>x:<\/span>59<\/span>:<\/span>59<\/span>:Account<\/span> used by the trousers package to sandbox the tcsd daemon:<\/span>\/dev\/null:<\/span>\/sbin\/nologin\r\npostfix:<\/span>x:<\/span>89<\/span>:<\/span>89<\/span>:<\/span>:\/var\/spool\/postfix<\/span>:\/sbin\/nologin<\/span>\r\nsshd:<\/span>x:<\/span>74<\/span>:<\/span>74<\/span>:Privilege-separated<\/span> SSH:<\/span>\/var\/empty\/sshd:<\/span>\/sbin\/nologin\r\nchrony:<\/span>x:<\/span>997<\/span>:<\/span>995<\/span>:<\/span>:\/var\/lib\/chrony<\/span>:\/sbin\/nologin<\/span>\r\n<\/pre>\n

\u6253\u5370\u6307\u5b9a\u884c(p)<\/strong><\/p>\n

[root@cham2<\/span> sed]# sed -n '2'p test.txt<\/span>\r\nbin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\n\r\n[root@cham2<\/span> sed]# sed -n '2,5'p test.txt<\/span>\r\nbin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\ndfsdfdsfsdfs:<\/span>1241124<\/span>:jksdfhdskjf<\/span>:sfdsf<\/span>:rao<\/span>:<\/span>325345345<\/span>\r\n222222<\/span>:r<\/span>.o:<\/span>dfdsfdsg\r\ndaemon:<\/span>x:<\/span>2<\/span>:<\/span>2<\/span>:daemon<\/span>:\/sbin<\/span>:\/sbin\/nologin<\/span>\r\n\r\n[root@cham2<\/span> sed]# sed -n '15,$'p test.txt<\/span>\r\nnobody:<\/span>x:<\/span>99<\/span>:<\/span>99<\/span>:Nobody<\/span>:\/<\/span>:\/sbin\/nologin<\/span>\r\nsystemd-bus-proxy:<\/span>x:<\/span>999<\/span>:<\/span>997<\/span>:systemd<\/span> Bus Proxy:<\/span>\/:\/sbin\/nologin<\/span>\r\nsystemd-network:<\/span>x:<\/span>192<\/span>:<\/span>192<\/span>:systemd<\/span> Network Management:<\/span>\/:\/sbin\/nologin<\/span>\r\ndbus:<\/span>x:<\/span>81<\/span>:<\/span>81<\/span>:System<\/span> message bus:<\/span>\/:\/sbin\/nologin<\/span>\r\npolkitd:<\/span>x:<\/span>998<\/span>:<\/span>996<\/span>:User<\/span> for<\/span> polkitd:<\/span>\/:\/sbin\/nologin<\/span>\r\ntss:<\/span>x:<\/span>59<\/span>:<\/span>59<\/span>:Account<\/span> used by the trousers package to sandbox the tcsd daemon:<\/span>\/dev\/null:<\/span>\/sbin\/nologin\r\npostfix:<\/span>x:<\/span>89<\/span>:<\/span>89<\/span>:<\/span>:\/var\/spool\/postfix<\/span>:\/sbin\/nologin<\/span>\r\nsshd:<\/span>x:<\/span>74<\/span>:<\/span>74<\/span>:Privilege-separated<\/span> SSH:<\/span>\/var\/empty\/sshd:<\/span>\/sbin\/nologin\r\nchrony:<\/span>x:<\/span>997<\/span>:<\/span>995<\/span>:<\/span>:\/var\/lib\/chrony<\/span>:\/sbin\/nologin<\/span>\r\n<\/pre>\n

-e\u9009\u9879\u5141\u8bb8\u5728\u540c\u4e00\u884c\u91cc\u6267\u884c\u591a\u6761\u547d\u4ee4(\u628a\u7b2c\u4e00\u884c\u6253\u5370\u51fa\u6765\u5e76\u5339\u914dbus)<\/strong><\/p>\n

[root@cham2<\/span> sed]# sed -e '1'p -e '\/bus\/'p -n test.txt<\/span>\r\nroot:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\nsystemd-bus-proxy:<\/span>x:<\/span>999<\/span>:<\/span>997<\/span>:systemd<\/span> Bus Proxy:<\/span>\/:\/sbin\/nologin<\/span>\r\ndbus:<\/span>x:<\/span>81<\/span>:<\/span>81<\/span>:System<\/span> message bus:<\/span>\/:\/sbin\/nologin<\/span>\r\n\r\n[root@cham2<\/span> sed]# sed -e '1'p -e '\/root\/'p -n test.txt <\/span>\r\nroot:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\nroot:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\noperator:<\/span>x:<\/span>11<\/span>:<\/span>0<\/span>:operator<\/span>:\/root<\/span>:\/sbin\/nologin<\/span><\/pre>\n

\u8bf4\u660e\u00a0\u201csed -e '1'p -e '\/bus\/'p -n test.txt\u201d\u8be5\u547d\u4ee4\u7684\u610f\u601d\u662f\uff1a\u6253\u5370\u7b2c\u4e00\u884c\uff0c\u540c\u65f6\u4e5f\u6253\u5370\u51fa\u5339\u914d\u6709\u2018bus\u2019\u7684\u884c\u3002\u5f53\u6253\u5370\u7684\u5185\u5bb9\u548c\u5339\u914d\u7684\u5185\u5bb9\u76f8\u540c\u65f6\uff0c\u4f1a\u5206\u522b\u663e\u793a\uff0c\u5373\uff1a\u4e24\u4e2a\u547d\u4ee4\u540c\u65f6\u8fdb\u884c\u4f46\u76f8\u5bf9\u72ec\u7acb\u3002<\/p>\n

\u5220\u9664\u6307\u5b9a\u884c(d)<\/strong><\/p>\n

[root@cham2<\/span> sed]# sed '1,10'd test.txt<\/span>\r\nmail:<\/span>x:<\/span>8<\/span>:<\/span>12<\/span>:mail<\/span>:\/var\/spoooooool\/mail<\/span>:\/sbin\/nologin<\/span>\r\noperator:<\/span>x:<\/span>11<\/span>:<\/span>0<\/span>:operator<\/span>:\/root<\/span>:\/sbin\/nologin<\/span>\r\ngames:<\/span>x:<\/span>12<\/span>:<\/span>100<\/span>:games<\/span>:\/usr\/games<\/span>:\/sbin\/nologin<\/span>\r\nftp:<\/span>x:<\/span>14<\/span>:<\/span>50<\/span>:FTP<\/span> User:<\/span>\/var\/ftp:<\/span>\/sbin\/nologin\r\nnobody:<\/span>x:<\/span>99<\/span>:<\/span>99<\/span>:Nobody<\/span>:\/<\/span>:\/sbin\/nologin<\/span>\r\nsystemd-bus-proxy:<\/span>x:<\/span>999<\/span>:<\/span>997<\/span>:systemd<\/span> Bus Proxy:<\/span>\/:\/sbin\/nologin<\/span>\r\nsystemd-network:<\/span>x:<\/span>192<\/span>:<\/span>192<\/span>:systemd<\/span> Network Management:<\/span>\/:\/sbin\/nologin<\/span>\r\ndbus:<\/span>x:<\/span>81<\/span>:<\/span>81<\/span>:System<\/span> message bus:<\/span>\/:\/sbin\/nologin<\/span>\r\npolkitd:<\/span>x:<\/span>998<\/span>:<\/span>996<\/span>:User<\/span> for<\/span> polkitd:<\/span>\/:\/sbin\/nologin<\/span>\r\ntss:<\/span>x:<\/span>59<\/span>:<\/span>59<\/span>:Account<\/span> used by the trousers package to sandbox the tcsd daemon:<\/span>\/dev\/null:<\/span>\/sbin\/nologin\r\npostfix:<\/span>x:<\/span>89<\/span>:<\/span>89<\/span>:<\/span>:\/var\/spool\/postfix<\/span>:\/sbin\/nologin<\/span>\r\nsshd:<\/span>x:<\/span>74<\/span>:<\/span>74<\/span>:Privilege-separated<\/span> SSH:<\/span>\/var\/empty\/sshd:<\/span>\/sbin\/nologin\r\nchrony:<\/span>x:<\/span>997<\/span>:<\/span>995<\/span>:<\/span>:\/var\/lib\/chrony<\/span>:\/sbin\/nologin<\/span>\r\n\r\n[root@cham2<\/span> sed]# sed '1,18'd test.txt<\/span>\r\npolkitd:<\/span>x:<\/span>998<\/span>:<\/span>996<\/span>:User<\/span> for<\/span> polkitd:<\/span>\/:\/sbin\/nologin<\/span>\r\ntss:<\/span>x:<\/span>59<\/span>:<\/span>59<\/span>:Account<\/span> used by the trousers package to sandbox the tcsd daemon:<\/span>\/dev\/null:<\/span>\/sbin\/nologin\r\npostfix:<\/span>x:<\/span>89<\/span>:<\/span>89<\/span>:<\/span>:\/var\/spool\/postfix<\/span>:\/sbin\/nologin<\/span>\r\nsshd:<\/span>x:<\/span>74<\/span>:<\/span>74<\/span>:Privilege-separated<\/span> SSH:<\/span>\/var\/empty\/sshd:<\/span>\/sbin\/nologin\r\nchrony:<\/span>x:<\/span>997<\/span>:<\/span>995<\/span>:<\/span>:\/var\/lib\/chrony<\/span>:\/sbin\/nologin<\/span>\r\n<\/pre>\n

\u6ce8\uff1a<\/strong>\u00a0\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u5220\u9664\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u77e5\u8bc6\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u7684\u5185\u5bb9\u5220\u9664\u4e86\u6307\u5b9a\u884c\u3002\u90a3\u4e48\u5982\u4f55\u76f4\u63a5\u5220\u9664\u6587\u4ef6\u4e2d\u6307\u5b9a\u7684\u5185\u5bb9\u5462\uff1f\u5982\u4e0b\uff1a<\/p>\n

[root@cham2<\/span> sed]# wc -l test.txt<\/span>\r\n23<\/span> test.txt\r\n[root@cham2<\/span> sed]# sed -i '1,20'd test.txt<\/span>\r\n[root@cham2<\/span> sed]# wc -l test.txt<\/span>\r\n3<\/span> test.txt\r\n[root@cham2<\/span> sed]# cat test.txt<\/span>\r\npostfix:<\/span>x:<\/span>89<\/span>:<\/span>89<\/span>:<\/span>:\/var\/spool\/postfix<\/span>:\/sbin\/nologin<\/span>\r\nsshd:<\/span>x:<\/span>74<\/span>:<\/span>74<\/span>:Privilege-separated<\/span> SSH:<\/span>\/var\/empty\/sshd:<\/span>\/sbin\/nologin\r\nchrony:<\/span>x:<\/span>997<\/span>:<\/span>995<\/span>:<\/span>:\/var\/lib\/chrony<\/span>:\/sbin\/nologin<\/span>\r\n[root@cham2<\/span> sed]# sed -i '\/SSH\/'d test.txt<\/span>\r\n[root@cham2<\/span> sed]# cat test.txt<\/span>\r\npostfix:<\/span>x:<\/span>89<\/span>:<\/span>89<\/span>:<\/span>:\/var\/spool\/postfix<\/span>:\/sbin\/nologin<\/span>\r\nchrony:<\/span>x:<\/span>997<\/span>:<\/span>995<\/span>:<\/span>:\/var\/lib\/chrony<\/span>:\/sbin\/nologin<\/span>\r\n<\/pre>\n

\u67e5\u627e\u66ff\u6362\uff08s\uff09 \u7c7b\u4f3c\u4e8evim\u4e2d\u7684\u67e5\u627e\u66ff\u6362<\/p>\n

[root@cham2<\/span> sed]# sed '1,10s\/root\/toor\/g' test.txt<\/span>\r\ntoor:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:toor<\/span>:\/toor<\/span>:\/bin\/bash<\/span>\r\nbin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\ndfsdfdsfsdfs:<\/span>1241124<\/span>:jksdfhdskjf<\/span>:sfdsf<\/span>:rao<\/span>:<\/span>325345345<\/span>\r\n222222<\/span>:r<\/span>.o:<\/span>dfdsfdsg\r\ndaemon:<\/span>x:<\/span>2<\/span>:<\/span>2<\/span>:daemon<\/span>:\/sbin<\/span>:\/sbin\/nologin<\/span>\r\nad11om:<\/span>x:<\/span>3<\/span>:<\/span>4<\/span>:ad1om<\/span>:\/var\/adm<\/span>:\/sbin\/nologin<\/span>\r\nlp:<\/span>x:<\/span>4<\/span>:<\/span>7<\/span>:lp<\/span>:\/var\/spoool\/lpd<\/span>:\/sbin\/nologin<\/span>\r\nsync:<\/span>x:<\/span>5<\/span>:<\/span>0<\/span>:sync<\/span>:\/sbin<\/span>:\/bin\/sync<\/span>\r\nshutdown:<\/span>x:<\/span>6<\/span>:<\/span>0<\/span>:shutdown<\/span>:\/sbin<\/span>:\/sbin\/shutdown<\/span>\r\nhalt:<\/span>x:<\/span>7<\/span>:<\/span>0<\/span>:halo1t<\/span>:\/sbin<\/span>:\/sbin\/halt<\/span>\r\nmail:<\/span>x:<\/span>8<\/span>:<\/span>12<\/span>:mail<\/span>:\/var\/spoooooool\/mail<\/span>:\/sbin\/nologin<\/span>\r\noperator:<\/span>x:<\/span>11<\/span>:<\/span>0<\/span>:operator<\/span>:\/root<\/span>:\/sbin\/nologin<\/span>\r\ngames:<\/span>x:<\/span>12<\/span>:<\/span>100<\/span>:games<\/span>:\/usr\/games<\/span>:\/sbin\/nologin<\/span>\r\nftp:<\/span>x:<\/span>14<\/span>:<\/span>50<\/span>:FTP<\/span> User:<\/span>\/var\/ftp:<\/span>\/sbin\/nologin\r\nnobody:<\/span>x:<\/span>99<\/span>:<\/span>99<\/span>:Nobody<\/span>:\/<\/span>:\/sbin\/nologin<\/span>\r\nsystemd-bus-proxy:<\/span>x:<\/span>999<\/span>:<\/span>997<\/span>:systemd<\/span> Bus Proxy:<\/span>\/:\/sbin\/nologin<\/span>\r\nsystemd-network:<\/span>x:<\/span>192<\/span>:<\/span>192<\/span>:systemd<\/span> Network Management:<\/span>\/:\/sbin\/nologin<\/span>\r\ndbus:<\/span>x:<\/span>81<\/span>:<\/span>81<\/span>:System<\/span> message bus:<\/span>\/:\/sbin\/nologin<\/span>\r\npolkitd:<\/span>x:<\/span>998<\/span>:<\/span>996<\/span>:User<\/span> for<\/span> polkitd:<\/span>\/:\/sbin\/nologin<\/span>\r\ntss:<\/span>x:<\/span>59<\/span>:<\/span>59<\/span>:Account<\/span> used by the trousers package to sandbox the tcsd daemon:<\/span>\/dev\/null:<\/span>\/sbin\/nologin\r\npostfix:<\/span>x:<\/span>89<\/span>:<\/span>89<\/span>:<\/span>:\/var\/spool\/postfix<\/span>:\/sbin\/nologin<\/span>\r\nsshd:<\/span>x:<\/span>74<\/span>:<\/span>74<\/span>:Privilege-separated<\/span> SSH:<\/span>\/var\/empty\/sshd:<\/span>\/sbin\/nologin\r\nchrony:<\/span>x:<\/span>997<\/span>:<\/span>995<\/span>:<\/span>:\/var\/lib\/chrony<\/span>:\/sbin\/nologin<\/span>\r\n\u6b64\u5904\u5982\u679c\u4e0d\u4f7f\u7528head\u547d\u4ee4\uff0c\u5c06\u4f1a\u663e\u793atest.txt\u5168\u90e8\u5185\u5bb9\u3002\r\n\r\n[root@cham2<\/span> sed]# sed '1,10s\/root\/toor\/g' test.txt | head -3<\/span>\r\ntoor:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:toor<\/span>:\/toor<\/span>:\/bin\/bash<\/span>\r\nbin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\ndfsdfdsfsdfs:<\/span>1241124<\/span>:jksdfhdskjf<\/span>:sfdsf<\/span>:rao<\/span>:<\/span>325345345<\/span>\r\n\r\n[root@cham2<\/span> sed]# cat test.txt | head -3<\/span>\r\nroot:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\nbin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\ndfsdfdsfsdfs:<\/span>1241124<\/span>:jksdfhdskjf<\/span>:sfdsf<\/span>:rao<\/span>:<\/span>325345345<\/span><\/pre>\n

\u66ff\u6362\u6bcf\u884c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a<\/p>\n

[root@cham2<\/span> sed]# head -3 test.txt<\/span>\r\nroot:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\nbin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\ndfsdfdsfsdfs:<\/span>1241124<\/span>:jksdfhdskjf<\/span>:sfdsf<\/span>:rao<\/span>:<\/span>325345345<\/span>\r\n[root@cham2<\/span> sed]# head -3 test.txt |sed -r 's\/([^:]+):(.*):([^:]+)\/\\3:\\2:\\1\/'<\/span>\r\n\/bin\/bash:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:root<\/span>\r\n\/sbin\/NOLOGIN:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:bin<\/span>\r\n325345345<\/span>:<\/span>1241124<\/span>:jksdfhdskjf<\/span>:sfdsf<\/span>:rao<\/span>:dfsdfdsfsdfs<\/span>\r\n<\/pre>\n

\u8bf4\u660e\uff1a<\/strong>\u00a0\u5c0f\u62ec\u53f7\u5185\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u7528\u53cd\u659c\u6760\u8ddf\u6570\u5b57\u7ec4\u5408\u6309\u987a\u5e8f\u6765\u4ee3\u66ff\u3002<\/p>\n

\u5728\u6bcf\u4e00\u884c\u884c\u9996\u6dfb\u52a0\u6307\u5b9a\u5b57\u7b26\uff1a<\/p>\n

[root@cham2<\/span> sed]# head -3 test.txt | sed -r 's\/(.*)\/AB:\\1\/'<\/span>\r\nAB:<\/span>root:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\nAB:<\/span>bin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\nAB:<\/span>dfsdfdsfsdfs:<\/span>1241124<\/span>:jksdfhdskjf<\/span>:sfdsf<\/span>:rao<\/span>:<\/span>325345345<\/span>\r\n\r\n[root@cham2<\/span> sed]# head -3 test.txt | sed -r 's\/(.*)\/AB:&\/'<\/span>\r\nAB:<\/span>root:<\/span>x:<\/span>0<\/span>:<\/span>0<\/span>:root<\/span>:\/root<\/span>:\/bin\/bash<\/span>\r\nAB:<\/span>bin:<\/span>x:<\/span>1<\/span>:<\/span>1<\/span>:bin<\/span>:\/bin<\/span>:\/sbin\/NOLOGIN<\/span>\r\nAB:<\/span>dfsdfdsfsdfs:<\/span>1241124<\/span>:jksdfhdskjf<\/span>:sfdsf<\/span>:rao<\/span>:<\/span>325345345<\/span>\r\n<\/pre>\n

\u8bf4\u660e\uff1a<\/strong>\u00a0\u5728\u6b64&\u4ee3\u8868\u524d\u9762\u62ec\u53f7\u4e2d\u7684\u5185\u5bb9=\u201c\\ 1\u201d<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/my.oschina.net\/u\/3708120\/blog\/1576609<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/sed-tools-linux.html<\/a>\u7f16\u8f91\uff1a\u5f20\u96c4\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb sed\u662f\u4e00\u79cd\u6d41\u7f16\u8f91\u5668\uff0c\u5b83\u662f\u6587\u672c\u5904\u7406\u4e2d\u975e\u5e38\u4e2d\u7684\u5de5\u5177\uff0c\u80fd\u591f\u5b8c\u7f8e\u7684\u914d\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528\uff0c\u529f\u80fd\u4e0d\u540c\u51e1\u54cd\u3002\u5904\u7406\u65f6\uff0c\u628a […]<\/p>\n","protected":false},"author":643,"featured_media":93460,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-93457","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/93457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=93457"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/93457\/revisions"}],"predecessor-version":[{"id":93469,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/93457\/revisions\/93469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/93460"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=93457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=93457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=93457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}