{"id":5617,"date":"2022-06-09T10:41:39","date_gmt":"2022-06-09T02:41:39","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=5617"},"modified":"2022-06-09T17:17:14","modified_gmt":"2022-06-09T09:17:14","slug":"high-efficiency-and-easy-sed-command","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/high-efficiency-and-easy-sed-command.html","title":{"rendered":"\u9ad8\u6548\u800c\u8f7b\u677e\u7684sed\u547d\u4ee4"},"content":{"rendered":"
\u672c\u6587\u7531lrxjmw.cn\u56e2\u961f\u6210\u5458\u9b4f\u4e3d\u733f<\/span>\u6574\u7406\u53d1\u5e03\uff0c\u70b9\u6b64\u67e5\u770b\u6700\u65b0Linux\u8d44\u8baf<\/a><\/span><\/strong>\u3002<\/p>\n<\/div>\n sed\uff08stream editor\uff09\u662f\u4e00\u6b3e\u9ad8\u6548\u7684\u6d41\u7f16\u8f91\u5668\uff0c\u5b83\u4e00\u6b21\u53ea\u5904\u7406\u4e00\u884c\u5185\u5bb9\uff0c\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\u628a\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u9001\u5f80\u5c4f\u5e55\uff0c\u63a5\u7740\u5904\u7406\u4e0b\u4e00\u884c\uff0c\u8fd9\u6837\u4e0d\u65ad\u91cd\u590d\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u3002sed\u5904\u7406\u8fc7\u7684\u6587\u4ef6\u5185\u5bb9\u5e76\u6ca1\u6709\u6539\u53d8\uff0c\u9664\u975e\u4f60\u4f7f\u7528\u91cd\u5b9a\u5411\u5b58\u50a8\u8f93\u51fa\u3002<\/p>\n \u547d\u4ee4\u683c\u5f0f\u5982\u4e0b\uff1a<\/strong><\/p>\n \u5176\u4e2doption\u662f\u53ef\u9009\u7684\uff0c\u5e38\u7528\u7684option<\/strong>\u6709\u5982\u4e0b\u51e0\u79cd\uff1a<\/p>\n \u5e38\u7528\u7684\u547d\u4ee4\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/strong><\/p>\n \u547d\u4ee4\u793a\u4f8b<\/strong><\/p>\n \u5047\u8bbe\u6709\u4e00\u4e2a\u672c\u5730\u6587\u4ef6test.txt\uff0c\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n \u672c\u8282\u5c06\u4f7f\u7528\u8be5\u6587\u4ef6\u8be6\u7ec6\u6f14\u793a\u6bcf\u4e00\u4e2a\u547d\u4ee4\u7684\u7528\u6cd5\u3002<\/p>\n \u672c\u4f8b\u547d\u4ee4\u90e8\u5206\u4e2d\u76841\u8868\u793a\u7b2c\u4e00\u884c\uff0c\u540c\u6837\u7684\u7b2c\u4e8c\u884c\u5199\u62102\uff0c\u7b2c\u4e00\u884c\u5230\u7b2c\u4e09\u884c\u5199\u62101,3\uff0c\u7528$\u8868\u793a\u6700\u540e\u4e00\u884c\uff0c\u6bd4\u59822,$\u8868\u793a\u7b2c\u4e8c\u884c\u5230\u6700\u540e\u4e00\u884c\u4e2d\u95f4\u6240\u6709\u7684\u884c(\u5305\u542b\u7b2c\u4e8c\u884c\u548c\u6700\u540e\u4e00\u884c)\u3002 \u672c\u4f8b\u8868\u793a\u5728\u7b2c\u4e00\u884c\u548c\u6700\u540e\u4e00\u884c\u6240\u6709\u7684\u884c\u540e\u9762\u90fd\u52a0\u4e0a\u201dadd one\u201d\u5b57\u7b26\u4e32\uff0c\u4ece\u8f93\u51fa\u53ef\u4ee5\u770b\u5230\u6548\u679c\u3002<\/p>\n \u672c\u4f8b\u8868\u793a\u5728\u5305\u542b\u201dfirst\u201d\u5b57\u7b26\u4e32\u7684\u884c\u7684\u540e\u9762\u52a0\u4e0a\u5b57\u7b26\u4e32\u201dadd one\u201d\uff0c\u4ece\u8f93\u51fa\u53ef\u4ee5\u770b\u5230\u7b2c\u4e00\u884c\u5305\u542bfirst\uff0c\u6240\u4ee5\u7b2c\u4e00\u884c\u4e4b\u540e\u589e\u52a0\u4e86\u201dadd one\u201d<\/p>\n \u672c\u4f8b\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u884c\uff0c^ha.*day$\u8868\u793a\u4ee5ha\u5f00\u5934\uff0c\u4ee5day\u7ed3\u5c3e\u7684\u884c\uff0c\u5219\u53ef\u4ee5\u5339\u914d\u5230\u6587\u4ef6\u7684\u201dhappy everyday\u201d\u8fd9\u6837\uff0c\u6240\u4ee5\u5728\u8be5\u884c\u540e\u9762\u589e\u52a0\u4e86\u201dadd one\u201d\u5b57\u7b26\u4e32\u3002<\/p>\n i\u547d\u4ee4\u4f7f\u7528\u65b9\u6cd5\u548ca\u547d\u4ee4\u4e00\u6837\u7684\uff0c\u53ea\u4e0d\u8fc7\u662f\u5728\u5339\u914d\u7684\u884c\u7684\u524d\u9762\u63d2\u5165\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u76f4\u63a5\u5c06\u4e0a\u9762a\u547d\u4ee4\u7684\u793a\u4f8b\u7684a\u66ff\u6362\u6210i\u5373\u53ef\uff0c\u5728\u6b64\u5c31\u4e0d\u5570\u55e6\u4e86\u3002<\/p>\n \u672c\u4f8b\u8868\u793a\u5c06\u6700\u540e\u4e00\u884c\u66ff\u6362\u6210\u5b57\u7b26\u4e32\u201dadd one\u201d\uff0c\u4ece\u8f93\u51fa\u53ef\u4ee5\u770b\u5230\u6548\u679c\u3002<\/p>\n \u672c\u4f8b\u5c06\u7b2c\u56db\u884c\u5230\u6700\u540e\u4e00\u884c\u7684\u5185\u5bb9\u66ff\u6362\u6210\u5b57\u7b26\u4e32\u201dadd one\u201d\u3002<\/p>\n \u672c\u4f8b\u5c06\u4ee5ha\u5f00\u5934\uff0c\u4ee5day\u7ed3\u5c3e\u7684\u884c\u66ff\u6362\u6210\u201dreplace line\u201d\u3002<\/p>\n \u672c\u4f8b\u5220\u9664\u4ee5ha\u5f00\u5934\uff0c\u4ee5day\u7ed3\u5c3e\u7684\u884c\u3002<\/p>\n \u672c\u4f8b\u5220\u9664\u7b2c\u56db\u884c\u5230\u6700\u540e\u4e00\u884c\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n \u672c\u4f8b\u5728\u5c4f\u5e55\u4e0a\u6253\u5370\u7b2c\u56db\u884c\u5230\u6700\u540e\u4e00\u884c\u7684\u5185\u5bb9\uff0cp\u547d\u4ee4\u4e00\u822c\u548c-n\u9009\u9879\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n \u672c\u4f8b\u6253\u5370\u4ee5ha\u5f00\u59cb\uff0c\u4ee5day\u7ed3\u5c3e\u7684\u884c\u3002<\/p>\n \u5b9e\u9645\u8fd0\u7528\u4e2ds\u547d\u4ee4\u5f0f\u6700\u5e38\u4f7f\u7528\u5230\u7684\u3002<\/p>\n \u672c\u4f8b\u5c06\u6587\u4ef6\u4e2d\u7684\u6240\u6709line\u66ff\u6362\u6210text\uff0c\u6700\u540e\u7684g\u662fglobal\u7684\u610f\u601d\uff0c\u4e5f\u5c31\u662f\u5168\u5c40\u66ff\u6362\uff0c\u5982\u679c\u4e0d\u52a0g\uff0c\u5219\u53ea\u4f1a\u66ff\u6362\u672c\u884c\u7684\u7b2c\u4e00\u4e2aline\u3002<\/p>\n \u672c\u4f8b\u9996\u5148\u5339\u914d\u4ee5ha\u5f00\u59cb\uff0c\u4ee5day\u7ed3\u5c3e\u7684\u884c\uff0c\u672c\u4f8b\u4e2d\u5339\u914d\u5230\u7684\u884c\u662f\u201dhappy everyday\u201d\u8fd9\u6837\uff0c\u7136\u540e\u518d\u5c06\u8be5\u884c\u4e2d\u7684happy\u66ff\u6362\u6210very happy\u3002<\/p>\n \u8fd9\u4e2a\u4f8b\u5b50\u6709\u70b9\u590d\u6742\uff0c\u5148\u5206\u89e3\u4e00\u4e0b\u3002\u9996\u5148s\u547d\u4ee4\u7684\u6a21\u5f0f\u662fs\/old\/new\/g\u8fd9\u6837\u7684\uff0c\u6240\u4ee5\u672c\u4f8b\u7684old\u90e8\u5206\u5373\\(.*\\)line$,sed\u547d\u4ee4\u4e2d\u4f7f\u7528\\(\\)\u5305\u88f9\u7684\u5185\u5bb9\u8868\u793a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2cn\u90e8\u5206\uff0c\u5e8f\u53f7\u4ece1\u5f00\u59cb\u8ba1\u7b97\uff0c\u672c\u4f8b\u4e2d\u53ea\u6709\u4e00\u4e2a\\(\\)\u6240\u4ee5\\(.*\\)\u8868\u793a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u90e8\u5206\uff0c\u8fd9\u90e8\u5206\u5339\u914d\u4efb\u610f\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\\(.*\\)line$\u5339\u914d\u7684\u5c31\u662f\u4ee5line\u7ed3\u5c3e\u7684\u4efb\u4f55\u884c\u3002\u7136\u540e\u5c06\u5339\u914d\u5230\u7684\u884c\u66ff\u6362\u6210\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u90e8\u5206\uff08\u672c\u4f8b\u4e2d\u76f8\u5f53\u4e8e\u5220\u9664line\u90e8\u5206\uff09\uff0c\u4f7f\u7528\\1\u8868\u793a\u5339\u914d\u5230\u7684\u7b2c\u4e00\u90e8\u5206\uff0c\u540c\u6837\\2\u8868\u793a\u7b2c\u4e8c\u90e8\u5206\uff0c\\3\u8868\u793a\u7b2c\u4e09\u90e8\u5206\uff0c\u53ef\u4ee5\u4f9d\u6b21\u8fd9\u6837\u5f15\u7528\u3002\u6bd4\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2dis\u4e24\u8fb9\u7684\u90e8\u5206\u53ef\u4ee5\u7528\\1\u548c\\2\u8868\u793a\uff0c\u8be5\u4f8b\u5b50\u7684\u4f5c\u7528\u5176\u5b9e\u5c31\u662f\u5220\u9664\u4e2d\u95f4\u90e8\u5206\u7684is\u3002<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttp:\/\/qifuguang.me\/2015\/09\/21\/sed\u547d\u4ee4\u8be6\u89e3\/<\/a><\/p>\nsed [option] 'command' input_file\r\n<\/pre>\n
-n \u4f7f\u7528\u5b89\u9759(silent)\u6a21\u5f0f\uff0c\u53ea\u5217\u51fa\u7ecf\u8fc7sed\u7279\u6b8a\u5904\u7406\u7684\u90a3\u4e00\u884c(\u6216\u8005\u52a8\u4f5c)\u5185\u5bb9\uff1b\r\n-e \u76f4\u63a5\u5728\u6307\u4ee4\u5217\u6a21\u5f0f\u4e0a\u8fdb\u884c sed \u7684\u52a8\u4f5c\u7f16\u8f91\uff1b\r\n-f \u76f4\u63a5\u5c06 sed \u7684\u52a8\u4f5c\u5199\u5728\u4e00\u4e2a\u6587\u4ef6\u5185\uff0c -f filename \u5219\u53ef\u4ee5\u6267\u884cfilename\u5185\u7684sed\u547d\u4ee4\uff1b\r\n-r \u8ba9sed\u547d\u4ee4\u652f\u6301\u6269\u5c55\u7684\u6b63\u5219\u8868\u8fbe\u5f0f(\u9ed8\u8ba4\u662f\u57fa\u7840\u6b63\u5219\u8868\u8fbe\u5f0f)\uff1b\r\n-i \u76f4\u63a5\u4fee\u6539\u8bfb\u53d6\u7684\u6587\u4ef6\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u7531\u5c4f\u5e55\u8f93\u51fa\uff1b<\/pre>\n
a \\\uff1a \u5373append\u8ffd\u52a0\u5b57\u7b26\u4e32\uff0c\u53ef\u5c06\u5176\u540e\u7684\u5b57\u7b26\u52a0\u5728\u6240\u9009\u62e9\u5185\u5bb9\u7684\u540e\u9762\r\nc \\\uff1a \u53d6\u4ee3\/\u66ff\u6362\u5b57\u7b26\u4e32\uff0c\u53ef\u5c06\u5176\u540e\u5185\u5bb9\u66ff\u6362\u81f3\u6240\u9009\u5185\u5bb9\r\nd \uff1a \u5373delete\u5220\u9664\uff0c\u8be5\u547d\u4ee4\u4f1a\u5c06\u5f53\u524d\u9009\u4e2d\u7684\u884c\u5220\u9664\r\ni \\\uff1a \u5373insert\u63d2\u5165\u5b57\u7b26\u4e32\uff0c\u53ef\u5c06\u5176\u540e\u5185\u5bb9\u63d2\u5165\u81f3\u6240\u9009\u5185\u5bb9\u524d\r\np \uff1a print\u5373\u6253\u5370\uff0c\u8be5\u547d\u4ee4\u4f1a\u6253\u5370\u5f53\u524d\u9009\u62e9\u7684\u884c\u5230\u5c4f\u5e55\u4e0a\r\ns \uff1a \u66ff\u6362\uff0c\u901a\u5e38s\u547d\u4ee4\u7684\u7528\u6cd5\u662f\u8fd9\u6837\u7684\uff1a1\uff0c2s\/old\/new\/g\uff0c\u5c06old\u5b57\u7b26\u4e32\u66ff\u6362\u6210new\u5b57\u7b26\u4e32\r\n<\/pre>\n
[root@linuxprobe ~]$ cat test.txt\r\nthis is first line\r\nthis is second line\r\nthis is third line\r\nthis is fourth line\r\nthis fifth line\r\nhappy everyday\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '1a \\add one' test.txt\r\nthis is first line\r\nadd one\r\nthis is secondline\r\nthis is third line\r\nthis is fourth line\r\nthis is fifth line\r\nhappy everyday\r\nend\r\n<\/pre>\n
\n\u672c\u4f8b\u7684\u4f5c\u7528\u662f\u5728\u7b2c\u4e00\u884c\u4e4b\u540e\u589e\u52a0\u5b57\u7b26\u4e32\u201dadd one\u201d\uff0c\u4ece\u8f93\u51fa\u53ef\u4ee5\u770b\u5230\u5177\u4f53\u6548\u679c\u3002<\/p>\n[root@linuxprobe ~]$ sed '1,$a \\add one' test.txt\r\nthis is first line\r\nadd one\r\nthis is second line\r\nadd one\r\nthis is third line\r\nadd one\r\nthis is fourth line\r\nadd one\r\nthis is fifth line\r\nadd one\r\nhappy everyday\r\nadd one\r\nend\r\nadd one\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '\/first\/a \\add one' test.txt\r\nthis is first line\r\nadd one\r\nthis is secondline\r\nthis is third line\r\nthis is fourth line\r\nthis is fifth line\r\nhappy everyday\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '\/^ha.*day$\/a \\add one' test.txt\r\nthis is first line\r\nthis is secondline\r\nthis is third line\r\nthis is fourth line\r\nthis is fifth line\r\nhappy everyday\r\nadd one\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '$c \\add one' test.txt\r\nthis is first line\r\nthis is secondline\r\nthis is third line\r\nthis is fourth line\r\nthis is fifth line\r\nhappy everyday\r\nadd one\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '4,$c \\add one' test.txt\r\nthis is first line\r\nthis is secondline\r\nthis is third line\r\nadd one\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '\/^ha.*day$\/c \\replace line' test.txt\r\nthis is first line\r\nthis is secondline\r\nthis is third line\r\nthis is fourth line\r\nthis is fifth line\r\nreplace line\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '\/^ha.*day$\/d' test.txt\r\nthis isfirst line\r\nthis issecond line\r\nthis isthird line\r\nthis isfourth line\r\nthis isfifth line\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '4,$d' test.txt\r\nthisis first line\r\nthisis second line\r\nthisis third line\r\n<\/pre>\n
[root@linuxprobe ~]$ sed -n '4,$p' test.txt\r\nthisis fourth line\r\nthisis fifth line\r\nhappy everyday\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed -n '\/^ha.*day$\/p' test.txt\r\nhappy everyday\r\n<\/pre>\n
[root@linuxprobe ~]$ sed 's\/line\/text\/g' test.txt\r\nthis isfirst text\r\nthis issecond text\r\nthis isthird text\r\nthis isfourth text\r\nthis isfifth text\r\nhappy everyday\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed '\/^ha.*day$\/s\/happy\/very happy\/g' test.txt\r\nthis isfirst line\r\nthis issecond line\r\nthis isthird line\r\nthis isfourth line\r\nthis isfifth line\r\nvery happy everyday\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed 's\/\\(.*\\)line$\/\\1\/g' test.txt\r\nthisis first\r\nthisis second\r\nthisis third\r\nthisis fourth\r\nthisis fifth\r\nhappy everyday\r\nend\r\n<\/pre>\n
[root@linuxprobe ~]$ sed 's\/\\(.*\\)is\\(.*\\)line\/\\1\\2\/g' test.txt\r\nthis first\r\nthis second\r\nthis third\r\nthis fourth\r\nthis fifth\r\nhappy everyday\r\nend\r\n<\/pre>\n
\n