{"id":3995,"date":"2015-09-18T11:35:32","date_gmt":"2015-09-18T11:35:32","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=3995"},"modified":"2015-09-18T13:46:17","modified_gmt":"2015-09-18T13:46:17","slug":"awk","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=3995","title":{"rendered":"awk"},"content":{"rendered":"<p>awk = <strong>A<\/strong>ho, <strong>W<\/strong>einberger, and <strong>K<\/strong>ernighan<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n$0 = whole line\r\n$1 = first field\r\n$2 = second field\r\n&#x5B;...]\r\n$NF = last field\r\nFS = field separator, default FS=\\s+\r\nOFS = output field seperator\r\nRS = record (line) separator\r\nORS = output record seperator\r\nNR = number of records (counter from 1 to n)\r\nNF = total number of fields\r\nFILENAME = name of the current input file\r\nFNR = number of records relative to the current input file\r\nmatch()\r\n  -&gt; RSTART = where the pattern starts\r\n  -&gt; RLENGTH = the length of the pattern\r\n<\/pre>\n<h3>Starters<\/h3>\n<p><a href=\"http:\/\/www.grymoire.com\/Unix\/Awk.html\">Awk &#8211; A Tutorial and Introduction &#8211; by Bruce Barnett<\/a><br \/>\n<a href=\"http:\/\/www.thegeekstuff.com\/2010\/01\/awk-introduction-tutorial-7-awk-print-examples\/\">Awk Introduction Tutorial \u2013 7 Awk Print Examples<\/a><br \/>\n<a href=\"http:\/\/www.catonmat.net\/blog\/ten-awk-tips-tricks-and-pitfalls\/\">10 Awk Tips, Tricks and Pitfalls<\/a><\/p>\n<h3>BEGIN and END<\/h3>\n<p><a href=\"https:\/\/www.gnu.org\/software\/gawk\/manual\/html_node\/Using-BEGIN_002fEND.html\">gawk Manual &#8211; Startup and Cleanup Actions<\/a><br \/>\n<a href=\"http:\/\/code.linuxnix.com\/2013\/02\/awk-scripting-10-begin-and-end-block-examples.html\">AWK scripting: 10 BEGIN and END block examples<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nBEGIN { just once }\r\n{ every record by itself }\r\nEND { just once }\r\n<\/pre>\n<h3>Built-In Variables<\/h3>\n<p><a href=\"http:\/\/www.thegeekstuff.com\/2010\/01\/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr\/\">8 Powerful Awk Built-in Variables \u2013 FS, OFS, RS, ORS, NR, NF, FILENAME, FNR<\/a><\/p>\n<h3>String Functions<\/h3>\n<p><a href=\"https:\/\/www.gnu.org\/software\/gawk\/manual\/html_node\/String-Functions.html\">gawk Manual &#8211; String-Manipulation Functions<\/a><\/p>\n<h3>String concatenate<\/h3>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/27028928\/awk-concatenate-two-string-variable-and-assign-to-a-third\">awk &#8211; concatenate two string variable and assign to a third<\/a><br \/>\n<a href=\"https:\/\/www.gnu.org\/software\/gawk\/manual\/html_node\/Concatenation.html\">gawk Manual &#8211; String Concatenation<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n$ awk '{b=$1$2; print b}' file\r\n$ awk '{b=$1&quot; &quot;$2; print b}' file\r\n$ awk '{b=$1 FS $2; print b}' file\r\n<\/pre>\n<h3>Regex<\/h3>\n<p><a href=\"https:\/\/www.gnu.org\/software\/gawk\/manual\/html_node\/Computed-Regexps.html\">gawk Manual &#8211; Using Dynamic Regexps<\/a><br \/>\n<a href=\"https:\/\/www.gnu.org\/software\/gawk\/manual\/html_node\/Using-Constant-Regexps.html\">gawk Manual &#8211; Using Regular Expression Constants<\/a><br \/>\n<a href=\"http:\/\/unix.stackexchange.com\/questions\/120788\/pass-shell-variable-as-a-pattern-to-awk\">Pass shell variable as a \/pattern\/ to awk<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/11534173\/how-to-use-awk-variables-in-regular-expressions\">How to use awk variables in regular expressions?<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/2227583\/passing-variable-to-awk-and-using-that-in-a-regular-expression\">Passing variable to awk and using that in a regular expression<\/a><\/p>\n<h3>Shell Variables<\/h3>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/15786777\/can-we-use-shell-variables-in-awk\">Can we use shell variables in awk?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>awk = Aho, Weinberger, and Kernighan $0 = whole line $1 = first field $2 = second field &#x5B;&#8230;] $NF = last field FS = field separator, default FS=\\s+ OFS = output field seperator RS = record (line) separator ORS = output record seperator NR = number of records (counter from 1 to n) NF [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3995","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3995"}],"version-history":[{"count":9,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3995\/revisions"}],"predecessor-version":[{"id":4013,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3995\/revisions\/4013"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}