﻿{"id":22027,"date":"2023-06-29T08:25:18","date_gmt":"2023-06-29T00:25:18","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=22027"},"modified":"2023-06-28T20:51:07","modified_gmt":"2023-06-28T12:51:07","slug":"linux-sed-command-guide","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/linux-sed-command-guide.html","title":{"rendered":"\u638c\u63e1 Linux sed \u547d\u4ee4\u7efc\u5408\u6307\u5357"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e86\u5173\u4e8e Linux \u4e2d sed \u547d\u4ee4\u7684\u5168\u9762\u6307\u5357\uff0c\u6db5\u76d6\u4e86\u5176\u5386\u53f2\u3001\u7528\u9014\u4ee5\u53ca\u4e00\u4e9b\u5b9e\u7528\u7684\u6280\u5de7\u548c\u7a8d\u95e8\u3002\u901a\u8fc7\u638c\u63e1 sed\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u5904\u7406\u6587\u672c\u5904\u7406\u4efb\u52a1\uff0c\u8fd9\u5bf9\u4e8e\u4efb\u4f55\u4f7f\u7528 Linux \u7684\u4eba\u6765\u8bf4\u90fd\u662f\u4e00\u9879\u5b9d\u8d35\u7684\u6280\u80fd\u3002<\/p>\n<p><strong>\u8bf4\u660e<\/strong><\/p>\n<p>\u672c\u6587\u65e8\u5728\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u5173\u4e8e\u5982\u4f55\u5728 Linux \u4e2d\u4f7f\u7528 sed\uff08\u6d41\u7f16\u8f91\u5668\uff09\u547d\u4ee4\u7684\u5168\u9762\u6307\u5357\u3002\u5b83\u6db5\u76d6\u4e86\u8be5\u547d\u4ee4\u7684\u5386\u53f2\u3001\u7528\u6cd5\u3001\u53c2\u6570\u3001\u5e38\u89c1\u7528\u4f8b\u4ee5\u53ca\u6280\u5de7\u548c\u7a8d\u95e8\u3002<\/p>\n<p><strong>\u5386\u53f2<\/strong><\/p>\n<p>sed \u547d\u4ee4\u662f\u5728 UNIX \u7684\u65e9\u671f\u5f00\u53d1\u7684\uff0c\u5e76\u4ece Linux \u7684\u8bde\u751f\u5f00\u59cb\u5c31\u6210\u4e3a\u5176\u4e2d\u4e00\u90e8\u5206\u3002\u5b83\u88ab\u5e7f\u6cdb\u7528\u4e8e\u6587\u672c\u5904\u7406\uff0c\u5e76\u4ee5\u5176\u9ad8\u6548\u548c\u5feb\u901f\u800c\u95fb\u540d\u3002<\/p>\n<p><strong>\u4f55\u65f6\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5b83<\/strong><\/p>\n<p>\u5f53\u60a8\u9700\u8981\u5728\u6587\u4ef6\u6216\u6d41\u4e0a\u9ad8\u6548\u5730\u6267\u884c\u6587\u672c\u8f6c\u6362\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 sed\u3002\u5b83\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u80fd\u591f\u5904\u7406\u5927\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u548c\u66ff\u6362\uff0c\u8fd9\u4f7f\u5176\u6210\u4e3a\u811a\u672c\u7f16\u5199\u548c\u6570\u636e\u6574\u7406\u7684\u5fc5\u5907\u5de5\u5177\u3002<\/p>\n<p><strong>\u76f8\u5173<\/strong>\uff1a<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8bf4\u6bcf\u4e2a Linux \u6781\u5ba2\u90fd\u9700\u8981\u4e86\u89e3 Sed \u548c Awk <a href=\"https:\/\/www.linuxmi.com\/linux-sed-awk.html\">https:\/\/www.linuxmi.com\/linux-sed-awk.html<\/a><\/p>\n<p>Linux\u4e2d\u7684Grep\uff0cAwk\uff0cSed\u547d\u4ee4 <a href=\"https:\/\/www.linuxmi.com\/linux-grep-awk-sed.html\">https:\/\/www.linuxmi.com\/linux-grep-awk-sed.html<\/a><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5b83<\/strong><\/p>\n<p>sed \u547d\u4ee4\u7684\u57fa\u672c\u8bed\u6cd5\u662f sed &#8216;command&#8217; file_name\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\">linuxmi@linuxmi ~\/www.linuxmi.com\r\n % echo \"Hello World\" | sed 's\/World\/linux\u8ff7www.linuxmi.com\/'\r\nHello linux\u8ff7www.linuxmi.com\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22029\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2023\/06\/sed.png\" alt=\"\" width=\"1042\" height=\"287\" \/><\/p>\n<p><strong>\u5e38\u7528\u53c2\u6570<\/strong><\/p>\n<p>-n\uff1a\u7981\u6b62\u81ea\u52a8\u6253\u5370\u6a21\u5f0f\u7f13\u51b2\u533a\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\">linuxmi@linuxmi ~\/www.linuxmi.com\r\n % echo \"Hello World\" | sed -n 's\/World\/linux\u8ff7www.linuxmi.com\/p'\r\nHello linux\u8ff7www.linuxmi.com<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22030\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2023\/06\/sed1.png\" alt=\"\" width=\"1042\" height=\"287\" \/><\/p>\n<p>-i\uff1a\u4f7f\u7528-i\u53c2\u6570\uff0csed\u547d\u4ee4\u4f1a\u76f4\u63a5\u4fee\u6539\u6e90\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u5c06\u4fee\u6539\u7ed3\u679c\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u3002\u5982\u679c\u63d0\u4f9b\u4e86\u6269\u5c55\u540d\uff0csed\u4f1a\u5728\u7f16\u8f91\u6587\u4ef6\u4e4b\u524d\u521b\u5efa\u4e00\u4e2a\u5907\u4efd\u6587\u4ef6\uff0c\u4ee5\u9632\u6b62\u610f\u5916\u4fee\u6539\u5bfc\u81f4\u7684\u6570\u636e\u4e22\u5931\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\">linuxmi@linuxmi ~\/www.linuxmi.com\r\n % echo \"Hello World\" &gt; linuxmi.txt\r\nlinuxmi@linuxmi ~\/www.linuxmi.com\r\n % sed -i 's\/World\/Linux\u8ff7www.linuxmi.com\/' linuxmi.txt\r\nlinuxmi@linuxmi ~\/www.linuxmi.com\r\n % cat linuxmi.txt                                    \r\nHello Linux\u8ff7www.linuxmi.com<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22031\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2023\/06\/sed2.png\" alt=\"\" width=\"1042\" height=\"411\" \/><\/p>\n<p><strong>\u5176\u4ed6\u652f\u6301\u7684\u53c2\u6570<\/strong><\/p>\n<p>-e\uff1a\u5141\u8bb8\u4f7f\u7528\u591a\u4e2a\u7f16\u8f91\u547d\u4ee4\u3002<br \/>\n-f\uff1a\u5141\u8bb8\u6307\u5b9a\u5305\u542bsed\u547d\u4ee4\u7684\u6587\u4ef6\u3002<br \/>\n-r\uff1a\u4f7f\u7528\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<br \/>\n-u\uff1a\u4f7f\u7f13\u51b2\u533a\u65e0\u7f13\u51b2\u3002<\/p>\n<p><strong>\u6700\u5e38\u89c1\u7684\u7528\u4f8b<\/strong><\/p>\n<p>sed \u7684\u5e38\u89c1\u7528\u4f8b\u4e4b\u4e00\u662f\u5728\u6587\u4ef6\u4e2d\u66ff\u6362\u6587\u672c\u3002<\/p>\n<pre><span style=\"background-color: #d22626;\">linuxmi@linuxmi<\/span> <span style=\"color: #586e75;\"><b>~\/www.linuxmi.com<\/b><\/span>\r\n<span style=\"color: #fdf6e3;\"><b> % <\/b><\/span><span style=\"color: #859900;\">echo<\/span> <span style=\"color: #b58900;\">\"Hello World! www.linuxmi.com\"<\/span> &gt; <u style=\"text-decoration-style: single;\">linuxmi.txt<\/u>\r\n<span style=\"background-color: #d22626;\">linuxmi@linuxmi<\/span> <span style=\"color: #586e75;\"><b>~\/www.linuxmi.com<\/b><\/span>\r\n<span style=\"color: #fdf6e3;\"><b> % <\/b><\/span><span style=\"color: #859900;\">sed<\/span> -i <span style=\"color: #b58900;\">'s\/World\/linuxmi.com\/'<\/span> <u style=\"text-decoration-style: single;\">linuxmi.txt<\/u>\r\n<span style=\"background-color: #d22626;\">linuxmi@linuxmi<\/span> <span style=\"color: #586e75;\"><b>~\/www.linuxmi.com<\/b><\/span>\r\n<span style=\"color: #fdf6e3;\"><b> % <\/b><\/span><span style=\"color: #859900;\">cat<\/span> <u style=\"text-decoration-style: single;\">linuxmi.txt<\/u>\r\nHello linuxmi.com! www.linuxmi.com\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22033\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2023\/06\/sed3.png\" alt=\"\" width=\"1042\" height=\"411\" \/><\/p>\n<p><strong>\u6280\u5de7<\/strong><\/p>\n<p>sed\u53ef\u4ee5\u901a\u8fc7\u94fe\u5f0f\u591a\u4e2a\u547d\u4ee4\u5728\u4e00\u884c\u547d\u4ee4\u4e2d\u6267\u884c\u590d\u6742\u7684\u6587\u672c\u8f6c\u6362\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\">linuxmi@linuxmi ~\/www.linuxmi.com\r\n % echo \"Hello World\" | sed 's\/Hello\/Hi\/; s\/World\/Linux\u8ff7www.linuxmi.com\/'\r\nHi Linux\u8ff7www.linuxmi.com<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22032\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2023\/06\/sed4.png\" alt=\"\" width=\"1042\" height=\"411\" \/><\/p>\n<p><strong>\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879<\/strong><\/p>\n<p>\u5f53\u4f7f\u7528-i\u9009\u9879\u65f6\uff0c\u8981\u6ce8\u610f\u5b83\u53ef\u80fd\u5bf9\u6587\u4ef6\u8fdb\u884c\u4e0d\u53ef\u9006\u7684\u4fee\u6539\u3002\u5728\u5904\u7406\u91cd\u8981\u6570\u636e\u65f6\uff0c\u52a1\u5fc5\u786e\u4fdd\u8fdb\u884c\u5907\u4efd\u3002<\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>sed\u662f\u547d\u4ee4\u884c\u6587\u672c\u5904\u7406\u7684\u5f3a\u5927\u5de5\u5177\u3002\u501f\u52a9\u5b83\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u6709\u6548\u5730\u8fdb\u884c\u590d\u6742\u7684\u6587\u672c\u8f6c\u6362\u3002\u719f\u7ec3\u638c\u63e1sed\u5c06\u63d0\u9ad8\u60a8\u5728\u6587\u672c\u5904\u7406\u4efb\u52a1\u4e2d\u7684\u751f\u4ea7\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3a\u521d\u5b66\u8005\u63d0\u4f9b\u4e86\u5173\u4e8e Linux \u4e2d sed \u547d\u4ee4\u7684\u5168\u9762\u6307\u5357\uff0c\u6db5\u76d6\u4e86\u5176\u5386\u53f2\u3001\u7528\u9014\u4ee5\u53ca\u4e00\u4e9b\u5b9e\u7528\u7684 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[605,8,985],"class_list":["post-22027","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-linuxmingling","tag-sed"],"aioseo_notices":[],"views":1144,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/22027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=22027"}],"version-history":[{"count":4,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/22027\/revisions"}],"predecessor-version":[{"id":22036,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/22027\/revisions\/22036"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=22027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=22027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=22027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}