{"id":2576,"date":"2021-12-01T12:29:59","date_gmt":"2021-12-01T12:29:59","guid":{"rendered":"https:\/\/dft.wiki\/?p=2576"},"modified":"2021-12-01T12:29:59","modified_gmt":"2021-12-01T12:29:59","slug":"awk-cheat-sheet","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2576","title":{"rendered":"AWK Cheat Sheet"},"content":{"rendered":"<p>AWK is a domain-specific language designed for text processing and typically used as data extraction and reporting tool.<\/p>\n<p>In other words, it gets input and shows it in a different way, making it simple to grab only the desired information.<\/p>\n<p>Actually, AWK is much more complex than just grep, sed, or similar Linux commands. It is a complete scripting language.<\/p>\n<hr \/>\n<p>Printing the desired columns:<\/p>\n<pre>awk '{print}' file.txt\r\nawk '{print $1}' file.txt\r\nwho | awk '{print $1,$4}'<\/pre>\n<p>NF means the Number of Field, and can represent the last field:<\/p>\n<pre>awk '{print $NF}' file.txt\r\nawk '{print $(NF-1)}' file.txt<\/pre>\n<p>Setting another delimiter character instead of space:<\/p>\n<pre>awk -F':' '{print $1,$7}' \/etc\/passwd\r\nawk -F: '{print $1,$7}' \/etc\/passwd<\/pre>\n<p>Defining the output field separator:<\/p>\n<pre>date | awk 'OFS=\"\/\" {print $7,$2,$3}'\r\ndate | awk 'OFS=\"-\" {print $7,$2,$3}'<\/pre>\n<p>Executing command before and after:<\/p>\n<pre>who | awk 'BEGIN {print \"Connected users:\"} {print $1,$4} END {print \"End of the list.\"}'<\/pre>\n<p>Adding RegEx patterns (conditions) to the data:<\/p>\n<pre>awk -F: '$3 &gt;= 1000 {print $1,$3}' \/etc\/passwd\r\nawk '\/UUID\/ {print $1}' \/etc\/fstab<\/pre>\n<hr \/>\n<p>A short-list of built-in functions:<\/p>\n<ul>\n<li>Numeric\n<ul>\n<li>atan2(y, x)<\/li>\n<li>cos(<var>x<\/var>)<\/li>\n<li>exp(<var>x<\/var>)<\/li>\n<li>int(<var>x<\/var>)<\/li>\n<li>log(x)<\/li>\n<li>rand()<\/li>\n<li>sin(x)<\/li>\n<li>sqrt(x)<\/li>\n<li>srand([x])<\/li>\n<\/ul>\n<\/li>\n<li>String\n<ul>\n<li>asort(source [, dest [, how ] ])<\/li>\n<li>asorti(source [, dest [, how ] ])<\/li>\n<li>gensub(regexp, replacement, how [, target])<\/li>\n<li>gsub(regexp, replacement [, target])<\/li>\n<li>index(in, find)<\/li>\n<li>length([string])<\/li>\n<li>match(string, regexp [, array])<\/li>\n<li>patsplit(string, array [, fieldpat [, seps ] ])<\/li>\n<li>split(string, array [, fieldsep [, seps ] ])<\/li>\n<li>sprintf(format, expression1, \u2026)<\/li>\n<li>strtonum(str)<\/li>\n<li>sub(regexp, replacement [, target])<\/li>\n<li>substr(string, start [, length ])<\/li>\n<li>tolower(string)<\/li>\n<li>toupper(string)<\/li>\n<\/ul>\n<\/li>\n<li>Input\/output\n<ul>\n<li>close(filename [, how])<\/li>\n<li>fflush([filename])<\/li>\n<li>system(command)<\/li>\n<\/ul>\n<\/li>\n<li>Time\n<ul>\n<li>mktime(datespec [, utc-flag ])<\/li>\n<li>strftime([format [, timestamp [, utc-flag] ] ])<\/li>\n<li>systime()<\/li>\n<\/ul>\n<\/li>\n<li>Bits\n<ul>\n<li>and(v1, v2 [, \u2026])<\/li>\n<li>compl(val)<\/li>\n<li>lshift(val, count)<\/li>\n<li>or(v1, v2 [, \u2026])<\/li>\n<li>rshift(val, count)<\/li>\n<li>xor(v1, v2 [, \u2026])<\/li>\n<\/ul>\n<\/li>\n<li>Type\n<ul>\n<li>isarray(x)<\/li>\n<li>typeof(x)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>awk 'BEGIN {print function(arg1, \"arg2\")}'<\/pre>\n<pre>awk 'length($0) &gt; 80'<\/pre>\n<pre>awk '{ if (length($0) &gt; max) max = length($0) } END { print max }'<\/pre>\n<hr \/>\n<p>Scripting:<\/p>\n<pre>nano cmd.awk<\/pre>\n<pre>#!\/bin\/awk -f\r\nBEGIN { print \"Hello!\" }<\/pre>\n<pre>chmod +x cmd.awk\r\n.\/cmd.awk<\/pre>\n<hr \/>\n<p>Examples:<\/p>\n<pre>#!\/bin\/awk -f\r\nBEGIN { n=1\r\nwhile ( n &lt; 5 )\r\n {\r\n  print n;\r\n  n++;\r\n  }\r\n}<\/pre>\n<pre>#!\/bin\/awk -f\r\nBEGIN {\r\nfor (i=1; i &lt;= 10; i++) {\r\n  printf \"The square of \", i, \" is \", i*i;\r\n  }\r\nexit;\r\n}<\/pre>\n<pre>#!\/bin\/awk -f\r\nBEGIN {\r\n  print \"Start\";\r\n}\r\n{\r\n  print \"Perform\";\r\n}\r\nEND {\r\n  print \"Complete\"\r\n}<\/pre>\n<pre>#!\/bin\/awk -f\r\nBEGIN {\r\n    lines=0;\r\n    total=0;\r\n}\r\n{\r\n    lines++;\r\n    total+=1;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>AWK is a domain-specific language designed for text processing and typically used as data extraction [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2576","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2576"}],"version-history":[{"count":3,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2576\/revisions"}],"predecessor-version":[{"id":2579,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2576\/revisions\/2579"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}