{"id":1109,"date":"2011-06-15T17:29:25","date_gmt":"2011-06-15T09:29:25","guid":{"rendered":"http:\/\/www.jpuyy.com\/blog\/?p=1109"},"modified":"2020-03-18T14:19:56","modified_gmt":"2020-03-18T06:19:56","slug":"sed-usage-and-examples","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=1109","title":{"rendered":"sed\u547d\u4ee4\u7528\u6cd5\u53ca\u793a\u4f8b"},"content":{"rendered":"<p>sed&#8211;\u6587\u4ef6\u6d41\u7f16\u8f91\u5668\uff0c\u8fc7\u6ee4\u548c\u6539\u53d8\u6587\u672c<\/p>\n<p>gnu\u7684\u8bf4\u660e\u6587\u6863\uff1ahttp:\/\/www.gnu.org\/software\/sed\/manual\/sed.html<\/p>\n<p>sed\u53ef\u5220\u9664(delete)\u3001\u6539\u53d8(change)\u3001\u6dfb\u52a0(append)\u3001\u63d2\u5165(insert)\u3001\u5408\u5e76\u3001\u4ea4\u6362\u6587\u672c\u4e2d<code>\u884c<\/code>\uff0c\u53ef\u66ff\u6362(substuite)\u5b83\u4eec\u5f53\u4e2d\u7684\u5b57\u4e32\u3001\u8f6c\u6362(tranfer)\u5176\u4e2d\u7684\u5b57\u6bcd\u3002<\/p>\n<p>sed\u7531\u6807\u51c6\u8f93\u5165\u8bfb\u5165\u4e00\u884c\u8d44\u6599\u5e76\u653e\u5165pattern space\u65f6\uff0c\u4f9d\u7167\u8bbe\u5b9a\u597d\u7684\u89c4\u5219\uff0c\u9010\u4e00\u5bf9pattern space\u5185\u7684\u8d44\u6599\u6267\u884c\u7f16\u8f91\u540e\u6807\u51c6\u8f93\u51fa\uff1b\u63a5\u7740\u518d\u5c06\u4e0b\u4e00\u884c\u8d44\u6599\u8bfb\u5165\u3002\u5982\u6b64\u91cd\u6267\u884c\u4e0a\u8ff0\u52a8\u4f5c\u76f4\u5230\u5904\u7406\u5b8c\u6210\u3002\u5373\uff1a<\/p>\n<p>(1)sed\u603b\u662f\u4ee5\u884c\u5bf9\u8f93\u5165\u8fdb\u884c\u5904\u7406<br \/>\n(2)sed\u5904\u7406\u7684\u4e0d\u662f\u539f\u6587\u4ef6\u800c\u662f\u539f\u6587\u4ef6\u7684\u62f7\u8d1d<\/p>\n<p>\u7528\u6cd5\uff1a sed [OPTION]&#8230; {script-only-if-no-other-script} [input-file]<br \/>\n\u5373\uff1a sed \u9009\u9879 \u6267\u884c\u65b9\u6cd5 \u6267\u884c\u6587\u4ef6<\/p>\n<p>sed -e \u8868\u793a\u4e0d\u4f1a\u4fee\u6539\u6587\u4ef6\uff0c\u64cd\u4f5c\u7684\u5bf9\u50cf\u662f\u8bfb\u53d6\u7684\u6587\u4ef6<\/p>\n<p>sed -i \u8868\u793ain-place\uff0c\u5c31\u5730\u4fee\u6539\uff0c\u6240\u4ee5\u8981\u5c0f\u5fc3\u4f7f\u7528<\/p>\n<p>\u76f4\u63a5\u4e0a\u5b9e\u4f8b\uff0c\u4ee5\u4e0b\u5b9e\u4f8b\u90fd\u7528sed -i\uff0c\u8981\u6765\u5c31\u6765\u771f\u7684\uff0c\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\u9700\u8981\u5b66\u4e60\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<h2>\u5220\u9664<\/h2>\n<p>\u5220\u9664\u7b2cx\u884c\uff0cx\u662f\u6570\u5b57\uff1b\u5982\u5220\u9664\u7b2c25\u884c<\/p>\n<pre>sed -i '25d' filename<\/pre>\n<p>\u5220\u9664\u7b2cn\u5230m\u884c\uff1b\u5982\u5220\u9664\u7b2c20\u523030\u884c<\/p>\n<pre>sed -e '20,30d' filename<\/pre>\n<p>\u5220\u9664\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\uff0c\u4e0d\u8fc7\u6ca1\u591a\u5927\u610f\u4e49<\/p>\n<pre>sed -e '1,$d' filename<\/pre>\n<p>\u5220\u9664\u5305\u542babc\u7684\u6240\u6709\u884c<\/p>\n<pre>sed -e '\/abc\/d' filename<\/pre>\n<p>\u5220\u9664<code>\u4e0d\u542b\u6709<\/code>\u5b57\u7b26\u4e32abc\u7684\u6240\u6709\u884c<\/p>\n<pre>sed -e '\/abc\/!d' filename<\/pre>\n<h2>\u66ff\u6362<\/h2>\n<p>\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u53c2\u6570\u4e3as(string)\uff0c\u66ff\u6362\u884c\u7684\u547d\u4ee4\u4e3ac\uff1b\u540e\u9762g\u8868\u793a\u5339\u914d\u6240\u6709<\/p>\n<p>\u5c06\u7b2c\u4e00\u884c\u66ff\u6362\u4e3a#!\/bin\/bash<\/p>\n<pre>sed -i '1c\\#!\/bin\/bash' filename<\/pre>\n<p>\u5c06\u7b2c1\u523010\u884c\u66ff\u6362\u6210 i love you<\/p>\n<pre>sed -i '1,10c\\I love you' filename<\/pre>\n<p>\u5c06\u7b2c12\u884c\u4e2d\u7684dz\uff0c\u6362\u4e3axxx<\/p>\n<pre>sed -i \"12s\/dz\/xxx\/g\" \/web\/club\/config.php<\/pre>\n<p>\u5c06\u6587\u4ef6\u4e2d39-41\u884c\u7684apache\u6362\u4e3anginx<\/p>\n<pre>sed -i \"39,41s\/apache\/nginx\/g\" \/etc\/php-fpm.d\/www.conf<\/pre>\n<p>\u66ff\u6362\/home\u4e0b\u6240\u6709\u6587\u4ef6\u4e2d\u7684www.jpuyy.com\u4e3ajpuyy.com\uff0c\u8981\u5c0f\u5fc3\u4f7f\u7528<\/p>\n<pre>sed -i \"s\/www.jpuyy.com\/jpuyy.com\/g\" `grep www.jpuyy.com -rl \/home`<\/pre>\n<p>\u5c06\u6bcf\u884c\u7684\u884c\u9996\u63d2\u5165#\uff0c\u867d\u7136\u8fd9\u662f\u63d2\u5165\uff0c\u4f46\u5176\u5b9e\u4e5f\u662f\u66ff\u6362<\/p>\n<pre>sed -i 's\/^\/\\#\/' test<\/pre>\n<p>\u628a\u5355\u5f15\u53f7\u53d8\u4e3a\u53cc\u5f15\u53f7<\/p>\n<pre>sed -i 's\/'\"'\"\/'\"''\/g' filename<\/pre>\n<p>\u628a\u53cc\u5f15\u53f7\u53d8\u4e3a\u5355\u5f15\u53f7<\/p>\n<pre>sed -i 's\/''\"'\/\"'\"'\/g' filename<\/pre>\n<h2>\u63d2\u5165(insert)<\/h2>\n<p>\u5728\u7b2c10\u884c\u524d\u9762\u63d2\u5165\u4e00\u884c\uff0c\u5e76\u5199\u5165 I am the ten line.<\/p>\n<pre>sed -i '10i\\&nbsp;I am the ten line.' filename<\/pre>\n<p>\u5728\u6240\u6709\u7684girls\u524d\u9762<code>\u63d2\u5165\u4e00\u884c<\/code>\uff0c\u5e76\u5199\u5165beautiful<\/p>\n<pre>sed -i '\/girls\/i\\beautiful' filename<\/pre>\n<h2>\u8ffd\u52a0(append)<\/h2>\n<p>\u5728\u7b2c\u4e00\u884c\u52a0\u5165I am line 2.<\/p>\n<pre>sed -i '1a\\I am line 2.' filename<\/pre>\n<p>\u5728\u542b\u6709oooo\u7684\u884c\u7684\u540e\u9762\u63d2\u5165\u4e00\u884c\uff0c\u5e76\u5199\u5165xxxx<\/p>\n<pre>sed -i '\/oooo\/a\\xxxx' filename<\/pre>\n<pre style=\"color: #000000;\">sed -i '\/Port\\ 22\/ a Port\\ 5277' \/etc\/ssh\/sshd_config<\/pre>\n<p>sed \u53d6 xml \u4e2d\u7684 value<\/p>\n<pre><code>echo '&lt;id&gt;I am a sample group&lt;\/id&gt;' | sed 's\/&lt;\\\/\\?[^&gt;]\\+&gt;\/\/g'<\/code><\/pre>\n<p>\u57fa\u672c\u7684\u5148\u5b66\u5230\u8fd9\uff0c\u6709\u65f6\u5019\u7f16\u5199\u811a\u672c\u7684\u65f6\u5019\uff0c\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u800c\u4e0d\u662f\u5355\u5f15\u53f7\u3002sed\u8fd8\u6709\u5f88\u591a\u9ad8\u7ea7\u7684\u547d\u4ee4\uff0c\u8d8a\u9ad8\u7ea7\u6740\u4f24\u529b\u8d8a\u5927\u3002<\/p>\n<p>\u67e5\u770b\u6587\u4ef6<\/p>\n<p>\u67e5\u770b\u7b2c90-100\u884c<\/p>\n<pre>cat filename | sed -n '90,100p'<\/pre>\n<p>\u67e5\u770b\u7b2c2\u884c<\/p>\n<pre>cat filename | sed -e '2!d'\n\ncat filename | sed -n '2p'\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>sed&#8211;\u6587\u4ef6\u6d41\u7f16\u8f91\u5668\uff0c\u8fc7\u6ee4\u548c\u6539\u53d8\u6587\u672c gnu\u7684\u8bf4\u660e\u6587\u6863\uff1ahttp:\/\/www.gnu.org\/software\/sed\/manual\/sed.html sed\u53ef\u5220\u9664(delete)\u3001\u6539\u53d8(change)\u3001\u6dfb\u52a0(append)\u3001\u63d2\u5165(insert)\u3001\u5408\u5e76\u3001\u4ea4\u6362\u6587\u672c\u4e2d\u884c\uff0c\u53ef\u66ff\u6362(substuite)\u5b83\u4eec\u5f53\u4e2d\u7684\u5b57\u4e32\u3001\u8f6c\u6362(tranfer)\u5176\u4e2d\u7684\u5b57\u6bcd\u3002 sed\u7531\u6807\u51c6\u8f93\u5165\u8bfb\u5165\u4e00\u884c\u8d44\u6599\u5e76\u653e\u5165pattern space\u65f6\uff0c\u4f9d\u7167\u8bbe\u5b9a\u597d\u7684\u89c4\u5219\uff0c\u9010\u4e00\u5bf9pattern space\u5185\u7684\u8d44\u6599\u6267\u884c\u7f16\u8f91\u540e\u6807\u51c6\u8f93\u51fa\uff1b\u63a5\u7740\u518d\u5c06\u4e0b\u4e00\u884c\u8d44\u6599\u8bfb\u5165\u3002\u5982\u6b64\u91cd\u6267\u884c\u4e0a\u8ff0\u52a8\u4f5c\u76f4\u5230\u5904\u7406\u5b8c\u6210\u3002\u5373\uff1a (1)sed\u603b\u662f\u4ee5\u884c\u5bf9\u8f93\u5165\u8fdb\u884c\u5904\u7406 (2)sed\u5904\u7406\u7684\u4e0d\u662f\u539f\u6587\u4ef6\u800c\u662f\u539f\u6587\u4ef6\u7684\u62f7\u8d1d \u7528\u6cd5\uff1a sed [OPTION]&#8230; {script-only-if-no-other-script} [input-file] \u5373\uff1a sed \u9009\u9879 \u6267\u884c\u65b9\u6cd5 \u6267\u884c\u6587\u4ef6 sed -e \u8868\u793a\u4e0d\u4f1a\u4fee\u6539\u6587\u4ef6\uff0c\u64cd\u4f5c\u7684\u5bf9\u50cf\u662f\u8bfb\u53d6\u7684\u6587\u4ef6 sed -i \u8868\u793ain-place\uff0c\u5c31\u5730\u4fee\u6539\uff0c\u6240\u4ee5\u8981\u5c0f\u5fc3\u4f7f\u7528 \u76f4\u63a5\u4e0a\u5b9e\u4f8b\uff0c\u4ee5\u4e0b\u5b9e\u4f8b\u90fd\u7528sed -i\uff0c\u8981\u6765\u5c31\u6765\u771f\u7684\uff0c\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\u9700\u8981\u5b66\u4e60\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u5220\u9664 \u5220\u9664\u7b2cx\u884c\uff0cx\u662f\u6570\u5b57\uff1b\u5982\u5220\u9664\u7b2c25\u884c sed -i &#8217;25d&#8217; filename \u5220\u9664\u7b2cn\u5230m\u884c\uff1b\u5982\u5220\u9664\u7b2c20\u523030\u884c sed -e &#8216;20,30d&#8217; filename \u5220\u9664\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\uff0c\u4e0d\u8fc7\u6ca1\u591a\u5927\u610f\u4e49 sed -e &#8216;1,$d&#8217; filename \u5220\u9664\u5305\u542babc\u7684\u6240\u6709\u884c sed -e &#8216;\/abc\/d&#8217; filename \u5220\u9664\u4e0d\u542b\u6709\u5b57\u7b26\u4e32abc\u7684\u6240\u6709\u884c sed -e &#8216;\/abc\/!d&#8217; filename \u66ff\u6362 \u66ff\u6362\u5b57\u7b26\u4e32\u7684\u53c2\u6570\u4e3as(string)\uff0c\u66ff\u6362\u884c\u7684\u547d\u4ee4\u4e3ac\uff1b\u540e\u9762g\u8868\u793a\u5339\u914d\u6240\u6709 \u5c06\u7b2c\u4e00\u884c\u66ff\u6362\u4e3a#!\/bin\/bash [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[123,23],"class_list":["post-1109","post","type-post","status-publish","format-standard","hentry","category-linux","tag-sed","tag-summary"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/1109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1109"}],"version-history":[{"count":18,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/1109\/revisions"}],"predecessor-version":[{"id":8762,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/1109\/revisions\/8762"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}