{"id":509,"date":"2009-02-17T11:45:17","date_gmt":"2009-02-17T16:45:17","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=509"},"modified":"2011-03-08T09:45:56","modified_gmt":"2011-03-08T14:45:56","slug":"awk","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=509","title":{"rendered":"awk"},"content":{"rendered":"<p>awk<\/p>\n<p>\u2022fields<br \/>\n$0\twhole line<br \/>\n$1\t1st field<br \/>\n$2\t2nd field<br \/>\nNF\tnumber of fields<br \/>\nNR\tnumber of records<\/p>\n<p>\u2022pattern operators<br \/>\n==<br \/>\n!=<br \/>\n<\n<=\n>=<br \/>\n?:<\/p>\n<p>\u2022Punctuation<br \/>\n{}<br \/>\n$<br \/>\n~\tcontains<br \/>\n!~\tnot contain<br \/>\n,\tseparate things in &#8220;print&#8221;<br \/>\n;\tseparate statements<br \/>\n\/\/\tused around regular expression<br \/>\n()\tgrouping<\/p>\n<p>\u2022they are same<br \/>\nawk &#8216;$1 > $2&#8217;<br \/>\nawk &#8216;$1 > $2{print}&#8217;<br \/>\nawk &#8216;$1 > $2{print $0}&#8217;<\/p>\n<p>\u00a7samples<br \/>\n\u2022Print second field then first<br \/>\nawk &#8216;{print $2,$1}&#8217; file<\/p>\n<p>\u2022Checks for matching string:<br \/>\nawk &#8216;$1==&#8221;foo&#8221;{print $2}&#8217; file<\/p>\n<p>\u2022Search all lines that contain foo*.bar<br \/>\nawk &#8216;\/foo*.bar\/{print $1,$3}&#8217; file<\/p>\n<p>\u2022foo occurs in second field<br \/>\nawk &#8216;$2~\/foo\/{print $3,$1}&#8217; file<\/p>\n<p>\u2022foo not occur in second field<br \/>\nawk &#8216;$2!~\/foo\/{print $3,$1}&#8217; file<\/p>\n<p>\u2022Prints all lines between and including lines that contain foo and bar<br \/>\nawk &#8216;\/foo\/,\/bar\/&#8217; file<\/p>\n<p>\u2022Prints running total of 5th column<br \/>\nls -l | awk &#8216;{print x+=$5,$0}&#8217;<\/p>\n<p>\u00a7BEGIN and END<br \/>\n\u2022awk &#8216;BEGIN{print &#8220;fee&#8221;} $1==&#8221;foo&#8221; {print &#8220;fi&#8221;} END {print &#8220;foo fum&#8221;}&#8217; filename<\/p>\n<p>\u00a7awk variables<br \/>\n\u2022Field separater<br \/>\nawk -F: &#8216;{print $1,$3}&#8217; \/etc\/passwd<\/p>\n<p>\u2022Delete 10th field from each line<br \/>\nawk &#8216;{$10=&#8221;&#8221;; print}&#8217; filename<\/p>\n<p>\u2022C like for, while, do-while, if<br \/>\nawk &#8216;{for(i=1;i<NF;i++) print $i}' filename\n\n\u2022Use printf to format, print w\/o newline\n\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>awk \u2022fields $0 whole line $1 1st field $2 2nd field NF number of fields NR number of records \u2022pattern operators == != < = ?: \u2022Punctuation {} $ ~ contains !~ not contain , separate things in &#8220;print&#8221; ; &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=509\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[65,33],"tags":[],"class_list":["post-509","post","type-post","status-publish","format-standard","hentry","category-shell","category-unix"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s8cRUO-awk","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=509"}],"version-history":[{"count":2,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":2033,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/509\/revisions\/2033"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}