{"id":1657,"date":"2011-08-17T13:32:00","date_gmt":"2011-08-17T05:32:00","guid":{"rendered":""},"modified":"2013-11-23T15:01:19","modified_gmt":"2013-11-23T07:01:19","slug":"vim%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1657.html","title":{"rendered":"Vim\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u547d\u4ee4<\/p>\n<div>\n<p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u547d\u4ee4\u6700\u5e38\u89c1\u7684\u5c31\u662f\u00a0\/ \uff08\u641c\u7d22\uff09\u547d\u4ee4\u3002\u5176\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>\/\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<p>\u53e6\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u547d\u4ee4\u5c31\u662f\u00a0:s\uff08\u66ff\u6362\uff09\u547d\u4ee4\uff0c\u5c06\u7b2c\u4e00\u4e2a\/\/\u4e4b\u95f4\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u6210\u7b2c\u4e8c\u4e2a\/\/\u4e4b\u95f4\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>:s\/\u6b63\u5219\u8868\u8fbe\u5f0f\/\u66ff\u6362\u5b57\u7b26\u4e32\/\u9009\u9879<\/p>\n<p>\u5728\u5b66\u4e60\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\u53ef\u4ee5\u5229\u7528\u00a0\/\u00a0\u547d\u4ee4\u6765\u7ec3\u4e60\u3002<\/p>\n<p>\u4e8c\u3001\u5143\u5b57\u7b26<\/p>\n<p>\u5143\u5b57\u7b26\u662f\u5177\u6709\u7279\u6b8a\u610f\u4e49\u7684\u5b57\u7b26\u3002\u4f7f\u7528\u5143\u5b57\u7b26\u53ef\u4ee5\u8868\u8fbe\u4efb\u610f\u5b57\u7b26\u3001\u884c\u9996\u3001\u884c \u5c3e\u3001\u67d0\u51e0\u4e2a\u5b57\u7b26\u7b49\u610f\u4e49\u3002<\/p>\n<p>\u5143\u5b57\u7b26\u4e00\u89c8<\/p>\n<div>\n<table border=\"0\" cellspacing=\"1\">\n<thead>\n<tr>\n<td>\u5143\u5b57\u7b26<\/td>\n<td>\u8bf4\u660e<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>.<\/td>\n<td>\u5339\u914d\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>[abc]<\/td>\n<td>\u5339\u914d\u65b9\u62ec\u53f7\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002\u53ef\u4ee5\u4f7f\u7528-\u8868\u793a\u5b57\u7b26\u8303\u56f4\uff0c<br \/>\n\u5982[a-z0-9]\u5339 \u914d\u5c0f\u5199\u5b57\u6bcd\u548c\u963f\u62c9\u4f2f\u6570\u5b57\u3002<\/td>\n<\/tr>\n<tr>\n<td>[^abc]<\/td>\n<td>\u5728\u65b9\u62ec\u53f7\u5185\u5f00\u5934\u4f7f\u7528^\u7b26\u53f7\uff0c\u8868\u793a\u5339\u914d\u9664\u65b9\u62ec\u53f7\u4e2d\u5b57\u7b26\u4e4b\u5916\u7684\u4efb\u610f\u5b57\u7b26\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\d<\/td>\n<td>\u5339\u914d\u963f\u62c9\u4f2f\u6570\u5b57\uff0c\u7b49\u540c\u4e8e[0-9]\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\D<\/td>\n<td>\u5339\u914d\u963f\u62c9\u4f2f\u6570\u5b57\u4e4b\u5916\u7684\u4efb\u610f\u5b57\u7b26\uff0c\u7b49\u540c\u4e8e[^0-9]\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\x<\/td>\n<td>\u5339\u914d\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff0c\u7b49\u540c\u4e8e[0-9A-Fa-f]\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\X<\/td>\n<td>\u5339\u914d\u5341\u516d\u8fdb\u5236\u6570\u5b57\u4e4b\u5916\u7684\u4efb\u610f\u5b57\u7b26\uff0c\u7b49\u540c\u4e8e[^0-9A-Fa-f]\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\w<\/td>\n<td>\u5339\u914d\u5355\u8bcd\u5b57\u6bcd\uff0c\u7b49\u540c\u4e8e[0-9A-Za-z_]\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\W<\/td>\n<td>\u5339\u914d\u5355\u8bcd\u5b57\u6bcd\u4e4b\u5916\u7684\u4efb\u610f\u5b57\u7b26\uff0c\u7b49\u540c\u4e8e[^0-9A-Za-z_]\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\t<\/td>\n<td>\u5339\u914d&lt;TAB&gt;\u5b57\u7b26\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\s<\/td>\n<td>\u5339\u914d\u7a7a\u767d\u5b57\u7b26\uff0c\u7b49\u540c\u4e8e[ \\t]\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\S<\/td>\n<td>\u5339\u914d\u975e\u7a7a\u767d\u5b57\u7b26\uff0c\u7b49\u540c\u4e8e[^ \\t]\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u8981\u67e5\u627e\u5b57\u7b26 *\u3001.\u3001\/\u7b49\uff0c\u5219\u9700\u8981\u5728\u524d\u9762\u7528\u00a0\\\u00a0\u7b26\u53f7\uff0c\u8868\u793a\u8fd9\u4e0d\u662f\u5143\u5b57\u7b26\uff0c\u800c\u53ea\u662f\u666e\u901a\u5b57\u7b26\u800c\u5df2\u3002<\/p>\n<div>\n<table border=\"0\" cellspacing=\"1\">\n<thead>\n<tr>\n<td>\u5143\u5b57\u7b26<\/td>\n<td>\u8bf4\u660e<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\\*<\/td>\n<td>\u5339\u914d * \u5b57\u7b26\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\.<\/td>\n<td>\u5339\u914d . \u5b57\u7b26\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\\/<\/td>\n<td>\u5339\u914d \/ \u5b57\u7b26\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\\\<\/td>\n<td>\u5339\u914d \\ \u5b57\u7b26\u3002<\/td>\n<\/tr>\n<tr>\n<td>\\[<\/td>\n<td>\u5339\u914d [ \u5b57\u7b26\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u8868\u793a\u6570\u91cf\u7684\u5143\u5b57\u7b26<\/p>\n<div>\n<table border=\"0\" cellspacing=\"1\">\n<thead>\n<tr>\n<td>\u5143\u5b57\u7b26<\/td>\n<td>\u8bf4\u660e<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>*<\/td>\n<td>\u5339\u914d0-\u4efb\u610f\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\+<\/td>\n<td>\u5339\u914d1-\u4efb\u610f\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\?<\/td>\n<td>\u5339\u914d0-1\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\{n,m}<\/td>\n<td>\u5339\u914dn-m\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\{n}<\/td>\n<td>\u5339\u914dn\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\{n,}<\/td>\n<td>\u5339\u914dn-\u4efb\u610f\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\{,m}<\/td>\n<td>\u5339\u914d0-m\u4e2a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u8868\u793a\u4f4d\u7f6e\u7684\u7b26\u53f7<\/p>\n<div>\n<table border=\"0\" cellspacing=\"1\">\n<thead>\n<tr>\n<td>\u5143\u5b57\u7b26<\/td>\n<td>\u8bf4\u660e<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>$<\/td>\n<td>\u5339\u914d\u884c\u5c3e<\/td>\n<\/tr>\n<tr>\n<td>^<\/td>\n<td>\u5339\u914d\u884c\u9996<\/td>\n<\/tr>\n<tr>\n<td>\\&lt;<\/td>\n<td>\u5339\u914d\u5355\u8bcd\u8bcd\u9996<\/td>\n<\/tr>\n<tr>\n<td>\\&gt;<\/td>\n<td>\u5339\u914d\u5355\u8bcd\u8bcd\u5c3e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u4f7f\u7528\u4f8b<\/p>\n<p>\/char\\s\\+[A-Za-z_]\\w*; &#8221; \u67e5\u627e\u6240\u6709\u4ee5char\u5f00\u5934\uff0c\u4e4b\u540e\u662f\u4e00\u4e2a\u4ee5\u4e0a\u7684\u7a7a\u767d\uff0c<br \/>\n&#8221; \u6700\u540e\u662f\u4e00\u4e2a\u6807\u8bc6\u7b26\u548c\u5206\u53f7<br \/>\n\/\\d\\d:\\d\\d:\\d\\d &#8221; \u67e5\u627e\u5982 17:37:01 \u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32<br \/>\n:g\/^\\s*$\/d &#8221; \u5220\u9664\u53ea\u6709\u7a7a\u767d\u7684\u884c<\/p>\n<p>:s\/\\&lt;four\\&gt;\/4\/g &#8221; \u5c06\u6240\u6709\u7684four\u66ff\u6362\u62104\uff0c\u4f46\u662ffourteen\u4e2d\u7684four\u4e0d\u66ff\u6362<\/p>\n<p>\u4e09\u3001\u66ff\u6362\u53d8\u91cf<\/p>\n<p>\u5728\u6b63\u89c4\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u00a0\\(\u00a0\u548c\u00a0\\)\u00a0\u7b26\u53f7\u62ec\u8d77\u6b63\u89c4\u8868\u8fbe\u5f0f\uff0c\u5373\u53ef\u5728\u540e\u9762\u4f7f\u7528\\1\u3001\\2\u00a0\u7b49\u53d8\u91cf\u6765\u8bbf\u95ee\u00a0\\(\u00a0\u548c\u00a0\\)\u00a0\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u4f7f\u7528\u4f8b<\/p>\n<p>\/\\(a\\+\\)[^a]\\+\\1 &#8221; \u67e5\u627e\u5f00\u5934\u548c\u7ed3\u5c3e\u5904a\u7684\u4e2a\u6570\u76f8\u540c\u7684\u5b57\u7b26\u4e32\uff0c<br \/>\n&#8221; \u5982 aabbbaa\uff0caaacccaaa\uff0c\u4f46\u662f\u4e0d\u5339\u914d abbbaa<br \/>\n:s\/\\(http:\\\/\\\/[-a-z\\._~\\+%\\\/]\\+\\)\/&lt;a href=&#8221;\\1&#8243;&gt;\\1&lt;\\\/a&gt;\/ &#8221; \u5c06URL\u66ff\u6362\u4e3a&lt;a href=&#8221;http:\/\/url&#8221;&gt;http:\/\/url&lt;\/a&gt;\u7684\u683c\u5f0f<\/p>\n<p>:s\/\\(\\w\\+\\)\\s\\+\\(\\w\\+\\)\/\\2\\t\\1 &#8221; \u5c06 data1 data2 \u4fee\u6539\u4e3a data2 data1<\/p>\n<p>\u56db\u3001\u51fd\u6570\u5f0f<\/p>\n<p>\u5728\u66ff\u6362\u547d\u4ee4\u00a0s\/\/\/\u00a0\u4e2d\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u8868\u8fbe\u5f0f\u6765\u4e66\u5199\u66ff\u6362\u5185\u5bb9\uff0c\u683c\u5f0f\u4e3a<\/p>\n<p>:s\/\u66ff\u6362\u5b57\u7b26\u4e32\/\\=\u51fd\u6570\u5f0f<\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u4e2d\u53ef\u4ee5\u4f7f\u7528 submatch(1)\u3001submatch(2) \u7b49\u6765\u5f15\u7528\u00a0\\1\u3001\\2\u00a0\u7b49\u7684\u5185\u5bb9\uff0c\u800csubmatch(0)\u53ef\u4ee5\u5f15\u7528\u5339\u914d\u7684\u6574\u4e2a\u5185\u5bb9\u3002<\/p>\n<p>\u4f7f\u7528\u4f8b<\/p>\n<p>:%s\/\\&lt;id\\&gt;\/\\=line(&#8220;.&#8221;) &#8221; \u5c06\u5404\u884c\u7684 id \u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u884c\u53f7<br \/>\n:%s\/^\\&lt;\\w\\+\\&gt;\/\\=(line(&#8220;.&#8221;)-10) .&#8221;.&#8221;. submatch(1) &#8221; \u5c06\u6bcf\u884c\u5f00\u5934\u7684\u5355\u8bcd\u66ff\u6362\u4e3a (\u884c\u53f7-10).\u5355\u8bcd \u7684\u683c\u5f0f\uff0c<\/p>\n<p>&#8221; \u5982\u7b2c11\u884c\u7684 word \u66ff\u6362\u6210 1. word<\/p>\n<p>\u4e94\u3001\u4e0ePerl\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u533a\u522b<\/p>\n<p>\u5143\u5b57\u7b26\u7684\u533a\u522b<\/p>\n<table border=\"0\" cellspacing=\"1\">\n<thead>\n<tr>\n<td>Vim\u8bed\u6cd5<\/td>\n<td>Perl\u8bed\u6cd5<\/td>\n<td>\u542b\u4e49<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\\+<\/td>\n<td>+<\/td>\n<td>1-\u4efb\u610f\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\?<\/td>\n<td>?<\/td>\n<td>0-1\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\{n,m}<\/td>\n<td>{n,m}<\/td>\n<td>n-m\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\\(\u548c\\)<\/td>\n<td>(\u548c)<\/td>\n<td>\u5206\u7ec4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>vi \u547d\u4ee4 \u4f5c\u7528<\/p>\n<p>:%s\/ *\/ \/g \u628a\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u7a7a\u683c\u66ff\u6362\u4e3a\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n<p>:%s\/ *$\/\/ \u53bb\u6389\u884c\u5c3e\u7684\u6240\u6709\u7a7a\u683c\u3002<\/p>\n<p>:%s\/^\/ \/ \u5728\u6bcf\u4e00\u884c\u5934\u4e0a\u52a0\u5165\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n<p>:%s\/^[0-9][0-9]* \/\/ \u53bb\u6389\u884c\u9996\u7684\u6240\u6709\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<p>:%s\/b[aeio]g\/bug\/g \u5c06\u6240\u6709\u7684bag\u3001beg\u3001big\u548cbog\u6539\u4e3abug\u3002<\/p>\n<p>:%s\/t([aou])g\/h1t\/g \u5c06\u6240\u6709tag\u3001tog\u548ctug\u5206\u522b\u6539\u4e3ahat\u3001hot\u548chug\uff08\u6ce8\u610f\u7528group\u7684\u7528\u6cd5\u548c\u4f7f\u75281\u5f15\u7528\u524d\u9762\u88ab\u5339\u914d\u7684\u5b57\u7b26\uff09\u3002<\/p>\n<p>Sed<\/p>\n<p>Sed\u662fStream EDitor\u7684\u7f29\u5199\uff0c\u662fUnix\u4e0b\u5e38\u7528\u7684\u57fa\u4e8e\u6587\u4ef6\u548c\u7ba1\u9053\u7684\u7f16\u8f91\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u624b\u518c\u4e2d\u5f97\u5230\u5173\u4e8esed\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u8fd9\u91cc\u662f\u4e00\u4e9b\u6709\u8da3\u7684sed\u811a\u672c\uff0c\u5047\u5b9a\u6211\u4eec\u6b63\u5728\u5904\u7406\u4e00\u4e2a\u53eb\u505aprice.txt\u7684\u6587\u4ef6\u3002\u6ce8\u610f\u8fd9\u4e9b\u7f16\u8f91\u5e76\u4e0d\u4f1a\u6539\u53d8\u6e90\u6587\u4ef6\uff0csed\u53ea\u662f\u5904\u7406\u6e90\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u5e76 \u628a\u7ed3\u679c\u663e\u793a\u5728\u6807\u51c6\u8f93\u51fa\u4e2d\uff08\u5f53\u7136\u5f88\u5bb9\u6613\u4f7f\u7528\u91cd\u5b9a\u5411\u6765\u5b9a\u5236\uff09\uff1a<\/p>\n<p>sed\u811a\u672c \u63cf\u8ff0<\/p>\n<p>sed \u2019s\/^$\/d\u2019 price.txt \u5220\u9664\u6240\u6709\u7a7a\u884c<\/p>\n<p>sed \u2019s\/^[ ]*$\/d\u2019 price.txt \u5220\u9664\u6240\u6709\u53ea\u5305\u542b\u7a7a\u683c\u6216\u8005\u5236\u8868\u7b26\u7684\u884c<\/p>\n<p>sed \u2019s\/\u201d\/\/g\u2019 price.txt \u5220\u9664\u6240\u6709\u5f15\u53f7<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u547d\u4ee4 \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u547d\u4ee4\u6700\u5e38\u89c1\u7684\u5c31\u662f\u00a0\/ \uff08\u641c\u7d22\uff09\u547d\u4ee4\u3002\u5176\u683c\u5f0f\u5982\u4e0b\uff1a \/\u6b63\u5219\u8868\u8fbe\u5f0f \u53e6\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1657","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=1657"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1657\/revisions"}],"predecessor-version":[{"id":5152,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1657\/revisions\/5152"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}