{"id":459,"date":"2013-09-20T00:04:24","date_gmt":"2013-09-20T00:04:24","guid":{"rendered":"https:\/\/shan.info\/new\/%e3%80%90shell%e8%84%9a%e6%9c%ac%e3%80%91%e9%80%90%e8%a1%8c%e5%a4%84%e7%90%86%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6\/"},"modified":"2013-09-20T00:04:24","modified_gmt":"2013-09-20T00:04:24","slug":"%e3%80%90shell%e8%84%9a%e6%9c%ac%e3%80%91%e9%80%90%e8%a1%8c%e5%a4%84%e7%90%86%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/shan.info\/new\/%e3%80%90shell%e8%84%9a%e6%9c%ac%e3%80%91%e9%80%90%e8%a1%8c%e5%a4%84%e7%90%86%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6\/","title":{"rendered":"\u3010Shell\u811a\u672c\u3011\u9010\u884c\u5904\u7406\u6587\u672c\u6587\u4ef6"},"content":{"rendered":"<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u7ecf\u5e38\u4f1a\u5bf9\u6587\u4f53\u6587\u4ef6\u8fdb\u884c\u9010\u884c\u5904\u7406\uff0c\u5728Shell\u91cc\u9762\u5982\u4f55\u83b7\u53d6\u6bcf\u884c\u6570\u636e\uff0c\u7136\u540e\u5904\u7406\u8be5\u884c\u6570\u636e\uff0c\u6700\u540e\u8bfb\u53d6\u4e0b\u4e00\u884c\u6570\u636e\uff0c\u5faa\u73af\u5904\u7406\uff0e\u6709\u591a\u79cd\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\"><strong style=\"margin: 0px; padding: 0px;\">1.\u901a\u8fc7read\u547d\u4ee4\u5b8c\u6210\uff0e<\/strong><\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">read\u547d\u4ee4\u63a5\u6536\u6807\u51c6\u8f93\u5165\uff0c\u6216\u5176\u4ed6\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8f93\u5165\uff0c\u5f97\u5230\u8f93\u5165\u540e\uff0cread\u547d\u4ee4\u5c06\u6570\u636e\u653e\u5165\u4e00\u4e2a\u6807\u51c6\u53d8\u91cf\u4e2d\uff0e<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u5229\u7528read\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u6bcf\u6b21\u8c03\u7528read\u547d\u4ee4\u90fd\u4f1a\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684&#8221;\u4e00\u884c&#8221;\u6587\u672c\uff0e<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u5f53\u6587\u4ef6\u6ca1\u6709\u53ef\u8bfb\u7684\u884c\u65f6\uff0cread\u547d\u4ee4\u5c06\u4ee5\u975e\u96f6\u72b6\u6001\u9000\u51fa\uff0e<\/p>\n<div class=\"cnblogs_code\" style=\"margin: 5px 0px; padding: 5px; background-color: #f5f5f5; font-family: 'Courier New'; font-size: 12px; border: 1px solid #cccccc; overflow: auto;\">\n<div class=\"cnblogs_code_toolbar\" style=\"margin: 5px 0px 0px; padding: 0px;\">&nbsp;<\/div>\n<pre style=\"margin: 0px; padding: 0px; white-space: pre-wrap; font-size: 12px; font-family: 'Courier New';\"><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">1<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">cat<\/span> data.dat | <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">while<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5;\"> read line\r\n<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">2<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">do<\/span>\r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">3<\/span>     <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">echo<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">\"<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">File:${line}<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">\"<\/span>\r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">4<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">done<\/span>\r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">5<\/span> \r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">6<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">while<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5;\"> read line\r\n<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">7<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">do<\/span> \r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">8<\/span>     <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">echo<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">\"<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">File:${line}<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">\"<\/span>\r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">9<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">done<\/span> &lt; data.dat<\/pre>\n<div class=\"cnblogs_code_toolbar\" style=\"margin: 5px 0px 0px; padding: 0px;\">&nbsp;<\/div>\n<\/div>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\"><strong style=\"margin: 0px; padding: 0px;\">2.\u4f7f\u7528awk\u547d\u4ee4\u5b8c\u6210<\/strong><\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">awk\u662f\u4e00\u79cd\u4f18\u826f\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u6781\u5176\u5f3a\u5927\u7684\u529f\u80fd\uff0e<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u5229\u7528awk\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6bcf\u884c\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u5bf9\u6bcf\u884c\u6570\u636e\u505a\u4e00\u4e9b\u5904\u7406\uff0c\u8fd8\u53ef\u4ee5\u5355\u72ec\u5904\u7406\u6bcf\u884c\u6570\u636e\u91cc\u7684\u6bcf\u5217\u6570\u636e\uff0e<\/p>\n<div class=\"cnblogs_code\" style=\"margin: 5px 0px; padding: 5px; background-color: #f5f5f5; font-family: 'Courier New'; font-size: 12px; border: 1px solid #cccccc; overflow: auto;\">\n<pre style=\"margin: 0px; padding: 0px; white-space: pre-wrap; font-size: 12px; font-family: 'Courier New';\"><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">1<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">cat<\/span> data.dat | <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">awk<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">'<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">{print $0}<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">'<\/span>\r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">2<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">cat<\/span> data.dat | <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">awk<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">'<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">for(i=2;i&lt;NF;i++) {printf $i} printf \"n\"}<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">'<\/span><\/pre>\n<\/div>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u7b2c1\u884c\u4ee3\u7801\u8f93\u51fadata.dat\u91cc\u7684\u6bcf\u884c\u6570\u636e\uff0c\u7b2c2\u4ee3\u7801\u8f93\u51fa\u6bcf\u884c\u4e2d\u4ece\u7b2c2\u5217\u4e4b\u540e\u7684\u6570\u636e\uff0e<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u5982\u679c\u662f\u5355\u7eaf\u7684\u6570\u636e\u6216\u6587\u672c\u6587\u4ef6\u7684\u6309\u884c\u8bfb\u53d6\u548c\u663e\u793a\u7684\u8bdd\uff0c\u4f7f\u7528awk\u547d\u4ee4\u6bd4\u8f83\u65b9\u4fbf\uff0e<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\"><strong style=\"margin: 0px; padding: 0px;\">3.\u4f7f\u7528for var in file \u547d\u4ee4\u5b8c\u6210<\/strong><\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">for var in file\u3000\u8868\u793a\u53d8\u91cfvar\u5728file\u4e2d\u5faa\u73af\u53d6\u503c\uff0e\u53d6\u503c\u7684\u5206\u9694\u7b26\u7531$IFS\u786e\u5b9a\uff0e<\/p>\n<div class=\"cnblogs_code\" style=\"margin: 5px 0px; padding: 5px; background-color: #f5f5f5; font-family: 'Courier New'; font-size: 12px; border: 1px solid #cccccc; overflow: auto;\">\n<div class=\"cnblogs_code_toolbar\" style=\"margin: 5px 0px 0px; padding: 0px;\">&nbsp;<\/div>\n<pre style=\"margin: 0px; padding: 0px; white-space: pre-wrap; font-size: 12px; font-family: 'Courier New';\"><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">1<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">for<\/span> line <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">in<\/span> $(<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">cat<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5;\"> data.dat)\r\n<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">2<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">do<\/span> \r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">3<\/span>     <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">echo<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">\"<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">File:${line}<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">\"<\/span>\r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">4<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">done<\/span>\r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">5<\/span> \r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">6<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">for<\/span> line <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">in<\/span> `<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">cat<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5;\"> data.dat`\r\n<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">7<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">do<\/span> \r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">8<\/span>     <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">echo<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">\"<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">File:${line}<\/span><span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #800000;\">\"<\/span>\r\n<span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #008080;\">9<\/span> <span style=\"margin: 0px; padding: 0px; font-size: 12px; line-height: 1.5; color: #0000ff;\">done<\/span><\/pre>\n<div class=\"cnblogs_code_toolbar\" style=\"margin: 5px 0px 0px; padding: 0px;\">&nbsp;<\/div>\n<\/div>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u5982\u679c\u8f93\u5165\u6587\u672c\u6bcf\u884c\u4e2d\u6ca1\u6709\u7a7a\u683c\uff0c\u5219line\u5728\u8f93\u5165\u6587\u672c\u4e2d\u6309\u6362\u884c\u7b26\u5206\u9694\u7b26\u5faa\u73af\u53d6\u503c\uff0e<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u5982\u679c\u8f93\u5165\u6587\u672c\u4e2d\u5305\u62ec\u7a7a\u683c\u6216\u5236\u8868\u7b26\uff0c\u5219\u4e0d\u662f\u6362\u884c\u8bfb\u53d6\uff0cline\u5728\u8f93\u5165\u6587\u672c\u4e2d\u6309\u7a7a\u683c\u5206\u9694\u7b26\u6216\u5236\u8868\u7b26\u6216\u6362\u884c\u7b26\u7279\u73af\u53d6\u503c\uff0e<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\">\u53ef\u4ee5\u901a\u8fc7\u628aIFS\u8bbe\u7f6e\u4e3a\u6362\u884c\u7b26\u6765\u8fbe\u5230\u9010\u884c\u8bfb\u53d6\u7684\u529f\u80fd\uff0e<\/p>\n<p style=\"margin: 10px auto; padding: 0px; font-size: 14px;\"><strong style=\"margin: 0px; padding: 0px;\">IFS\u7684\u9ed8\u8ba4\u503c\u4e3a\uff1a\u7a7a\u767d(\u5305\u62ec\uff1a\u7a7a\u683c\uff0c\u5236\u8868\u7b26\uff0c\u6362\u884c\u7b26)\uff0e<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ecf\u5e38\u4f1a\u5bf9\u6587\u4f53\u6587\u4ef6\u8fdb\u884c\u9010\u884c\u5904\u7406\uff0c\u5728Shell\u91cc\u9762\u5982\u4f55\u83b7\u53d6\u6bcf\u884c\u6570\u636e\uff0c\u7136\u540e\u5904\u7406\u8be5\u884c\u6570\u636e\uff0c\u6700\u540e\u8bfb\u53d6\u4e0b\u4e00\u884c\u6570\u636e\uff0c\u5faa\u73af\u5904\u7406\uff0e\u6709\u591a\u79cd\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a 1.\u901a\u8fc7read\u547d\u4ee4\u5b8c\u6210\uff0e read\u547d\u4ee4\u63a5\u6536\u6807\u51c6\u8f93\u5165\uff0c\u6216\u5176\u4ed6\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8f93\u5165\uff0c\u5f97\u5230\u8f93\u5165\u540e\uff0cread\u547d\u4ee4\u5c06\u6570\u636e\u653e\u5165\u4e00\u4e2a\u6807\u51c6\u53d8\u91cf\u4e2d\uff0e \u5229\u7528read\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u6bcf\u6b21\u8c03\u7528read\u547d\u4ee4\u90fd\u4f1a\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684&#8221;\u4e00\u884c&#8221;\u6587\u672c\uff0e \u5f53\u6587\u4ef6\u6ca1\u6709\u53ef\u8bfb\u7684\u884c\u65f6\uff0cread\u547d\u4ee4\u5c06\u4ee5\u975e\u96f6\u72b6\u6001\u9000\u51fa\uff0e &nbsp; 1 cat data.dat | while read line 2 do 3 echo &#8220;File:${line}&#8221; 4 done 5 6 while read line 7 do 8 echo &#8220;File:${line}&#8221; 9 done &lt; data.dat &nbsp; 2.\u4f7f\u7528awk\u547d\u4ee4\u5b8c\u6210 awk\u662f\u4e00\u79cd\u4f18\u826f\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u6781\u5176\u5f3a\u5927\u7684\u529f\u80fd\uff0e \u5229\u7528awk\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6bcf\u884c\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u5bf9\u6bcf\u884c\u6570\u636e\u505a\u4e00\u4e9b\u5904\u7406\uff0c\u8fd8\u53ef\u4ee5\u5355\u72ec\u5904\u7406\u6bcf\u884c\u6570\u636e\u91cc\u7684\u6bcf\u5217\u6570\u636e\uff0e 1 cat data.dat | awk &#8216;{print $0}&#8217; 2 cat data.dat | awk &#8216;for(i=2;i&lt;NF;i++) {printf $i} printf &#8220;n&#8221;}&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-459","post","type-post","status-publish","format-standard","hentry","category-centos-6"],"_links":{"self":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts\/459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/comments?post=459"}],"version-history":[{"count":0,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts\/459\/revisions"}],"wp:attachment":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/media?parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/categories?post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/tags?post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}