{"id":307,"date":"2011-07-02T21:42:20","date_gmt":"2011-07-02T13:42:20","guid":{"rendered":"http:\/\/www.ipcpu.com\/?p=307"},"modified":"2011-07-02T21:42:20","modified_gmt":"2011-07-02T13:42:20","slug":"linux-sed-basic","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2011\/07\/linux-sed-basic\/","title":{"rendered":"Linux\u5b66\u4e60\u7b14\u8bb0\u4e4bsed\u547d\u4ee4\u57fa\u7840"},"content":{"rendered":"

1. Sed\u7b80\u4ecb<\/strong>
\nsed \u662f\u4e00\u79cd\u5728\u7ebf\u7f16\u8f91\u5668\uff0c\u5b83\u4e00\u6b21\u5904\u7406\u4e00\u884c\u5185\u5bb9\u3002\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\uff0c\u628a\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u9001\u5f80\u5c4f\u5e55\u3002\u63a5\u7740\u5904\u7406\u4e0b\u4e00\u884c\uff0c\u8fd9\u6837\u4e0d\u65ad\u91cd\u590d\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u3002\u6587\u4ef6\u5185\u5bb9\u5e76\u6ca1\u6709 \u6539\u53d8\uff0c\u9664\u975e\u4f60\u4f7f\u7528\u91cd\u5b9a\u5411\u5b58\u50a8\u8f93\u51fa\u3002Sed\u4e3b\u8981\u7528\u6765\u81ea\u52a8\u7f16\u8f91\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\uff1b\u7b80\u5316\u5bf9\u6587\u4ef6\u7684\u53cd\u590d\u64cd\u4f5c\uff1b\u7f16\u5199\u8f6c\u6362\u7a0b\u5e8f\u7b49.
\nCentos4\u7684sed\u7248\u672csed-4.1.5-5.fc6
\n2. \u5b9a\u5740<\/strong>
\n\u53ef\u4ee5\u901a\u8fc7\u5b9a\u5740\u6765\u5b9a\u4f4d\u4f60\u6240\u5e0c\u671b\u7f16\u8f91\u7684\u884c\uff0c\u8be5\u5730\u5740\u7528\u6570\u5b57\u6784\u6210\uff0c\u7528\u9017\u53f7\u5206\u9694\u7684\u4e24\u4e2a\u884c\u6570\u8868\u793a\u4ee5\u8fd9\u4e24\u884c\u4e3a\u8d77\u6b62\u7684\u884c\u7684\u8303\u56f4\uff08\u5305\u62ec\u884c\u6570\u8868\u793a\u7684\u90a3\u4e24\u884c\uff09\u3002\u59821\uff0c3\u8868\u793a1\uff0c2\uff0c3\u884c\uff0c\u7f8e\u5143\u7b26\u53f7($)\u8868\u793a\u6700\u540e\u4e00\u884c\u3002\u8303\u56f4\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u8005\u4e8c\u8005\u7ed3\u5408\u7684\u65b9\u5f0f\u786e\u5b9a.
\n3. Sed\u547d\u4ee4<\/strong>
\n\u8c03\u7528sed\u547d\u4ee4\u6709\u4e24\u79cd\u5f62\u5f0f\uff1a
\n* sed [options] 'command' file(s)
\n* sed [options] -f scriptfile file(s)
\n\u4e00\u79cd\u662f\u76f4\u63a5\u5199\u547d\u4ee4\uff0c\u4e00\u79cd\u662f\u5c06\u547d\u4ee4\u5199\u5230\u6587\u4ef6\u518d\u8c03\u7528
\n4. \u9009\u9879[options]<\/strong>
\n-e command, \u2013expression=command
\n##\u5141\u8bb8\u591a\u6b21\u7f16\u8f91\u3002
\n-n, \u2013quiet, \u2013silent
\n##\u53d6\u6d88\u9ed8\u8ba4\u8f93\u51fa\uff08\u9ed8\u8ba4\u8f93\u51fa\u6587\u4ef6\u5168\u6587\uff09
\n-f, \u2013filer=script-file\u5f15\u5bfcsed\u811a\u672c\u6587\u4ef6\u540d\u3002
\n##\u8c03\u7528\u547d\u4ee4\u6587\u4ef6
\n-i\uff0c
\n##\u76f4\u63a5\u4fee\u6539\u6587\u4ef6
\n5.\u547d\u4ee4[command]<\/strong>
\n\u6211\u4eec\u91c7\u7528\u6848\u4f8b\u65b9\u6cd5\u6765\u8bb2\u8ff0\u8fd9\u90e8\u5206\uff1a
\n\u4f7f\u7528\u7684\u6848\u4f8b\u6587\u4ef6\u4e3a1233.txt\u5185\u5bb9\u5982\u4e0b\uff1a
\nipcpu@ubuntu:~$ cat 1233.txt
\nfirst
\nsecond
\nthird
\nfive
\nipcpu@ubuntu:~$
\n============\u6253\u5370\u5339\u914d\u7684\u884c============<\/span>
\np\u00a0 \u6253\u5370\u6a21\u677f\u5757\u7684\u884c\u3002
\nipcpu@ubuntu:~$ cat 1233.txt
\nfirst
\nsecond
\nthird
\nfive
\nipcpu@ubuntu:~$
\nipcpu@ubuntu:~$ sed '\/second\/p' 1233.txt
\nfirst
\nsecond
\nsecond
\nthird
\nfive
\nipcpu@ubuntu:~$ sed -n '\/second\/p' 1233.txt
\nsecond
\nipcpu@ubuntu:~$
\nsed\u9ed8\u8ba4\u4f1a\u628a\u6240\u6709\u884c\u6253\u5370\u51fa\u6765\uff0c\u5339\u914d\u4e0asecond\u4f1a\u6267\u884cp\u518d\u6253\u5370\u4e00\u904d
\n============\u6dfb\u52a0\u548c\u63d2\u5165\u6587\u672c ============<\/span>
\na\u00a0 \u5728\u884c\u540e\u9762\u6dfb\u52a0\u6587\u672c
\ni\u00a0 \u5728\u822a\u524d\u9762\u6dfb\u52a0\u6587\u672c
\nipcpu@ubuntu:~$ sed '$a \\
\n> oo \\
\n> last line ' 1233.txt
\nfirst
\nsecond
\nthird
\nfive
\noo
\nlast line
\nipcpu@ubuntu:~$
\n\u6ce8\u610f\u6b64\u5904\u7684\u53cd\u659c\u7ebf\\\u8868\u793a\u56de\u8f66\uff0c\u5728\u53cd\u659c\u7ebf\u540e\u5fc5\u987b\u6309\u4e0b\u56de\u8f66
\nipcpu@ubuntu:~$ sed '3a four' 1233.txt
\nfirst
\nsecond
\nthird
\nfour
\nfive
\n\u5728\u7b2c\u4e09\u884c\u540e\u9762\u52a0\u5165\u5185\u5bb9
\nipcpu@ubuntu:~$ sed '1i zero' 1233.txt
\nzero
\nfirst
\nsecond
\nthird
\nfive
\n\u5728\u7b2c\u4e00\u884c\u524d\u9762\u63d2\u5165\u5185\u5bb9
\nipcpu@ubuntu:~$ sed '\/five\/i four' 1233.txt
\nfirst
\nsecond
\nthird
\nfour
\nfive
\nipcpu@ubuntu:~$
\n\u5728five\u524d\u9762\u63d2\u5165\u4e00\u884c\uff0c\u5185\u5bb9\u4e3afour
\n============\u5220\u9664\u884c ============<\/span>
\nd \u5220\u9664\u884c
\nipcpu@ubuntu:~$ sed '1,3d' 1233.txt
\nfive
\n\u5220\u96641\u52303\u884c
\nipcpu@ubuntu:~$ sed '$d' 1233.txt
\nfirst
\nsecond
\nthird
\n\u5220\u9664\u6700\u540e\u4e00\u884c
\nipcpu@ubuntu:~$ sed '2d' 1233.txt
\nfirst
\nthird
\nfive
\n\u5220\u9664\u7b2c2\u884c
\nipcpu@ubuntu:~$ sed '\/^th\/d' 1233.txt
\nfirst
\nsecond
\nfive
\n\u5220\u9664th\u5f00\u5934\u7684\u884c
\nipcpu@ubuntu:~$ sed '\/^$\/d' 1233.txt
\nfirst
\nsecond
\nthird
\nfive
\n\u5220\u9664\u7a7a\u884c
\nipcpu@ubuntu:~$ sed 's\/^..\/\/' 1233.txt
\nrst
\ncond 2nd
\nird
\nve
\nipcpu@ubuntu:~$
\n\u5220\u9664\u6bcf\u884c\u7684\u524d\u4e24\u4e2a\u5b57\u7b26
\n============\u66ff\u6362============<\/span>
\ns \u66ff\u6362
\nipcpu@ubuntu:~$ sed 's\/f\/F\/g' 1233.txt
\nFirst
\nsecond
\nthird
\nFive
\n\u5c06\u6240\u6709\u7684f\u66ff\u6362\u6210\u5927\u5199F
\nipcpu@ubuntu:~$ sed '1,2 s\/d$\/&dd\/' 1233.txt
\nfirst
\nseconddd
\nthird
\nfive
\n\u57281-2\u884c\u4e2d\uff0c\u6240\u6709\u4ee5d\u7ed3\u5c3e\u5b57\u6bb5\u540e\u9644\u52a0\u4e00\u4e2add
\nipcpu@ubuntu:~$ sed '\/first\/ s\/st\/ST\/' 1233.txt
\nfirST
\nsecond
\nthird
\nfive
\nipcpu@ubuntu:~$
\n\u5728\u542b\u6709first\u7684\u884c\u4e2d\uff0c\u628ast\u6362\u6210\u5927\u5199ST
\nipcpu@ubuntu:~$ cat box
\nit is a box.
\nipcpu@ubuntu:~$ sed 's\/ \/\\t\/g' box
\nit\u00a0\u00a0\u00a0\u00a0\u00a0 is\u00a0\u00a0\u00a0\u00a0\u00a0 a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 box.
\n\u5c06\u6587\u4e2d\u6240\u6709\u7a7a\u683c\u66ff\u6362\u6210TAB
\nipcpu@ubuntu:~$sed-i '\/string\/ s\/^.*$\/newlinecontent\/g' file
\n\u5c06\u6587\u4ef6file\u4e2d\u5305\u542bstring\u7684\u6574\u884c\u66ff\u6362\u6210\u201cnewlinecontent\u201d
\n============\u8f6c\u6362============<\/span>
\ny \u8f6c\u6362
\nipcpu@ubuntu:~$ sed 'y\/five\/six1\/' 1233.txt
\nsirst
\ns1cond 2nd
\nthird
\nsix1
\n\u901a\u5e38\u7528\u4e8e\u5927\u5c0f\u5199\u8f6c\u6362\uff0c\u8981\u6c42\u957f\u5ea6\u5fc5\u987b\u4e00\u81f4\u3002\u529f\u80fd\u4e0es\u57fa\u672c\u4e00\u81f4\u3002
\n============\u6307\u5b9a\u884c\u7684\u8303\u56f4============<\/span>
\n\uff0c\u9017\u53f7
\nipcpu@ubuntu:~$ sed -n '\/second\/,\/five\/ p' 1233.txt
\nsecond
\nthird
\nfive
\n\u8f93\u51fasecond\u548cfive\u4e2d\u95f4\u7684\u6570\u636e\uff0c\u6b64\u5904\u4e0d\u52a0-n\u4f1a\u51fa\u73b0\u91cd\u590d
\nipcpu@ubuntu:~$ sed '\/second\/,\/five\/ s\/i\/IIII\/' 1233.txt
\nfirst
\nsecond
\nthIIIIrd
\nfIIIIve
\n\u4fee\u6539second\u548cfive\u4e2d\u95f4\u7684\u6570\u636e\uff0c\u7528III\u66ff\u6362i
\n============\u591a\u6b21\u7f16\u8f91============<\/span>
\ne \u591a\u6b21\u7f16\u8f91
\nipcpu@ubuntu:~$ sed -e '1d' -e 's\/^sec\/2nd\/' 1233.txt
\n2ndond
\nthird
\nfive
\nipcpu@ubuntu:~$
\n============\u8bfb\u5199\u6587\u4ef6============<\/span>
\nr\u00a0 \u8bfb\u6587\u4ef6
\nw \u5199\u6587\u4ef6
\nipcpu@ubuntu:~$ sed '\/second\/r box' 1233.txt
\nfirst
\nsecond
\nit is a box.
\nthird
\nfive
\n\u5339\u914d\u4e0asecond\u540e\uff0c\u5728\u5176\u540e\u8bfb\u5165\u6587\u4ef6
\nipcpu@ubuntu:~$ sed '\/second\/w write' 1233.txt
\nfirst
\nsecond
\nthird
\nfive
\nipcpu@ubuntu:~$ cat write
\nsecond
\nipcpu@ubuntu:~$
\n\u5339\u914d\u4e0asecond\u540e\u628a\u8be5\u884c\u5199\u5165\u5230\u6587\u4ef6
\n============\u63d0\u524d\u9000\u51fa============<\/span>
\nq\u63d0\u524d\u9000\u51fa
\nsed \u9ed8\u8ba4\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\uff0c\u5e76\u53ea\u5728\u5230\u8fbe\u672b\u5c3e\u65f6\u624d\u505c\u6b62\u3002\u4e0d\u8fc7\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u9000\u51fa\u547d\u4ee4\u63d0\u524d\u505c\u6b62\u5904\u7406\u3002\u53ea\u80fd\u6307\u5b9a\u4e00\u6761\u9000\u51fa\u547d\u4ee4\uff0c\u800c\u5904\u7406\u5c06\u4e00\u76f4\u6301\u7eed\u76f4\u5230\u6ee1\u8db3\u8c03\u7528\u9000\u51fa\u547d\u4ee4\u7684\u6761\u4ef6\u3002
\n\u4f8b\u5982\uff0c\u4ec5\u5728\u6587\u4ef6\u7684\u524d\u4e94\u884c\u4e0a\u6267\u884c\u66ff\u6362\uff0c\u7136\u540e\u9000\u51fa\uff1a
\n$ sed '
\n> \/two\/ s\/1\/2\/
\n> \/three\/ s\/1\/3\/
\n> 5q' sample_one
\none 1
\ntwo 2
\nthree 3
\none 1
\ntwo 2
\n$
\n============\u5176\u4ed6\u547d\u4ee4<\/span>
\n\u6682\u5b58\u4e0e\u53d6\u7528
\nh \u548c g
\n\u6682\u5b58\u548c\u4e92\u6362
\nx
\n\u53c2\u8003\u8d44\u6599\uff1a<\/strong>
\nhttp:\/\/blog.csdn.net\/thimin\/article\/details\/1802738<\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Linux\u5b66\u4e60\u7b14\u8bb0\u4e4bsed\u547d\u4ee4\u57fa\u7840<\/a><\/p>","protected":false},"excerpt":{"rendered":"

1. Sed\u7b80\u4ecb sed \u662f\u4e00\u79cd\u5728\u7ebf\u7f16\u8f91\u5668\uff0c\u5b83\u4e00\u6b21\u5904\u7406\u4e00\u884c\u5185\u5bb9\u3002\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\uff0c\u628a\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u9001\u5f80\u5c4f\u5e55\u3002\u63a5\u7740\u5904\u7406\u4e0b\u4e00\u884c\uff0c\u8fd9\u6837\u4e0d\u65ad\u91cd\u590d\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u3002\u6587\u4ef6\u5185\u5bb9\u5e76\u6ca1\u6709 \u6539\u53d8\uff0c\u9664\u975e\u4f60\u4f7f\u7528\u91cd\u5b9a\u5411\u5b58\u50a8\u8f93\u51fa\u3002Sed\u4e3b\u8981\u7528\u6765\u81ea\u52a8\u7f16\u8f91\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\uff1b\u7b80\u5316\u5bf9\u6587\u4ef6\u7684\u53cd\u590d\u64cd\u4f5c\uff1b\u7f16\u5199\u8f6c\u6362\u7a0b\u5e8f\u7b49. Centos4\u7684sed\u7248\u672csed-4.1.5-5.fc6 2. \u5b9a\u5740 \u53ef\u4ee5\u901a\u8fc7\u5b9a\u5740\u6765\u5b9a\u4f4d\u4f60\u6240\u5e0c\u671b\u7f16\u8f91\u7684\u884c\uff0c\u8be5\u5730\u5740\u7528\u6570\u5b57\u6784\u6210\uff0c\u7528\u9017\u53f7\u5206\u9694\u7684\u4e24\u4e2a\u884c\u6570\u8868\u793a\u4ee5\u8fd9\u4e24\u884c\u4e3a\u8d77\u6b62\u7684\u884c\u7684\u8303\u56f4\uff08\u5305\u62ec\u884c\u6570\u8868\u793a\u7684\u90a3\u4e24\u884c\uff09\u3002\u59821\uff0c3\u8868\u793a1\uff0c2\uff0c3\u884c\uff0c\u7f8e\u5143\u7b26\u53f7($)\u8868\u793a\u6700\u540e\u4e00\u884c\u3002\u8303\u56f4\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u8005\u4e8c\u8005\u7ed3\u5408\u7684\u65b9\u5f0f\u786e\u5b9a. 3. Sed\u547d\u4ee4 \u8c03\u7528sed\u547d\u4ee4\u6709\u4e24\u79cd\u5f62\u5f0f\uff1a * sed [options] ‘command’ file(s) * sed [options] -f scriptfile file(s) \u4e00\u79cd\u662f\u76f4\u63a5\u5199\u547d\u4ee4\uff0c\u4e00\u79cd\u662f\u5c06\u547d\u4ee4\u5199\u5230\u6587\u4ef6\u518d\u8c03\u7528 4. \u9009\u9879[options] -e command, \u2013expression=command ##\u5141\u8bb8\u591a\u6b21\u7f16\u8f91\u3002 -n, \u2013quiet, \u2013silent ##\u53d6\u6d88\u9ed8\u8ba4\u8f93\u51fa\uff08\u9ed8\u8ba4\u8f93\u51fa\u6587\u4ef6\u5168\u6587\uff09 -f, \u2013filer=script-file\u5f15\u5bfcsed\u811a\u672c\u6587\u4ef6\u540d\u3002 ##\u8c03\u7528\u547d\u4ee4\u6587\u4ef6 -i\uff0c ##\u76f4\u63a5\u4fee\u6539\u6587\u4ef6 5.\u547d\u4ee4[command] \u6211\u4eec\u91c7\u7528\u6848\u4f8b\u65b9\u6cd5\u6765\u8bb2\u8ff0\u8fd9\u90e8\u5206\uff1a \u4f7f\u7528\u7684\u6848\u4f8b\u6587\u4ef6\u4e3a1233.txt\u5185\u5bb9\u5982\u4e0b\uff1a ipcpu@ubuntu:~$ cat 1233.txt first second third five ipcpu@ubuntu:~$ ============\u6253\u5370\u5339\u914d\u7684\u884c============ p\u00a0 \u6253\u5370\u6a21\u677f\u5757\u7684\u884c\u3002 ipcpu@ubuntu:~$ […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[17],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/307"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":0,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}