{"id":220596,"date":"2021-06-22T08:02:05","date_gmt":"2021-06-22T00:02:05","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=220596"},"modified":"2021-06-15T09:04:14","modified_gmt":"2021-06-15T01:04:14","slug":"sed-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sed-linux.html","title":{"rendered":"Linux\u547d\u4ee4-Sed\u7528\u6cd5\u6559\u7a0b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nsed \u5168\u540d\u4e3a stream editor\uff0c\u662f\u7528\u4e8e\u6587\u672c\u5904\u7406\u7684\u6d41\u7f16\u8f91\u5668\uff0c\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u3002sed\u5904\u7406\u6587\u672c...<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u524d\u8a00<\/strong><\/div>\n

sed \u5168\u540d\u4e3a stream editor\uff0c\u662f\u7528\u4e8e\u6587\u672c\u5904\u7406\u7684\u6d41\u7f16\u8f91\u5668\uff0c\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u3002sed\u5904\u7406\u6587\u672c\u65f6\u662f\u4e00\u6b21\u5904\u7406\u4e00\u884c\u5185\u5bb9<\/p>\n

sed\u8bed\u6cd5<\/strong><\/div>\n
    \n
  • sed\u547d\u4ee4\u5904\u7406\u7684\u5185\u5bb9\u662f\u6a21\u5f0f\u7a7a\u95f4\u4e2d\u7684\u5185\u5bb9\uff0c\u800c\u975e\u76f4\u63a5\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u3002\u5982\u679c\u52a0\u4e0a\u53c2\u6570 i \u5219\u53ef\u76f4\u63a5\u4fee\u6539\u6587\u4ef6\u5185\u5bb9<\/li>\n
  • \u793a\u4f8b\uff1ased -i 's\/\u539f\u5b57\u7b26\u4e32\/\u65b0\u5b57\u7b26\u4e32\/' \/home\/test.txt<\/li>\n<\/ul>\n
    sed [-nefr\u53c2\u6570] [function] [filePath]<\/strong><\/div>\n\n\n\n\n\n\n\n\n\n
    \u9009\u9879\u4e0e\u53c2\u6570<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
    -n<\/td>\n\u4f7f\u7528 silent \u6a21\u5f0f\u3002\u5728\u4e00\u822c sed \u7684\u7528\u6cd5\u4e2d\uff0c\u8f93\u5165\u7684\u6570\u636e\u90fd\u4f1a\u88ab\u8f93\u51fa\u5230\u5c4f\u5e55\u4e0a\u3002\u4f46\u5982\u679c\u52a0\u4e0a -n \u53c2\u6570\u540e\uff0c\u5219\u4e0d\u4f1a\u663e\u793a\uff0c\u5982\u679c\u6709\u8ddf\u7740 p \u6807\u5fd7\uff0c\u88ab sed \u7279\u6b8a\u5904\u7406\u7684\u90a3\u4e00\u884c\u4f1a\u88ab\u5217\u51fa\u6765<\/td>\n<\/tr>\n
    -e<\/td>\n\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u754c\u9762\u4e0a\u8fdb\u884c sed \u7684\u52a8\u4f5c\u7f16\u8f91\uff0c\u6267\u884c\u591a\u6761\u5b50\u547d\u4ee4<\/td>\n<\/tr>\n
    -f<\/td>\n\u5c06 sed \u7684\u52a8\u4f5c\u5199\u5728\u4e00\u4e2a\u6587\u4ef6\u5185\uff0c -f filename \u6267\u884c\u811a\u672c\u6587\u4ef6\u7684 sed \u52a8\u4f5c<\/td>\n<\/tr>\n
    -r<\/td>\nsed \u7684\u52a8\u4f5c\u652f\u6301\u7684\u662f\u5ef6\u4f38\u578b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5<\/td>\n<\/tr>\n
    -i<\/td>\n\u76f4\u63a5\u4fee\u6539\u8bfb\u53d6\u7684\u6587\u4ef6\u5185\u5bb9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

    \u9009\u9879-n\uff0c\u52a0\u4e0a-n\u9009\u9879\u540e\u88ab\u8bbe\u5b9a\u4e3a\u5b89\u9759\u6a21\u5f0f\uff0c\u4e5f\u5c31\u662f\u4e0d\u4f1a\u8f93\u51fa\u9ed8\u8ba4\u6253\u5370\u4fe1\u606f\uff0c\u9664\u975e\u5b50\u547d\u4ee4\u4e2d\u7279\u522b\u6307\u5b9a\u6253\u5370 p \u9009\u9879\uff0c\u5219\u53ea\u4f1a\u628a\u5339\u914d\u4fee\u6539\u7684\u884c\u8fdb\u884c\u6253\u5370<\/p>\n

    ----\u00a0\u4e24\u884c\u90fd\u6253\u5370\u51fa\u6765\u00a0----\r\nserver11:~\/test\u00a0#\u00a0echo\u00a0-e\u00a0'hello\u00a0\\n\u00a0world'\u00a0|\u00a0sed\u00a0's\/hello\/csc\/'\r\ncsc\r\n\u00a0world\r\n----\u00a0\u4e00\u884c\u4e5f\u6ca1\u6253\u5370\u00a0-----\r\nserver11:~\/test\u00a0#\u00a0echo\u00a0-e\u00a0'hello\u00a0\\n\u00a0world'\u00a0|\u00a0sed\u00a0-n\u00a0's\/hello\/csc\/'\r\n----\u00a0\u6253\u5370\u4e86\u5339\u914d\u884c\u00a0-----\r\nserver11:~\/test\u00a0#\u00a0echo\u00a0-e\u00a0'hello\u00a0\\n\u00a0world'\u00a0|\u00a0sed\u00a0-n\u00a0's\/hello\/csc\/p'\r\ncsc<\/pre>\n

    \u9009\u9879-e\uff0c\u591a\u6761\u5b50\u547d\u4ee4\u8fde\u7eed\u8fdb\u884c\u64cd\u4f5c<\/p>\n

    echo\u00a0-e\u00a0'hello\u00a0world'\u00a0|\u00a0sed\u00a0-e\u00a0's\/hello\/csc\/'\u00a0-e\u00a0's\/world\/lwl\/'\r\n\u7ed3\u679c\uff1acsc\u00a0lwl<\/pre>\n

    \u9009\u9879-i\uff0c\u76f4\u63a5\u4fee\u6539\u8bfb\u53d6\u7684\u6587\u4ef6\u5185\u5bb9<\/p>\n

    server11:~\/test\u00a0#\u00a0cat\u00a0file.txt\r\nhello\u00a0world\r\nserver11:~\/test\u00a0#\u00a0sed\u00a0's\/hello\/lwl\/'\u00a0file.txt\r\nlwl\u00a0world\r\nserver11:~\/test\u00a0#\u00a0cat\u00a0file.txt\r\nhello\u00a0world\r\n----\u00a0\u52a0\u4e0a\u53c2\u6570\u00a0i\u00a0\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u6587\u4ef6\u5185\u5bb9----\r\nserver11:~\/test\u00a0#\u00a0sed\u00a0-i\u00a0's\/hello\/lwl\/'\u00a0file.txt\r\nlwl\u00a0world\r\nserver11:~\/test\u00a0#\u00a0cat\u00a0file.txt\r\nlwl\u00a0world<\/pre>\n

    \u9009\u9879-f\uff0c\u6267\u884c\u6587\u4ef6\u811a\u672c<\/p>\n

    sed.script\u811a\u672c\u5185\u5bb9\uff1a\r\ns\/hello\/csc\/\r\ns\/world\/lwl\/\r\n------\r\necho\u00a0\"hello\u00a0world\"\u00a0|\u00a0sed\u00a0-f\u00a0sed.script\r\n\u7ed3\u679c\uff1acsc\u00a0lwl<\/pre>\n

    \u9009\u9879-r\uff0c\u9ed8\u8ba4\u53ea\u80fd\u652f\u6301\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u9700\u8981\u652f\u6301\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u9700\u8981\u52a0\u4e0a -r<\/p>\n

    echo\u00a0\"hello\u00a0world\"\u00a0|\u00a0sed\u00a0-r\u00a0's\/(hello)|(world)\/csc\/g'\r\ncsc\u00a0csc<\/pre>\n
    function\u8868\u8fbe\u5f0f\uff1a[n1[,n2]] function or \/{pattern}\/function<\/strong><\/div>\n

    n1, n2 \uff1a\u53ef\u9009\u9879\uff0c\u4e00\u822c\u4ee3\u8868\u201c\u9009\u62e9\u8fdb\u884cfunction\u5904\u7406\u7684\u884c\u6570\u201d\uff0c\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u300cfunction\u300d\u662f\u9700\u8981\u5728 10 \u5230 20 \u884c\u4e4b\u95f4\u8fdb\u884c\u7684\uff0c\u5219\u8868\u793a\u4e3a 10,20 [function]\u5982\u679c\u9700\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5b57\u7b26\u4e32\uff0c\u5219\u53ef\u7528 \/{pattern}\/ \u5339\u914d<\/p>\n

    test.txt\u00a0\u5185\u5bb9\r\n111\r\n222\r\n333\r\n444\r\n-----\u00a0\u5220\u9664\u975e\u7b2c2\u7b2c3\u884c\u4e4b\u95f4\u7684\u6240\u6709\u884c\u00a0----------\r\nserver11:~\u00a0#\u00a0sed\u00a0-i\u00a0'2,3!d'\u00a0test.txt\r\nserver11:~\u00a0#\u00a0cat\u00a0test.txt\r\n222\r\n333\r\n------\u00a0\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u00a0------------\r\nserver11:~\u00a0#\u00a0echo\u00a0'clswcl.txt'\u00a0|\u00a0sed\u00a0-nr\u00a0'\/.*\/p'\r\nclswcl.txt\u00a0\u00a0\/\/\u00a0\u00a0\/{pattern}\/\u00a0=\u00a0\/.*\/<\/pre>\n

    function \u6709\u4ee5\u4e0b\u8fd9\u4e9b\u9009\u9879[\/yiji]<\/h3>\n\n\n\n\n\n\n\n\n\n\n
    function<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
    a<\/td>\n\u65b0\u589e\uff1aa \u7684\u540e\u9762\u53ef\u4ee5\u63a5\u5b57\u4e32\uff0c\u800c\u8fd9\u4e9b\u5b57\u4e32\u4f1a\u5728\u65b0\u7684\u4e00\u884c\u51fa\u73b0\uff08\u76ee\u524d\u7684\u4e0b\u4e00\u884c\uff09<\/td>\n<\/tr>\n
    i<\/td>\n\u63d2\u5165\uff1ai \u7684\u540e\u9762\u53ef\u4ee5\u63a5\u5b57\u4e32\uff0c\u800c\u8fd9\u4e9b\u5b57\u4e32\u4f1a\u5728\u65b0\u7684\u4e00\u884c\u51fa\u73b0\uff08\u76ee\u524d\u7684\u4e0a\u4e00\u884c\uff09<\/td>\n<\/tr>\n
    c<\/td>\n\u53d6\u4ee3\uff1ac \u7684\u540e\u9762\u53ef\u4ee5\u63a5\u5b57\u4e32\uff0c\u8fd9\u4e9b\u5b57\u4e32\u53ef\u4ee5\u53d6\u4ee3 n1,n2 \u4e4b\u95f4\u7684\u884c<\/td>\n<\/tr>\n
    d<\/td>\n\u5220\u9664\uff1a\u56e0\u4e3a\u662f\u5220\u9664\u554a\uff0c\u6240\u4ee5 d \u540e\u9762\u901a\u5e38\u4e0d\u63a5\u4efb\u4f55\u4e1c\u897f<\/td>\n<\/tr>\n
    p<\/td>\n\u6253\u5370\uff1a\u4ea6\u5373\u5c06\u67d0\u4e2a\u9009\u62e9\u7684\u6570\u636e\u5370\u51fa\u3002\u901a\u5e38 p \u4f1a\u4e0e\u53c2\u6570 sed -n \u4e00\u8d77\u8fd0\u884c<\/td>\n<\/tr>\n
    s<\/td>\n\u53d6\u4ee3\uff1a\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u53d6\u4ee3\u7684\u5de5\u4f5c\u54e9\uff01\u901a\u5e38\u8fd9\u4e2a s \u7684\u52a8\u4f5c\u53ef\u4ee5\u642d\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\uff01\u4f8b\u5982\uff1a1,20 s\/old\/new\/g<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

    function\uff1a-a\uff0c\u884c\u540e\u63d2\u5165\u65b0\u884c<\/p>\n

    sed\u00a0-i\u00a0'\/\u7279\u5b9a\u5b57\u7b26\u4e32\/a\u00a0\u65b0\u884c\u5b57\u7b26\u4e32'\u00a0fileName<\/pre>\n

    function\uff1a-i\uff0c\u884c\u524d\u63d2\u5165\u65b0\u884c<\/p>\n

    sed\u00a0-i\u00a0'\/\u7279\u5b9a\u5b57\u7b26\u4e32\/i\u00a0\u65b0\u884c\u5b57\u7b26\u4e32'\u00a0fileName<\/pre>\n

    function\uff1a-c\uff0c\u4fee\u6539\u6307\u5b9a\u5185\u5bb9\u884c<\/p>\n

    sed\u00a0-i\u00a0'\/\u7279\u5b9a\u5b57\u7b26\u4e32\/c\u00a0csc\u00a0lwl'\u00a0fileName<\/pre>\n

    function\uff1a-d\uff0c\u5220\u9664\u7279\u5b9a\u5b57\u7b26\u4e32<\/p>\n

    sed\u00a0-i\u00a0'\/\u7279\u5b9a\u5b57\u7b26\u4e32\/d'\u00a0fileName<\/pre>\n
    sed s\u5b50\u547d\u4ee4: s\/{pattern}\/{replacement}\/{flags}<\/strong><\/div>\n
      \n
    • {pattern}\u662f\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n
    • \u5982\u679c{pattern}\u5b58\u5728\u5206\u7ec4\uff0c{replacement}\u4e2d\u7684\"\\n\"\u4ee3\u8868\u7b2cn\u4e2a\u5206\u7ec4\uff0c\"&\"\u4ee3\u8868\u6574\u4e2a\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002\u8be6\u60c5\u770b\u793a\u4f8b<\/li>\n
    • flags\u7684\u53c2\u6570\u5982\u4e0b<\/li>\n<\/ul>\n\n\n\n\n\n\n\n\n
      flags<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
      n<\/td>\n\u53ef\u4ee5\u662f1-512\uff0c\u8868\u793a\u7b2cn\u6b21\u51fa\u73b0\u7684\u60c5\u51b5\u8fdb\u884c\u66ff\u6362<\/td>\n<\/tr>\n
      g<\/td>\n\u5168\u5c40\u66f4\u6539<\/td>\n<\/tr>\n
      p<\/td>\n\u6253\u5370\u6a21\u5f0f\u7a7a\u95f4\u7684\u5185\u5bb9<\/td>\n<\/tr>\n
      w file<\/td>\n\u5199\u5165\u5230\u4e00\u4e2a\u6587\u4ef6file\u4e2d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

      \u793a\u4f8b<\/p>\n

      server11:~\u00a0#\u00a0echo\u00a0-e\u00a0'hello\u00a01112\u00a0world'\u00a0|\u00a0sed\u00a0-r\u00a0's\/([a-z]+)(\u00a0[0-9]+\u00a0)([a-z]+)\/&\/'\r\nhello\u00a01112\u00a0world\r\nserver11:~\u00a0#\u00a0echo\u00a0-e\u00a0'hello\u00a01112\u00a0world'\u00a0|\u00a0sed\u00a0-r\u00a0's\/([a-z]+)(\u00a0[0-9]+\u00a0)([a-z]+)\/\\3\\2\\1\/'\r\nworld\u00a01112\u00a0hello<\/pre>\n
      \u53c2\u8003\u6587\u7ae0<\/strong><\/div>\n
        \n
      • sed -i\u547d\u4ee4\u8be6\u89e3\u53ca\u5165\u95e8\u653b\u7565[1]<\/li>\n<\/ul>\n

        Reference
        \n[1]sed -i\u547d\u4ee4\u8be6\u89e3\u53ca\u5165\u95e8\u653b\u7565:<\/p>\n

        https:\/\/blog.csdn.net\/qq_33468857\/article\/details\/84324609<\/p>\n","protected":false},"excerpt":{"rendered":"

        sed \u5168\u540d\u4e3a stream editor\uff0c\u662f\u7528\u4e8e\u6587\u672c\u5904\u7406\u7684\u6d41\u7f16\u8f91\u5668\uff0c\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u3002sed\u5904\u7406\u6587\u672c\u65f6\u662f\u4e00\u6b21\u5904 […]<\/p>\n","protected":false},"author":323,"featured_media":208904,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-220596","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\/220596","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\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=220596"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220596\/revisions"}],"predecessor-version":[{"id":220609,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220596\/revisions\/220609"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208904"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=220596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=220596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=220596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}