{"id":1883,"date":"2012-06-16T16:50:00","date_gmt":"2012-06-16T08:50:00","guid":{"rendered":""},"modified":"2013-11-22T09:14:12","modified_gmt":"2013-11-22T01:14:12","slug":"shell%e4%b8%ad%e7%9a%84%e4%b8%80%e4%b8%aa%e7%b1%bb%e4%bc%bcsubstr%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1883.html","title":{"rendered":"shell\u4e2d\u7684\u4e00\u4e2a\u7c7b\u4f3csubstr\u7684\u7528\u6cd5"},"content":{"rendered":"<p>\u539f\u6765\u5728shell\u4e2d\u5982\u679c\u9047\u5230\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\uff0c\u5c31\u8c03\u7528awk\u4e2d\u7684substr\u6765\u5b9e\u73b0\u3002<br \/>\n\u4eca\u5929\u4e86\u89e3\u5230\u4e00\u79cd\u65b0\u7684\u65b9\u6cd5\uff0c\u539f\u6765shell\u4e2d\u672c\u8eab\u5c31\u652f\u6301\u8fd9\u79cd\u7528\u6cd5\u3002<br \/>\n\u5982\uff0c\u6709\u4e00\u5b57\u7b26\u4e32&#8221;12345678&#8243;\uff0c\u73b0\u5728\u8981\u622a\u53d6\u7b2c\u4e09\u4e2a\u5230\u7b2c\u516d\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u533a\u95f4\u3002<br \/>\n\u5219\u53ef\u4ee5\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;lyon@bss-test6 ~]$ export str=&quot;123456789&quot;\r\n&#x5B;lyon@bss-test6 ~]$ echo ${str:3:(6-3)}\r\n456\r\n&#x5B;lyon@bss-test6 ~]$ unset str\r\n<\/pre>\n<p>\u4ece\u4ee5\u4e0a\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u53ef\u4ee5\u770b\u51fa\u8fd9\u4e2a\u7528\u6cd5\uff0c\u5373\uff1a${str:3:(6-3)}<br \/>\n\u53ef\u4ee5\u5f52\u7eb3\u4e3a\uff1a${str:begin:len}\uff0c\u4e14\u652f\u6301\u8868\u8fbe\u5f0f\uff0c\u5982\uff1a6-3\u3002<\/p>\n<p>\u8865\u5145\u4e00\u4e0b\u4ece\u7f51\u4e0a\u627e\u5230\u4e00\u4e9b\u5176\u4ed6\u7684\u7528\u6cd5\uff1a<\/p>\n<p>shell\u5b57\u7b26\u4e32\u7684\u622a\u53d6\u7684\u95ee\u9898\uff1a<\/p>\n<p>\u4e00\u3001Linux shell \u622a\u53d6\u5b57\u7b26\u53d8\u91cf\u7684\u524d8\u4f4d\uff0c\u6709\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nexpr substr \u201c$a\u201d 1 8\r\necho $a|awk \u2018{print substr(,1,8)}\u2019\r\necho $a|cut -c1-8\r\necho $\r\nexpr $a : \u2018\\(.\\\\).*\u2019\r\necho $a|dd bs=1 count=8 2&amp;gt;\/dev\/null\r\n<\/pre>\n<p>\u4e8c\u3001\u6309\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u622a\u53d6<\/p>\n<p>1\u3001\u7b2c\u4e00\u79cd\u65b9\u6cd5:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n${varible##*string} \u4ece\u5de6\u5411\u53f3\u622a\u53d6\u6700\u540e\u4e00\u4e2astring\u540e\u7684\u5b57\u7b26\u4e32\r\n${varible#*string}\u4ece\u5de6\u5411\u53f3\u622a\u53d6\u7b2c\u4e00\u4e2astring\u540e\u7684\u5b57\u7b26\u4e32\r\n${varible%%string*}\u4ece\u53f3\u5411\u5de6\u622a\u53d6\u6700\u540e\u4e00\u4e2astring\u540e\u7684\u5b57\u7b26\u4e32\r\n${varible%string*}\u4ece\u53f3\u5411\u5de6\u622a\u53d6\u7b2c\u4e00\u4e2astring\u540e\u7684\u5b57\u7b26\u4e32\r\n<\/pre>\n<p>\u201c*\u201d\u53ea\u662f\u4e00\u4e2a\u901a\u914d\u7b26\u53ef\u4ee5\u4e0d\u8981<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ MYVAR=foodforthought.jpg\r\n$ echo ${MYVAR##*fo}\r\nrthought.jpg\r\n$ echo ${MYVAR#*fo}\r\nodforthought.jpg\r\n<\/pre>\n<p>2\u3001\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a${varible:n1:n2}:\u622a\u53d6\u53d8\u91cfvarible\u4ecen1\u5230n2\u4e4b\u95f4\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u5b57\u7b26\u504f\u79fb\u548c\u957f\u5ea6\uff0c\u4f7f\u7528\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684\u53d8\u91cf\u6269\u5c55\uff0c\u6765\u9009\u62e9\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002\u8bd5\u7740\u5728 bash \u4e2d\u8f93\u5165\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ EXCLAIM=cowabunga\r\n$ echo ${EXCLAIM:0:3}\r\ncow\r\n$ echo ${EXCLAIM:3:7}\r\nabunga\r\n<\/pre>\n<p>\u8fd9\u79cd\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\u622a\u65ad\u975e\u5e38\u7b80\u4fbf\uff0c\u53ea\u9700\u7528\u5192\u53f7\u5206\u5f00\u6765\u6307\u5b9a\u8d77\u59cb\u5b57\u7b26\u548c\u5b50\u5b57\u7b26\u4e32\u957f\u5ea6\u3002<\/p>\n<p>\u4e09\u3001\u6309\u7167\u6307\u5b9a\u8981\u6c42\u5206\u5272\uff1a<\/p>\n<p>\u6bd4\u5982\u83b7\u53d6\u540e\u7f00\u540d<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nls -al | cut -d \u201c.\u201d -f2\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6765\u5728shell\u4e2d\u5982\u679c\u9047\u5230\u8981\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\uff0c\u5c31\u8c03\u7528awk\u4e2d\u7684substr\u6765\u5b9e\u73b0\u3002 \u4eca\u5929\u4e86\u89e3\u5230\u4e00\u79cd\u65b0\u7684\u65b9\u6cd5\uff0c\u539f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1883","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1883","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=1883"}],"version-history":[{"count":2,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1883\/revisions"}],"predecessor-version":[{"id":5027,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1883\/revisions\/5027"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}