{"id":157757,"date":"2019-10-19T11:17:58","date_gmt":"2019-10-19T03:17:58","guid":{"rendered":"https:\/\/gulass.cn\/?p=157757"},"modified":"2019-09-28T11:19:14","modified_gmt":"2019-09-28T03:19:14","slug":"detailed-description-of-sed","status":"publish","type":"post","link":"https:\/\/gulass.cn\/detailed-description-of-sed.html","title":{"rendered":"Linux\u91cd\u8981\u547d\u4e4bsed\u547d\u4ee4\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSed \u4ee3\u8868\u6d41\u7f16\u8f91\u5668\uff0c\u5e38\u7528\u4e8e Linux \u4e2d\u57fa\u672c\u7684\u6587\u672c\u5904\u7406\u3002sed \u547d\u4ee4\u662f Linux \u4e2d\u7684\u91cd\u8981\u547d\u4ee4\u4e4b\u4e00\uff0c\u5728\u6587\u4ef6\u5904\u7406\u65b9\u9762\u6709\u7740\u91cd\u8981\u4f5c\u7528\u3002\u53ef\u7528\u4e8e\u5220\u9664\u6216\u79fb\u52a8\u4e0e\u7ed9\u5b9a\u6a21\u5f0f\u5339\u914d\u7684\u7279\u5b9a\u884c\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u5982\u4f55\u4f7f\u7528sed\u547d\u4ee4\u5220\u9664\u6587\u4ef6\u4e2d\u7684\u884c
\nSed \u4ee3\u8868\u6d41\u7f16\u8f91\u5668Stream Editor\uff0c\u5e38\u7528\u4e8e Linux \u4e2d\u57fa\u672c\u7684\u6587\u672c\u5904\u7406\u3002sed \u547d\u4ee4\u662f Linux \u4e2d\u7684\u91cd\u8981\u547d\u4ee4\u4e4b\u4e00\uff0c\u5728\u6587\u4ef6\u5904\u7406\u65b9\u9762\u6709\u7740\u91cd\u8981\u4f5c\u7528\u3002\u53ef\u7528\u4e8e\u5220\u9664\u6216\u79fb\u52a8\u4e0e\u7ed9\u5b9a\u6a21\u5f0f\u5339\u914d\u7684\u7279\u5b9a\u884c\u3002<\/p>\n

\u5b83\u8fd8\u53ef\u4ee5\u5220\u9664\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u884c\uff0c\u5b83\u80fd\u591f\u4ece\u6587\u4ef6\u4e2d\u5220\u9664\u8868\u8fbe\u5f0f\uff0c\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5206\u9694\u7b26\uff08\u4f8b\u5982\u9017\u53f7\u3001\u5236\u8868\u7b26\u6216\u7a7a\u683c\uff09\u8fdb\u884c\u6807\u8bc6\u3002<\/p>\n

\u672c\u6587\u5217\u51fa\u4e86 15 \u4e2a\u4f7f\u7528\u8303\u4f8b\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u4f60\u638c\u63e1 sed \u547d\u4ee4\u3002<\/p>\n

\u5982\u679c\u4f60\u80fd\u7406\u89e3\u5e76\u4e14\u8bb0\u4f4f\u8fd9\u4e9b\u547d\u4ee4\uff0c\u5728\u4f60\u9700\u8981\u4f7f\u7528 sed \u65f6\uff0c\u8fd9\u4e9b\u547d\u4ee4\u5c31\u80fd\u6d3e\u4e0a\u7528\u573a\uff0c\u5e2e\u4f60\u8282\u7ea6\u5f88\u591a\u65f6\u95f4\u3002<\/p>\n

\u6ce8\u610f\uff1a\u4e3a\u4e86\u65b9\u4fbf\u6f14\u793a\uff0c\u6211\u5728\u6267\u884c sed \u547d\u4ee4\u65f6\uff0c\u4e0d\u4f7f\u7528 -i \u9009\u9879\uff08\u56e0\u4e3a\u8fd9\u4e2a\u9009\u9879\u4f1a\u76f4\u63a5\u4fee\u6539\u6587\u4ef6\u5185\u5bb9\uff09\uff0c\u88ab\u79fb\u9664\u4e86\u884c\u7684\u6587\u4ef6\u5185\u5bb9\u5c06\u6253\u5370\u5230 Linux \u7ec8\u7aef\u3002<\/p>\n

\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u5728\u5b9e\u9645\u73af\u5883\u4e2d\u4ece\u6e90\u6587\u4ef6\u4e2d\u5220\u9664\u884c\uff0c\u8bf7\u5728 sed \u547d\u4ee4\u4e2d\u4f7f\u7528 -i \u9009\u9879\u3002<\/p>\n

\u6f14\u793a\u4e4b\u524d\uff0c\u6211\u521b\u5efa\u4e86 sed-demo.txt \u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4e86\u4ee5\u4e0b\u5185\u5bb9\u548c\u76f8\u5e94\u884c\u53f7\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u3002<\/p>\n

# cat sed-demo.txt\r\n  Linux Operating System\r\n  Unix Operating System\r\n  RHEL\r\n  Red Hat\r\n  Fedora\r\n  Arch Linux\r\n  CentOS\r\n  Debian\r\n  Ubuntu\r\n  openSUSE\r\n<\/pre>\n
1) \u5982\u4f55\u5220\u9664\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u5220\u9664\u6587\u4ef6\u9996\u884c\u3002<\/p>\n

N \u8868\u793a\u6587\u4ef6\u4e2d\u7684\u7b2c N \u884c\uff0cd \u9009\u9879\u5728 sed \u547d\u4ee4\u4e2d\u7528\u4e8e\u5220\u9664\u4e00\u884c\u3002<\/p>\n

\u8bed\u6cd5\uff1a<\/p>\n

sed 'Nd' file\r\n<\/pre>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 sed-demo.txt \u4e2d\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n

# sed '1d' sed-demo.txt\r\n  Unix Operating System\r\n  RHEL\r\n  Red Hat\r\n  Fedora\r\n  Arch Linux\r\n  CentOS\r\n  Debian\r\n  Ubuntu\r\n  openSUSE\r\n<\/pre>\n
2) \u5982\u4f55\u5220\u9664\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u5220\u9664\u6587\u4ef6\u6700\u540e\u4e00\u884c\u3002<\/p>\n

$ \u7b26\u53f7\u8868\u793a\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u3002<\/p>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 sed-demo.txt \u4e2d\u7684\u6700\u540e\u4e00\u884c\u3002<\/p>\n

# sed '$d' sed-demo.txt\r\n  Linux Operating System\r\n  Unix Operating System\r\n  RHEL\r\n  Red Hat\r\n  Fedora\r\n  Arch Linux\r\n  CentOS\r\n  Debian\r\n  Ubuntu\r\n<\/pre>\n
3) \u5982\u4f55\u5220\u9664\u6307\u5b9a\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 sed-demo.txt \u4e2d\u7684\u7b2c 3 \u884c\u3002<\/p>\n

# sed '3d' sed-demo.txt\r\n  Linux Operating System\r\n  Unix Operating System\r\n  Red Hat\r\n  Fedora\r\n  Arch Linux\r\n  CentOS\r\n  Debian\r\n  Ubuntu\r\n  openSUSE\r\n<\/pre>\n
4) \u5982\u4f55\u5220\u9664\u6307\u5b9a\u8303\u56f4\u5185\u7684\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 sed-demo.txt \u4e2d\u7684\u7b2c 5 \u5230 7 \u884c\u3002<\/p>\n

# sed '5,7d' sed-demo.txt\r\n  Linux Operating System\r\n  Unix Operating System\r\n  RHEL\r\n  Red Hat\r\n  Debian\r\n  Ubuntu\r\n  openSUSE\r\n<\/pre>\n
5) \u5982\u4f55\u5220\u9664\u591a\u884c\u5185\u5bb9\uff1f<\/strong><\/div>\n

sed \u547d\u4ee4\u80fd\u591f\u5220\u9664\u7ed9\u5b9a\u884c\u7684\u96c6\u5408\u3002<\/p>\n

\u672c\u4f8b\u4e2d\uff0c\u4e0b\u9762\u7684 sed \u547d\u4ee4\u5220\u9664\u4e86\u7b2c 1 \u884c\u3001\u7b2c 5 \u884c\u3001\u7b2c 9 \u884c\u548c\u6700\u540e\u4e00\u884c\u3002<\/p>\n

# sed '1d;5d;9d;$d' sed-demo.txt\r\n  Unix Operating System\r\n  RHEL\r\n  Red Hat\r\n  Arch Linux\r\n  CentOS\r\n  Debian\r\n<\/pre>\n
5a) \u5982\u4f55\u5220\u9664\u6307\u5b9a\u8303\u56f4\u4ee5\u5916\u7684\u884c\uff1f<\/strong><\/span><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 sed-demo.txt \u4e2d\u7b2c 3 \u5230 6 \u884c\u8303\u56f4\u4ee5\u5916\u7684\u6240\u6709\u884c\u3002<\/p>\n

# sed '3,6!d' sed-demo.txt\r\n  RHEL\r\n  Red Hat\r\n  Fedora\r\n  Arch Linux\r\n<\/pre>\n
6) \u5982\u4f55\u5220\u9664\u7a7a\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 sed-demo.txt \u4e2d\u7684\u7a7a\u884c\u3002<\/p>\n

# sed '\/^$\/d' sed-demo.txt\r\n  Linux Operating System\r\n  Unix Operating System\r\n  RHEL\r\n  Red Hat\r\n  Fedora\r\n  Arch Linux\r\n  CentOS\r\n  Debian\r\n  Ubuntu\r\n  openSUSE\r\n<\/pre>\n
7) \u5982\u4f55\u5220\u9664\u5305\u542b\u67d0\u4e2a\u6a21\u5f0f\u7684\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 sed-demo.txt \u4e2d\u5339\u914d\u5230 System \u6a21\u5f0f\u7684\u884c\u3002<\/p>\n

# sed '\/System\/d' sed-demo.txt\r\n RHEL\r\n Red Hat\r\n Fedora\r\n Arch Linux\r\n CentOS\r\n Debian\r\n Ubuntu\r\n openSUSE\r\n<\/pre>\n
8) \u5982\u4f55\u5220\u9664\u5305\u542b\u5b57\u7b26\u4e32\u96c6\u5408\u4e2d\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 sed-demo.txt \u4e2d\u5339\u914d\u5230 System \u6216 Linux \u8868\u8fbe\u5f0f\u7684\u884c\u3002<\/p>\n

# sed '\/System\\|Linux\/d' sed-demo.txt\r\n RHEL\r\n Red Hat\r\n Fedora\r\n CentOS\r\n Debian\r\n Ubuntu\r\n openSUSE\r\n<\/pre>\n
9) \u5982\u4f55\u5220\u9664\u4ee5\u6307\u5b9a\u5b57\u7b26\u5f00\u5934\u7684\u884c\uff1f<\/strong><\/div>\n

\u4e3a\u4e86\u6d4b\u8bd5\uff0c\u6211\u521b\u5efa\u4e86 sed-demo-1.txt \u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4e86\u4ee5\u4e0b\u5185\u5bb9\u3002<\/p>\n

# cat sed-demo-1.txt\r\nLinux Operating System\r\nUnix Operating System\r\nRHEL\r\nRed Hat\r\nFedora\r\ndebian\r\nubuntu\r\nArch Linux - 1\r\n2 - Manjaro\r\n3 4 5 6\r\n<\/pre>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664\u4ee5 R \u5b57\u7b26\u5f00\u5934\u7684\u6240\u6709\u884c\u3002<\/p>\n

# sed '\/^R\/d' sed-demo-1.txt\r\nLinux Operating System\r\nUnix Operating System\r\nFedora\r\ndebian\r\nubuntu\r\nArch Linux - 1\r\n2 - Manjaro\r\n3 4 5 6\r\n<\/pre>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 R \u6216\u8005 F \u5b57\u7b26\u5f00\u5934\u7684\u6240\u6709\u884c\u3002<\/p>\n

# sed '\/^[RF]\/d' sed-demo-1.txt\r\nLinux Operating System\r\nUnix Operating System\r\ndebian\r\nubuntu\r\nArch Linux - 1\r\n2 - Manjaro\r\n3 4 5 6\r\n<\/pre>\n
10) \u5982\u4f55\u5220\u9664\u4ee5\u6307\u5b9a\u5b57\u7b26\u7ed3\u5c3e\u7684\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 m \u5b57\u7b26\u7ed3\u5c3e\u7684\u6240\u6709\u884c\u3002<\/p>\n

# sed '\/m$\/d' sed-demo.txt\r\n RHEL\r\n Red Hat\r\n Fedora\r\n Arch Linux\r\n CentOS\r\n Debian\r\n Ubuntu\r\n openSUSE\r\n<\/pre>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664 x \u6216\u8005 m \u5b57\u7b26\u7ed3\u5c3e\u7684\u6240\u6709\u884c\u3002<\/p>\n

# sed '\/[xm]$\/d' sed-demo.txt\r\n RHEL\r\n Red Hat\r\n Fedora\r\n CentOS\r\n Debian\r\n Ubuntu\r\n openSUSE\r\n<\/pre>\n
11) \u5982\u4f55\u5220\u9664\u6240\u6709\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664\u6240\u6709\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u884c\u3002<\/p>\n

# sed '\/^[A-Z]\/d' sed-demo-1.txt\r\ndebian\r\nubuntu\r\n2 - Manjaro\r\n3 4 5 6\r\n<\/pre>\n
12) \u5982\u4f55\u5220\u9664\u6307\u5b9a\u8303\u56f4\u5185\u5339\u914d\u6a21\u5f0f\u7684\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664\u7b2c 1 \u5230 6 \u884c\u4e2d\u5305\u542b Linux \u8868\u8fbe\u5f0f\u7684\u884c\u3002<\/p>\n

# sed '1,6{\/Linux\/d;}' sed-demo.txt \r\n Unix Operating System\r\n RHEL\r\n Red Hat\r\n Fedora\r\n CentOS\r\n Debian\r\n Ubuntu\r\n openSUSE\r\n<\/pre>\n
13) \u5982\u4f55\u5220\u9664\u5339\u914d\u6a21\u5f0f\u7684\u884c\u53ca\u5176\u4e0b\u4e00\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664\u5305\u542b System \u8868\u8fbe\u5f0f\u7684\u884c\u4ee5\u53ca\u5b83\u7684\u4e0b\u4e00\u884c\u3002<\/p>\n

# sed '\/System\/{N;d;}' sed-demo.txt\r\n RHEL\r\n Red Hat\r\n Fedora\r\n Arch Linux\r\n CentOS\r\n Debian\r\n Ubuntu\r\n openSUSE\r\n<\/pre>\n
14) \u5982\u4f55\u5220\u9664\u5305\u542b\u6570\u5b57\u7684\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664\u6240\u6709\u5305\u542b\u6570\u5b57\u7684\u884c\u3002<\/p>\n

# sed '\/[0-9]\/d' sed-demo-1.txt\r\nLinux Operating System\r\nUnix Operating System\r\nRHEL\r\nRed Hat\r\nFedora\r\ndebian\r\nubuntu\r\n<\/pre>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664\u6240\u6709\u4ee5\u6570\u5b57\u5f00\u5934\u7684\u884c\u3002<\/p>\n

# sed '\/^[0-9]\/d' sed-demo-1.txt\r\nLinux Operating System\r\nUnix Operating System\r\nRHEL\r\nRed Hat\r\nFedora\r\ndebian\r\nubuntu\r\nArch Linux - 1\r\n<\/pre>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664\u6240\u6709\u4ee5\u6570\u5b57\u7ed3\u5c3e\u7684\u884c\u3002<\/p>\n

# sed '\/[0-9]$\/d' sed-demo-1.txt \r\nLinux Operating System\r\nUnix Operating System\r\nRHEL\r\nRed Hat\r\nFedora\r\ndebian\r\nubuntu\r\n - Manjaro\r\n<\/pre>\n
15) \u5982\u4f55\u5220\u9664\u5305\u542b\u5b57\u6bcd\u7684\u884c\uff1f<\/strong><\/div>\n

\u4f7f\u7528\u4ee5\u4e0b sed \u547d\u4ee4\u5220\u9664\u6240\u6709\u5305\u542b\u5b57\u6bcd\u7684\u884c\u3002<\/p>\n

# sed '\/[A-Za-z]\/d' sed-demo-1.txt \r\n3 4 5 6 \r\n<\/pre>\n

via: https:\/\/www.2daygeek.com\/linux-remove-delete-lines-in-file-sed-command\/<\/p>\n

\u4f5c\u8005\uff1aMagesh Maruthamuthu \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1ahello-wn \u6821\u5bf9\uff1awxy<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5982\u4f55\u4f7f\u7528sed\u547d\u4ee4\u5220\u9664\u6587\u4ef6\u4e2d\u7684\u884c Sed \u4ee3\u8868\u6d41\u7f16\u8f91\u5668Stream Editor\uff0c\u5e38\u7528\u4e8e Linux \u4e2d\u57fa\u672c\u7684 […]<\/p>\n","protected":false},"author":1898,"featured_media":157761,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[439],"class_list":["post-157757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-sed"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/157757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=157757"}],"version-history":[{"count":11,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/157757\/revisions"}],"predecessor-version":[{"id":160432,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/157757\/revisions\/160432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/157761"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=157757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=157757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=157757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}