﻿{"id":4870,"date":"2020-12-23T19:07:46","date_gmt":"2020-12-23T11:07:46","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=4870"},"modified":"2020-12-23T19:07:46","modified_gmt":"2020-12-23T11:07:46","slug":"linux-grep-awk-sed","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/linux-grep-awk-sed.html","title":{"rendered":"Linux\u4e2d\u7684Grep\uff0cAwk\uff0cSed\u547d\u4ee4"},"content":{"rendered":"<p><strong>Linux\u4e2d\u7684Grep\uff0cAwk\u548cSed\u547d\u4ee4\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/p>\n<p>Grep\u547d\u4ee4\u7528\u4e8e\u67e5\u627e\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\uff0c\u5e76\u8f93\u51fa\u5305\u542b\u641c\u7d22\u6a21\u5f0f\u7684\u6240\u6709\u7ed3\u679c\u3002 \u53e6\u4e00\u65b9\u9762\uff0cAwk\u4e5f\u7528\u4e8e\u5728\u6587\u4ef6\u4e2d\u641c\u7d22\u67d0\u4e9b\u6a21\u5f0f\uff0c\u4f46\u4f1a\u7ee7\u7eed\u5728\u6a21\u5f0f\u5339\u914d\u4e0a\u6267\u884c\u67d0\u4e9b\u4efb\u52a1\u3002 \u53ef\u4ee5\u540c\u65f6\u4f7f\u7528Grep\u548cawk\u6765\u7f29\u5c0f\u641c\u7d22\u589e\u5f3a\u7ed3\u679c\u7684\u8303\u56f4\u3002 Grep\u662f\u7528\u4e8e\u5feb\u901f\u641c\u7d22\u5339\u914d\u6a21\u5f0f\u7684\u7b80\u5355\u5de5\u5177\uff0c\u4f46\u662fawk\u66f4\u50cf\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u5e76\u6839\u636e\u8f93\u5165\u503c\u751f\u6210\u8f93\u51fa\u3002 sed\u547d\u4ee4\u5bf9\u4fee\u6539\u6587\u4ef6\u6700\u6709\u7528\u3002 \u5b83\u641c\u7d22\u5339\u914d\u7684\u6a21\u5f0f\u5e76\u66ff\u6362\u5b83\u4eec\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5728Linux\u4e2d\u4f7f\u7528Grep\u547d\u4ee4<\/strong><\/p>\n<p>Grep\u547d\u4ee4\u7528\u4e8e\u67e5\u627e\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u5e76\u663e\u793a\u4e0e\u8be5\u6a21\u5f0f\u5339\u914d\u7684\u6240\u6709\u5b57\u6bb5\u3002 \u641c\u7d22\u7684\u6a21\u5f0f\u901a\u5e38\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u5728\u6211\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u6709\u4e00\u4e2a\u540d\u4e3alinuxmi\u7684\u6587\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">cat linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4873\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/012-1.png\" alt=\"\" width=\"946\" height=\"348\" \/><\/p>\n<p>\u8981\u5728\u6587\u4ef6\u4e2d\u8f93\u51fa\u6bcf\u4e2a\u5305\u542b\u201clinuxmi\u201d\u4e00\u8bcd\u7684\u5b57\u6bb5\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -i &#8220;linuxmi&#8221; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4872\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/011-1.png\" alt=\"\" width=\"946\" height=\"210\" \/><\/p>\n<p>\u4e0a\u9762\u547d\u4ee4\u4e2d\u7684\u201c -i\u201d\u544a\u8bc9grep\u547d\u4ee4\u5ffd\u7565\u4efb\u4f55\u533a\u5206\u5927\u5c0f\u5199\u7684\u547d\u4ee4\u3002<\/p>\n<p>\u201c -c\u201d\u8f93\u51fa\u6587\u4ef6\u4e2d\u5339\u914d\u6a21\u5f0f\u51fa\u73b0\u6b21\u6570\u7684\u8ba1\u6570\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -c &#8220;linuxmi&#8221; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4874\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/013-1.png\" alt=\"\" width=\"946\" height=\"233\" \/><\/p>\n<p>\u8981\u4ece\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u4e2d\u641c\u7d22\uff0c\u6211\u4eec\u4f7f\u7528<strong>&#8216;*&#8217;<\/strong>\u3002\u5b83\u663e\u793a\u4e86\u7279\u5b9a\u7684\u6587\u4ef6\u548c\u5339\u914d\u7ed3\u679c<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep &#8220;linuxmi&#8221; *<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4875\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/014-1.png\" alt=\"\" width=\"946\" height=\"233\" \/><\/p>\n<p>\u8981\u4ec5\u8f93\u51fa\u5305\u542b\u5339\u914d\u6a21\u5f0f\u7684\u6587\u4ef6\uff0c\u6211\u4eec\u4f7f\u7528<strong>\u201c -l\u201d<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4876 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/015-1.png\" alt=\"\" width=\"946\" height=\"233\" \/><\/p>\n<p>\u5982\u679c\u60a8\u77e5\u9053\u6587\u4ef6\u540d\u53ef\u4ee5\u8f93\u51fa\u6240\u6709\u5339\u914d\u9879\uff0c\u5219\u8fd8\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u3002<\/p>\n<p>grep &#8220;linuxmi&#8221; test test1 test2<\/p>\n<p>\u5339\u914d\u6587\u4ef6\u4e2d\u7684\u6574\u4e2a\u5355\u8bcd\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5373\u4f7f\u5728\u5b50\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u4e86Grep\uff0c\u4e5f\u4f1a\u8f93\u51fa\u6240\u6709\u51fa\u73b0\u7684\u67d0\u79cd\u7279\u5b9a\u6a21\u5f0f\u3002\u4e3a\u4e86\u4ec5\u5339\u914d\u6574\u4e2a\u5355\u8bcd\uff0c\u6211\u4eec\u4f7f\u7528<strong>\u201c -w\u201d<\/strong>\u6807\u5fd7<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$<span style=\"color: #ff0000;\"> grep -w &#8220;linuxmi&#8221; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4877\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/017.png\" alt=\"\" width=\"946\" height=\"187\" \/><\/p>\n<p>\u8981\u5728\u641c\u7d22\u4e2d\u5305\u542b\u5b50\u76ee\u5f55\uff0c\u8bf7\u4f7f\u7528<strong>\u201c -r\u201d<\/strong>\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -r &#8220;linuxmi&#8221; *<\/span><\/p>\n<p>\u4ec5\u8f93\u51fa\u5339\u914d\u7684\u6a21\u5f0f\u3002Grep\u663e\u793a\u5305\u542b\u5339\u914d\u6a21\u5f0f\u7684\u6574\u4e2a\u53e5\u5b50\u3002\u6211\u4eec\u53ea\u80fd\u4f7f\u7528<strong>&#8216;-o&#8217;<\/strong>\u5b57\u7b26\u4e32\u6765\u663e\u793a\u641c\u7d22\u6a21\u5f0f\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -o &#8220;linuxmi&#8221; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4878\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/019.png\" alt=\"\" width=\"946\" height=\"187\" \/><\/p>\n<p>\u5e26\u6570\u5b57\u7684\u8f93\u51fa\u662f\u4f7f\u7528<strong>&#8216;-n&#8217;<\/strong>\u7684\u5339\u914d\u6a21\u5f0f\u3002\u8fd9\u5c06\u663e\u793a\u6587\u4ef6\u4e2d\u5339\u914d\u6a21\u5f0f\u6240\u5728\u7684\u7279\u5b9a\u6570\u5b57\u8ba1\u6570\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -n &#8220;linuxmi&#8221; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4879\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/020.png\" alt=\"\" width=\"946\" height=\"187\" \/><\/p>\n<p>\u53cd\u8f6c\u5339\u914d\u4ee5\u4f7f\u7528<strong>&#8216;-v&#8217;<\/strong>\u663e\u793a\u4e0d\u5339\u914d\u8f93\u5165\u6a21\u5f0f\u7684\u8f93\u51fa\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u8f93\u51fa\u4e0d\u5305\u542b\u5355\u8bcd\u201clinuxmi\u201d\u7684\u5176\u4ed6\u4efb\u4f55\u5185\u5bb9\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -v &#8220;linuxmi&#8221; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4880\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/021.png\" alt=\"\" width=\"946\" height=\"279\" \/><\/p>\n<p>\u5339\u914d\u4ee5\u67d0\u4e2a\u5b57\u7b26\u4e32\u5f00\u5934\u7684\u6240\u6709\u5b57\u6bb5\uff0c\u4f8b\u5982\uff0c\u8f93\u51fa\u6240\u6709\u4ee5\u5355\u8bcd\u201csat\u201d\u5f00\u5934\u7684\u5b57\u6bb5<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4881\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/022.png\" alt=\"\" width=\"946\" height=\"164\" \/><\/p>\n<p>\u5339\u914d\u4ee5\u7ed9\u5b9a\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u884c\u3002 \u201c $\u201d\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u884c\u7684\u7ed3\u5c3e\uff0c\u53ef\u7528\u4e8e\u5339\u914d\u4ee5\u7279\u5b9a\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u884c\u3002 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u6ce8\u610f\u5339\u914d\u4ee5\u201c 0\u201d\u7ed3\u5c3e\u7684\u884c\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep &#8220;0$&#8221; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4883\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/023.png\" alt=\"\" width=\"946\" height=\"348\" \/><\/p>\n<p>\u663e\u793a\u5339\u914d\u524d\u540e\u7684\u884c\u6570<\/p>\n<ul>\n<li><strong>-A &#8211; <\/strong>\u6307\u5b9a\u5339\u914d\u540e\u8981\u663e\u793a\u7684\u884c\u6570<\/li>\n<li><strong>-B &#8211; <\/strong>\u6307\u5b9a\u8981\u663e\u793a\u7684\u884c\u6570<\/li>\n<li><strong>-C &#8211; <\/strong>\u6307\u5b9a\u5339\u914d\u4e4b\u524d\u548c\u4e4b\u540e\u8981\u663e\u793a\u7684\u884c\u6570<\/li>\n<\/ul>\n<p>\u4f7f\u7528<strong>\u201c -e\u201d<\/strong>\u5728\u540c\u4e00\u884c\u4e2d\u6307\u5b9a\u591a\u4e2a\u5339\u914d\u6a21\u5f0f<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -e &#8220;linuxmi&#8221; -e &#8220;ubuntu&#8221; -e &#8220;fedora&#8221; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4884\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/024.png\" alt=\"\" width=\"946\" height=\"279\" \/><\/p>\n<p>\u4e3a\u4e86\u5c06grep\u8f93\u51fa\u9650\u5236\u5728\u884c\u6570\u4ee5\u5185\uff0c\u6211\u4eec\u4f7f\u7528&#8217; -m &#8216;<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -m1 &#8220;linuxmi&#8221; linuxmi.txt<\/span><br \/>\nManny linuxmi account 49000<br \/>\nlinuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">grep -m2 &#8220;linuxmi&#8221; linuxmi.txt<\/span><br \/>\nManny linuxmi account 49000<br \/>\ntarun linuxmi sales 15000<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4885\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/025.png\" alt=\"\" width=\"946\" height=\"233\" \/><\/p>\n<h2>\u5728Linux\u4e2d\u4f7f\u7528AWK\u547d\u4ee4<\/h2>\n<p><strong>Awk<\/strong>\u547d\u4ee4\u66f4\u591a\u662f\u7528\u4e8e\u5904\u7406\u6570\u636e\u548c\u751f\u6210\u62a5\u544a\u7684\u811a\u672c\u8bed\u8a00\u3002\u5b83\u4e0d\u9700\u8981\u4efb\u4f55\u7f16\u8bd1\uff0c\u5e76\u4e14\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u6570\u5b57\u51fd\u6570\uff0c\u53d8\u91cf\uff0c\u5b57\u7b26\u4e32\u51fd\u6570\u548c\u903b\u8f91\u8fd0\u7b97\u7b26\u3002\u5b83\u4f7f\u60a8\u80fd\u591f\u4ee5\u8bed\u53e5\u5f62\u5f0f\u7f16\u5199\u7b80\u5355\u6709\u6548\u7684\u7a0b\u5e8f\uff0c\u4ee5\u5728\u6587\u4ef6\u4e2d\u641c\u7d22\u7279\u5b9a\u6a21\u5f0f\uff0c\u5e76\u5728\u627e\u5230\u5339\u914d\u9879\u65f6\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n<p>\u5f53\u4f7f\u7528&#8217;\u00a0<strong>awk<\/strong>\u00a0&#8216;\u65f6\uff0c\u6211\u4eec\u5c06\u82b1\u62ec\u53f7\u62ec\u8d77\u6765\u3002\u6a21\u5f0f\u548c\u52a8\u4f5c\u90fd\u5f62\u6210\u89c4\u5219\uff0c\u6574\u4e2aawk\u7a0b\u5e8f\u90fd\u7528\u5355\u5f15\u53f7\u5f15\u8d77\u6765\u3002<\/p>\n<h4>\u5982\u4f55\u5728Linux\u4e2d\u4f7f\u7528AWK\u547d\u4ee4<\/h4>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cAwk\u547d\u4ee4\u7528\u4e8e\u6253\u5370\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6ca1\u6709\u6307\u5b9a\u6a21\u5f0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u5e94\u7528\u4e8e\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;{print}&#8217; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4886\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/027.png\" alt=\"\" width=\"946\" height=\"233\" \/><\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u6253\u5370\u4e0e\u7ed9\u5b9a\u6a21\u5f0f\u5339\u914d\u7684\u884c\uff0c\u8ba9\u6211\u4eec\u6253\u5370\u5176\u4e2d\u5305\u542b\u5355\u8bcd\u201clinuxmi\u201d\u7684\u6240\u6709\u884c\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;\/linuxmi\/'{print}&#8217; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4888 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/026.png\" alt=\"\" width=\"946\" height=\"325\" \/><\/p>\n<p>Awk\u7528\u4e8e\u5c06\u4e00\u884c\u5206\u5272\u6210\u591a\u4e2a\u5b57\u6bb5\u5e76\u6253\u5370\u51fa\u5185\u5bb9\u3002\u8ba9\u6211\u4eec\u6253\u5370linuxmi.txt\u6587\u4ef6\u4e2d\u884c\u7684\u7b2c\u4e00\u90e8\u5206\u548c\u7b2c\u4e8c\u90e8\u5206\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;{print $1,$2}&#8217; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4887 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/028.png\" alt=\"\" width=\"946\" height=\"348\" \/><\/p>\n<p>\u6ce8\u610f<strong>$0<\/strong>\u8f93\u51fa\u6574\u884c\u3002<\/p>\n<p><strong>NR &#8211; <\/strong>\u6253\u5370\u884c\u4ee5\u53ca\u884c\u53f7\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;{print NR,$0}&#8217; linuxmi.txt<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4889\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2020\/12\/029.png\" alt=\"\" width=\"946\" height=\"348\" \/><\/p>\n<p>\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6307\u5b9a\u4ece\u67d0\u4e2a\u6570\u5b57\u6253\u5370\u5230\u53e6\u4e00\u4e2a\u6570\u5b57\u3002\u8ba9\u6211\u4eec\u663e\u793a\u4ece\u7b2c3\u884c\u5230\u7b2c6\u884c\u3002<\/p>\n<p><strong>NF &#8211; <\/strong>\u8f93\u51fa\u6bcf\u4e2a\u8bb0\u5f55\uff08\u884c\uff09\u4e2d\u5305\u542b\u7684\u5b57\u6bb5\u6570<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;NR==3, NR==6 {print NR,$0}&#8217; linuxmi.txt<\/span><\/p>\n<p><strong>$ NF &#8211; <\/strong>\u6253\u5370\u6700\u540e\u4e00\u5217<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;{print NF}&#8217; linuxmi.txt<\/span><\/p>\n<p><strong>OFS &#8211; <\/strong>\u8f93\u51fa\u5b57\u6bb5\u5206\u9694\u7b26\u2013\u7528\u4e8e\u6307\u5b9a\u8f93\u51fa\u503c\u7684\u5206\u9694\u7b26\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;OFS=&#8221;\/&#8221; {print $1,$4}&#8217; linuxmi.txt<\/span><\/p>\n<h4>Awk BEGIN\u548cEND\u89c4\u5219<\/h4>\n<p><strong>BEGIN<\/strong>\u89c4\u5219\u5e94\u8be5\u5728\u4efb\u4f55\u6587\u672c\u5904\u7406\u4e4b\u524d\u6267\u884c\u4e00\u6b21\uff0c\u5e76\u4e14\u5728\u6267\u884c\u5176\u4ed6\u4efb\u4f55\u64cd\u4f5c\u4e4b\u524d\u5148\u6267\u884c\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u6a21\u5f0f<\/strong><\/p>\n<p>\u5927\u4e8e\u7684\u503c\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u5728\u6211\u4eec\u7684\u6587\u4ef6\u4e2d\uff0c\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u8868\u793a\u85aa\u6c34\u6570\u5b57\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5728\u503c\u5927\u4e8e35000\u65f6\u8f93\u51fa<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;$NF&gt;=35000 {print $1,$4}&#8217; linuxmi.txt<\/span><\/p>\n<h4>\u5185\u7f6e\u51fd\u6570<\/h4>\n<p>awk\u5e26\u6709\u5185\u7f6e\u51fd\u6570\uff0c\u4f8b\u5982\u6570\u503c\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">awk &#8216;BEGIN{print sqrt(400)}&#8217;<\/span><br \/>\n20<\/p>\n<h4>Awk\u811a\u672c<\/h4>\n<p>\u5982\u679c\u53d1\u73b0\u81ea\u5df1\u7f16\u5199\u4e86\u590d\u6742\u7684awk\u547d\u4ee4\uff0c\u5219\u53ef\u4ee5\u9009\u62e9\u5c06\u5b83\u4eec\u653e\u5728\u811a\u672c\u6587\u4ef6\uff08.awk\uff09\u4e2d\u5e76\u4ece\u90a3\u91cc\u8fd0\u884c\u5b83\u4eec\u3002<\/p>\n<h2>\u5728Linux\u4e2d\u4f7f\u7528SED\u547d\u4ee4<\/h2>\n<p><strong>SED<\/strong>\u662f\u6d41\u7f16\u8f91\u5668(stream editor)\u7684\u7f29\u5199\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u641c\u7d22\uff0c\u67e5\u627e\u548c\u66ff\u6362\uff0c\u63d2\u5165\u548c\u5220\u9664\u3002\u4f46\u662f\uff0c\u67e5\u627e\u548c\u66ff\u6362\u5f88\u5e38\u89c1\uff0c\u60a8\u65e0\u9700\u6253\u5f00\u6587\u4ef6\u5373\u53ef\u66ff\u6362\u5355\u8bcd\u3002<\/p>\n<p>\u5c06\u6d4b\u8bd5\u6587\u4ef6\u4e2d\u7684\u201clinuxmi\u201d\u4e00\u8bcd\u66ff\u6362\u4e3a\u201cxxvi\u201d\u5e76\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;s\/linuxmi\/xxvi\/&#8217; linuxmi.txt<\/span><\/p>\n<p>\u4e3a\u4e86\u66ff\u6362\u6bcf\u6b21\u51fa\u73b0\uff0c\u6211\u4eec\u5728\u8868\u8fbe\u5f0f\u7684\u672b\u5c3e\u6dfb\u52a0<strong>\u201c g\u201d<\/strong>\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;s\/linuxmi\/xxvi\/g&#8217; linuxmi.txt<\/span><\/p>\n<p>\u8981\u4ec5\u66ff\u6362\u7279\u5b9a\u6570\u91cf\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u8bf7\u5728<strong>&#8216;g&#8217;<\/strong>\u5904\u6307\u5b9a\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u6700\u591a\u66ff\u6362\u7b2c\u56db\u884c\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;s\/linuxmi\/xxvi\/4&#8217; linuxmi.txt<\/span><\/p>\n<p>\u8981\u4ece\u67d0\u4e2a\u7279\u5b9a\u4f4d\u7f6e\u66ff\u6362\u4e3a\u6587\u4ef6\u7684\u5176\u4f59\u90e8\u5206\uff0c\u8bf7\u6307\u5b9a\u4ee5\u6570\u5b57\u5f00\u5934\u7684\u6570\u5b57\u548c&#8217;g&#8217;\u6765\u8868\u793a\u6587\u4ef6\u7684\u5168\u90e8\u6216\u5176\u4f59\u90e8\u5206\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;s\/linuxmi\/xxvi\/4g&#8217; linuxmi.txt<\/span><\/p>\n<p>\u8981\u4ec5\u66ff\u6362\u7279\u5b9a\u884c\uff0c\u8bf7\u5728\u4e0b\u9762\u7684\u7b2c\u4e09\u884c\u4e2d\u6307\u5b9a\u8981\u66ff\u6362\u7684\u6587\u4ef6\u884c\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;3 s\/linuxmi\/xxvi\/&#8217; linuxmi.txt<\/span><\/p>\n<p>\u8981\u4ec5\u6253\u5370\u66ff\u6362\u7684\u884c\u800c\u4e0d\u662f\u6253\u5370\u6240\u6709\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed -n &#8216;s\/linuxmi\/xxvi\/p&#8217; linuxmi.txt<\/span><\/p>\n<p>\u4ece\u67d0\u4e00\u884c\u5f00\u59cb\u66f4\u6362\uff0c\u4f8b\u5982\uff0c\u4ece\u7b2c3\u884c\u5230\u6700\u540e<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;3,g s\/linuxmi\/xxvi\/&#8217; linuxmi.txt<\/span><\/p>\n<p>\u4f7f\u7528SED\u5220\u9664\u6587\u4ef6\u7684\u884c\u3002\u4f8b\u5982\uff0c\u8981\u5220\u9664\u6587\u4ef6\u7684\u7b2c5\u884c\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;5d&#8217; linuxmi.txt<\/span><\/p>\n<p>\u8981\u5220\u9664\u6700\u540e\u4e00\u884c\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;$d&#8217; linuxmi.txt<\/span><\/p>\n<p>\u4ece\u7b2cn\u884c\u5f00\u59cb\u5220\u9664\u5230\u6700\u540e\uff0c\u4f8b\u5982\uff0c\u4ece\u7b2c3\u884c\u5230\u6700\u540e\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;3,$d&#8217; linuxmi.txt<\/span><\/p>\n<p>\u8981\u5220\u9664\u6a21\u5f0f\u5339\u914d\u6587\u4ef6\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ <span style=\"color: #ff0000;\">sed &#8216;\/pattern\/d&#8217; linuxmi.txt<\/span><\/p>\n<p>\u8fd9\u5c31\u662f\u662f\u6211\u4eec\u6709\u5173\u5982\u4f55\u5728Linux\u4e2d\u4f7f\u7528Grep\uff0cAwk\u548cSed\u547d\u4ee4\u7684\u6307\u5357\u3002\u6211\u5e0c\u671b\u5b83\u80fd\u63d0\u4f9b\u5f88\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u4e2d\u7684Grep\uff0cAwk\u548cSed\u547d\u4ee4\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1f Grep\u547d\u4ee4\u7528\u4e8e\u67e5\u627e\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\uff0c\u5e76 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4870","post","type-post","status-publish","format-standard","hentry","category-linux"],"aioseo_notices":[],"views":1039,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/4870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=4870"}],"version-history":[{"count":5,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/4870\/revisions"}],"predecessor-version":[{"id":4892,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/4870\/revisions\/4892"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=4870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=4870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=4870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}