{"id":2203,"date":"2015-05-22T15:15:58","date_gmt":"2015-05-22T07:15:58","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2203"},"modified":"2015-05-22T15:15:58","modified_gmt":"2015-05-22T07:15:58","slug":"%e7%94%a8awksedgrep%e8%bf%9b%e8%a1%8c%e6%96%87%e6%9c%ac%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2203.html","title":{"rendered":"\u7528awk\/sed\/grep\u8fdb\u884c\u6587\u672c\u5904\u7406"},"content":{"rendered":"<p>\u7ecf\u5e38\u4f1a\u78b0\u5230\u6587\u672c\u5904\u7406\u7684\u95ee\u9898\uff0c\u4e4b\u524d\u4e5f\u6709\u8fc7\u5f88\u591a\u603b\u7ed3\u4e86\uff0c\u4f46\u77e5\u8bc6\u8fd9\u4e1c\u897f\uff0c\u7ecf\u5e38\u6027\u7684\u8fc7\u51e0\u904d\u8fd8\u662f\u6709\u597d\u5904\u7684\uff08\u56e0\u6b64\u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\uff09\uff0c\u628a\u6700\u8fd1\u7684\u4e00\u4e9b\u5c0f\u77e5\u8bc6\u8bb0\u5f55\u5982\u4e0b\uff1a<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>awk\/sed\/grep\u00a0++\u642d\u914d++ &#8220;cheat sheet&#8221;\/cheatsheet\/\u6e05\u5355\/\u5217\u8868<\/p>\n<h6>\u4ee5\u5f80\u8bb0\u5f55\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/ixyzero.com\/blog\/awk_sed.txt\" target=\"_blank\">http:\/\/ixyzero.com\/blog\/awk_sed.txt<\/a><\/li>\n<li><a href=\"http:\/\/ixyzero.com\/blog\/sed1line.txt\" target=\"_blank\">http:\/\/ixyzero.com\/blog\/sed1line.txt<\/a><\/li>\n<li><a href=\"http:\/\/ixyzero.com\/blog\/regex.html\" target=\"_blank\">http:\/\/ixyzero.com\/blog\/regex.html<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/sparky.rice.edu\/~hartigan\/awk.html\" target=\"_blank\">http:\/\/sparky.rice.edu\/~hartigan\/awk.html<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/linux\/library\/l-awk1\/index.html\">http:\/\/www.ibm.com\/developerworks\/linux\/library\/l-awk1\/index.html<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/linux\/library\/l-awk2\/index.html\">http:\/\/www.ibm.com\/developerworks\/linux\/library\/l-awk2\/index.html<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/linux\/library\/l-awk3\/index.html\">http:\/\/www.ibm.com\/developerworks\/linux\/library\/l-awk3\/index.html<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/chengmo\/archive\/2010\/10\/08\/1846190.html\" target=\"_blank\">linux awk\u6570\u7ec4\u64cd\u4f5c\u8be6\u7ec6\u4ecb\u7ecd &#8211; \u7a0b\u9ed8<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/txsniper\/Note\/blob\/master\/awk.sh\" target=\"_blank\">https:\/\/github.com\/txsniper\/Note\/blob\/master\/awk.sh<\/a><\/li>\n<li><a href=\"http:\/\/bl831.als.lbl.gov\/~gmeigs\/scripting_help\/awk_cheat_sheet.pdf\">http:\/\/bl831.als.lbl.gov\/~gmeigs\/scripting_help\/awk_cheat_sheet.pdf<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/www.grymoire.com\/unix\/SedChart.pdf\">http:\/\/www.grymoire.com\/unix\/SedChart.pdf<\/a><\/li>\n<li><a href=\"http:\/\/blog.51yip.com\/shell\/986.html\" target=\"_blank\">sed\u5f88\u5f3a\u5927\u7684\u6587\u672c\u64cd\u4f5c\u547d\u4ee4<\/a><\/li>\n<li><a href=\"http:\/\/man.linuxde.net\/sed\" target=\"_blank\">sed \u547d\u4ee4\u7528\u6cd5\u8be6\u89e3<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/wirelessqa\/article\/details\/11874227\" target=\"_blank\">\u5f3a\u5927\u7684\u6587\u4ef6\u641c\u7d22\u5de5\u5177grep<\/a><\/li>\n<li><a href=\"http:\/\/www.ahlinux.com\/start\/kernel\/7592.html\" target=\"_blank\">\u4f7f\u7528grep\u3001awk\u7edf\u8ba1\u67e5\u8be2\u65e5\u5fd7<\/a><\/li>\n<\/ul>\n<h6>\u66f4\u591a\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/www.howtoforge.com\/community\/threads\/linux-cheat-sheets-awk-ed-sed-bash-screen-perl-and-more.20009\/\" target=\"_blank\">Linux Cheat Sheets (awk, ed, sed, bash, screen, perl, and more)<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/aix\/library\/au-badunixhabits.html\" target=\"_blank\">UNIX \u9ad8\u624b\u7684 10 \u4e2a\u4e60\u60ef<\/a><\/li>\n<li><a href=\"https:\/\/www.ibm.com\/developerworks\/cn\/aix\/library\/au-unixtips\/\" target=\"_blank\">UNIX \u6280\u5de7: UNIX \u9ad8\u624b\u7684\u53e6\u5916 10 \u4e2a\u4e60\u60ef<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/aix\/library\/au-expressions.html\" target=\"_blank\">\u78e8\u7ec3\u6784\u5efa\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u7684\u6280\u80fd<\/a><\/li>\n<li><a href=\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/sdk\/perl\/l-p101\/\" target=\"_blank\">\u529f\u80fd\u4e30\u5bcc\u7684 Perl: \u4e00\u884c\u7a0b\u5e8f 101<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/linux\/sdk\/perl\/l-p102\/\" target=\"_blank\">\u529f\u80fd\u4e30\u5bcc\u7684 Perl: \u4e00\u884c\u7a0b\u5e8f 102<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-awkinwork\/\" target=\"_blank\">\u5229\u7528 AWK \u7684\u6570\u503c\u8ba1\u7b97\u529f\u80fd\u63d0\u5347\u5de5\u4f5c\u6548\u7387<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/education\/aix\/au-gawk\/index.html\" target=\"_blank\">GAWK \u5165\u95e8\uff1aAWK \u8bed\u8a00\u57fa\u7840<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/mprc.pku.edu.cn\/mentors\/training\/TrainingCourses\/material\/ShellProgramming.HTM\" target=\"_blank\">Shell\u811a\u672c\u7f16\u7a0b\u7684\u5e38\u8bc6<\/a><\/li>\n<li><a href=\"http:\/\/www.1987.name\/277.html\" target=\"_blank\">[Shell\u5b66\u4e60\u7b14\u8bb0] Shell\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0egrep\u3001sed\u3001awk\u7684\u7279\u70b9<\/a><\/li>\n<li><a href=\"http:\/\/www.1987.name\/docs\/shell_regex.html\" target=\"_blank\">Shell\u6b63\u5219\u8868\u8fbe\u5f0f \u5217\u8868<\/a><\/li>\n<li><a href=\"http:\/\/www.1987.name\/287.html\" target=\"_blank\">[Shell\u5b66\u4e60\u7b14\u8bb0] \u5728\u6587\u4ef6\u4e2d\u641c\u7d22\u6587\u672c\u5de5\u5177grep\u547d\u4ee4\u7528\u6cd5<\/a><\/li>\n<li><a href=\"http:\/\/scm.zoomquiet.io\/data\/20120306153355\/index.html\" target=\"_blank\">\u4f7f\u7528 xargs \u547d\u4ee4 &#8211; Linux\u4e2a\u4eba\u7b14\u8bb0<\/a><\/li>\n<\/ul>\n<h6>\u4e00\u4e9b\u5c0f\u6d4b\u8bd5\uff1a<\/h6>\n<pre class=\"lang:default decode:true \">$ grep \"['software_version']\" case.log\n        ...\n        $unshift['software_version'] = '4.0.1';\n\t\t...\n$ grep \"\\['software_version'\\]\" case.log\n        $unshift['software_version'] = '4.0.1';\n$ grep \"\\['software_version'\\]\" case.log | awk '{print $3}'\n'4.0.1';\n$ grep \"\\['software_version'\\]\" case.log | awk '{print $3}' | tr -d \"';\"\n4.0.1\n$ grep \"\\['software_version'\\]\" case.log | awk '{print substr($3,1,length($3)-3)}'\n'4.0.\n$ grep \"\\['software_version'\\]\" case.log | awk '{print substr($3,0,length($3)-2)}'\n'4.0.1\n$ grep \"\\['software_version'\\]\" case.log | awk '{print substr($3,2,length($3)-2)}'\n4.0.1'\n$ grep \"\\['software_version'\\]\" case.log | awk '{print substr($3,2,length($3)-3)}'\n4.0.1\n\n# http:\/\/www.cnblogs.com\/sunada2005\/p\/3493941.html\n# http:\/\/blog.chinaunix.net\/uid-10540984-id-325914.html<\/pre>\n<p>==<\/p>\n<h5>\u7528sed\u8fdb\u884c\u6362\u884c\u7b26(\u2018\\n\u2019)\u7684\u66ff\u6362<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>linux bash convert &#8216;\\n&#8217; to &lt;br\/&gt;<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1251999\/how-can-i-replace-a-newline-n-using-sed\">http:\/\/stackoverflow.com\/questions\/1251999\/how-can-i-replace-a-newline-n-using-sed<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3498799\/unix-commandline-for-inline-replacement-of-all-newlines-in-file-with-br-n\">http:\/\/stackoverflow.com\/questions\/3498799\/unix-commandline-for-inline-replacement-of-all-newlines-in-file-with-br-n<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/26788\/using-sed-to-convert-newlines-into-spaces\">http:\/\/unix.stackexchange.com\/questions\/26788\/using-sed-to-convert-newlines-into-spaces<\/a><\/li>\n<\/ul>\n<h5>\u7528sed\u63d0\u53d6\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u884c<\/h5>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/83329\/how-can-i-extract-a-range-of-lines-from-a-text-file-on-unix\">http:\/\/stackoverflow.com\/questions\/83329\/how-can-i-extract-a-range-of-lines-from-a-text-file-on-unix<\/a><\/p>\n<h5>\u8df3\u8fc7\u524dn\u884c<\/h5>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/6869449\/skipping-the-first-n-lines-when-using-regex-with-sed\">http:\/\/stackoverflow.com\/questions\/6869449\/skipping-the-first-n-lines-when-using-regex-with-sed<\/a><\/p>\n<h5>\u4f7f\u7528sed \u7684-i\u9009\u9879<\/h5>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/5171901\/sed-command-find-and-replace-in-file-and-overwrite-file-doesnt-work-it-empties\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/5171901\/sed-command-find-and-replace-in-file-and-overwrite-file-doesnt-work-it-empties<\/a><\/p>\n<h5>\u4f7f\u7528sed\u7684-r\u9009\u9879<\/h5>\n<pre class=\"lang:default decode:true \">sed\n-r, --regexp-extended\n    use extended regular expressions in the script.\n\n# astr=\"&lt;mac address='52:54:00:a9:cc:20'\/&gt;\"\n# echo $astr\n&lt;mac address='52:54:00:a9:cc:20'\/&gt;\n# echo $astr | sed \"s\/.'(.)'.*\/\\1\/g\"\nsed: -e expression #1, char 15: invalid reference \\1 on `s' command's RHS\n# echo $astr | sed \"s\/.'(.)'.*\/\\\\1\/g\"\nsed: -e expression #1, char 15: invalid reference \\1 on `s' command's RHS\n#\n# echo $astr | sed -r \"s\/.'(.)'.*\/\\1\/g\"\n&lt;mac address='52:54:00:a9:cc:20'\/&gt;\n#\n# echo $astr | sed -e \"s\/.'(.)'.*\/\\1\/g\"\nsed: -e expression #1, char 15: invalid reference \\1 on `s' command's RHS\n#\n# echo $astr | awk -F' '{print $2}'\n&gt; -bash: unexpected EOF while looking for matching `''\n-bash: syntax error: unexpected end of file\n#\n# echo $astr | awk -F\\' '{print $2}'\n52:54:00:a9:cc:20\n#\n# echo $astr | sed -r \"s\/.'(.+)'.*\/\\1\/g\"\n&lt;mac address52:54:00:a9:cc:20\n# echo $astr | sed -r \"s\/.+'(.+)'.*\/\\1\/g\"\n52:54:00:a9:cc:20\n# echo $astr | sed -r \"s\/.+'(.+)'.+\/\\1\/g\"\n52:54:00:a9:cc:20<\/pre>\n<p>==<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ecf\u5e38\u4f1a\u78b0\u5230\u6587\u672c\u5904\u7406\u7684\u95ee\u9898\uff0c\u4e4b\u524d\u4e5f\u6709\u8fc7\u5f88\u591a\u603b\u7ed3\u4e86\uff0c\u4f46\u77e5\u8bc6\u8fd9\u4e1c\u897f\uff0c\u7ecf\u5e38\u6027\u7684\u8fc7\u51e0\u904d\u8fd8\u662f\u6709\u597d\u5904\u7684\uff08\u56e0\u6b64\u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\uff09\uff0c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7,12],"tags":[74,77,78,19],"class_list":["post-2203","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","category-tools","tag-awk","tag-grep","tag-sed","tag-tips"],"views":7641,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2203"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2203\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}