{"id":3380,"date":"2014-07-23T23:37:47","date_gmt":"2014-07-23T15:37:47","guid":{"rendered":"http:\/\/rmohan.com\/?p=3380"},"modified":"2014-08-09T12:18:55","modified_gmt":"2014-08-09T04:18:55","slug":"awk","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3380","title":{"rendered":"awk"},"content":{"rendered":"<p>USAGE<\/p>\n<p>Unix:     awk &#8216;\/pattern\/ {print &#8220;$1&#8221;}&#8217;    # standard Unix shells<br \/>\nDOS\/Win:  awk &#8216;\/pattern\/ {print &#8220;$1&#8221;}&#8217;    # okay for DJGPP compiled<br \/>\n          awk &#8220;\/pattern\/ {print \\&#8221;$1\\&#8221;}&#8221;  # required for Mingw32<br \/>\nMost of my experience comes from version of GNU awk (gawk) compiled for Win32. Note in particular that DJGPP compilations permit the awk script to follow Unix quoting syntax &#8216;\/like\/ {&#8220;this&#8221;}&#8217;. However, the user must know that single quotes under DOS\/Windows do not protect the redirection arrows (<, >) nor do they protect pipes (|). Both are special symbols for the DOS\/CMD command shell and their special meaning is ignored only if they are placed within &#8220;double quotes.&#8221; Likewise, DOS\/Win users must remember that the percent sign (%) is used to mark DOS\/Win environment variables, so it must be doubled (%%) to yield a single percent sign visible to awk.<\/p>\n<p>If I am sure that a script will NOT need to be quoted in Unix, DOS, or CMD, then I normally omit the quote marks. If an example is peculiar to GNU awk, the command &#8216;gawk&#8217; will be used. Please notify me if you find errors or new commands to add to this list (total length under 65 characters). I usually try to put the shortest script first.<\/p>\n<p>File Spacing<\/p>\n<p>Double space a file<\/p>\n<p> awk &#8216;1;{print &#8220;&#8221;}&#8217;<br \/>\n awk &#8216;BEGIN{ORS=&#8221;\\n\\n&#8221;};1&#8217;<br \/>\nDouble space a file which already has blank lines in it. Output file should contain no more than one blank line between lines of text. NOTE: On Unix systems, DOS lines which have only CRLF (\\r\\n) are often treated as non-blank, and thus &#8216;NF&#8217; alone will return TRUE.<\/p>\n<p>awk &#8216;NF{print $0 &#8220;\\n&#8221;}&#8217;<br \/>\nTriple space a file<\/p>\n<p>awk &#8216;1;{print &#8220;\\n&#8221;}&#8217;<br \/>\nNumbering and Calculations<\/p>\n<p>Precede each line by its line number FOR THAT FILE (left alignment). Using a tab (\\t) instead of space will preserve margins.<\/p>\n<p>awk &#8216;{print FNR &#8220;\\t&#8221; $0}&#8217; files*<br \/>\nPrecede each line by its line number FOR ALL FILES TOGETHER, with tab.<\/p>\n<p>awk &#8216;{print NR &#8220;\\t&#8221; $0}&#8217; files*<br \/>\nNumber each line of a file (number on left, right-aligned) Double the percent signs if typing from the DOS command prompt.<\/p>\n<p>awk &#8216;{printf(&#8220;%5d : %s\\n&#8221;, NR,$0)}&#8217;<br \/>\nNumber each line of file, but only print numbers if line is not blank Remember caveats about Unix treatment of \\r (mentioned above)<\/p>\n<p>awk &#8216;NF{$0=++a &#8221; :&#8221; $0};{print}&#8217;<br \/>\n awk &#8216;{print (NF? ++a &#8221; :&#8221; :&#8221;&#8221;) $0}&#8217;<br \/>\nCount lines (emulates &#8220;wc -l&#8221;)<\/p>\n<p>awk &#8216;END{print NR}&#8217;<br \/>\nPrint the sums of the fields of every line<\/p>\n<p>awk &#8216;{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}'\nAdd all fields in all lines and print the sum\n\nawk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}'\nPrint every line after replacing each field with its absolute value\n\n awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'\n awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }'\nPrint the total number of fields (\"words\") in all lines\n\n awk '{ total = total + NF }; END {print total}' file\nPrint the total number of lines that contain \"Beth\"\n\n awk '\/Beth\/{n++}; END {print n+0}' file\nPrint the largest first field and the line that contains it Intended for finding the longest string in field #1\n\nawk '$1 > max {max=$1; maxline=$0}; END{ print max, maxline}&#8217;<br \/>\nPrint the number of fields in each line, followed by the line<\/p>\n<p>awk &#8216;{ print NF &#8220;:&#8221; $0 } &#8216;<br \/>\nPrint the last field of each line<\/p>\n<p>awk &#8216;{ print $NF }&#8217;<br \/>\nPrint the last field of the last line<\/p>\n<p>awk &#8216;{ field = $NF }; END{ print field }&#8217;<br \/>\nPrint every line with more than 4 fields<\/p>\n<p>awk &#8216;NF > 4&#8217;<br \/>\nPrint every line where the value of the last field is > 4<\/p>\n<p>awk &#8216;$NF > 4&#8217;<br \/>\nText Conversion and Substitution<\/p>\n<p>IN UNIX ENVIRONMENT: convert DOS newlines (CR\/LF) to Unix format<\/p>\n<p>awk &#8216;{sub(\/\\r$\/,&#8221;&#8221;);print}&#8217;   # assumes EACH line ends with Ctrl-M<br \/>\nIN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format<\/p>\n<p>awk &#8216;{sub(\/$\/,&#8221;\\r&#8221;);print}<br \/>\nIN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format<\/p>\n<p>awk 1<br \/>\nIN DOS ENVIRONMENT: convert DOS newlines (CR\/LF) to Unix format Cannot be done with DOS versions of awk, other than gawk:<\/p>\n<p>gawk -v BINMODE=&#8221;w&#8221; &#8216;1&#8217; infile >outfile<br \/>\nUse &#8220;tr&#8221; instead.<\/p>\n<p> tr -d \\r outfile # GNU tr version 1.22 or higher<br \/>\nDelete leading whitespace (spaces, tabs) from front of each line aligns all text flush left<\/p>\n<p>awk &#8216;{sub(\/^[ \\t]+\/, &#8220;&#8221;); print}&#8217;<br \/>\nDelete trailing whitespace (spaces, tabs) from end of each line<\/p>\n<p>awk &#8216;{sub(\/[ \\t]+$\/, &#8220;&#8221;);print}&#8217;<br \/>\nDelete BOTH leading and trailing whitespace from each line<\/p>\n<p>awk &#8216;{gsub(\/^[ \\t]+|[ \\t]+$\/,&#8221;&#8221;);print}&#8217;<br \/>\nawk &#8216;{$1=$1;print}&#8217;           # also removes extra space between fields<br \/>\nInsert 5 blank spaces at beginning of each line (make page offset)<\/p>\n<p>awk &#8216;{sub(\/^\/, &#8221;     &#8220;);print}&#8217;<br \/>\nAlign all text flush right on a 79-column width<\/p>\n<p>awk &#8216;{printf &#8220;%79s\\n&#8221;, $0}&#8217; file*<br \/>\nCenter all text on a 79-character width<\/p>\n<p>awk &#8216;{l=length();s=int((79-l)\/2); printf &#8220;%&#8221;(s+l)&#8221;s\\n&#8221;,$0}&#8217; file*<br \/>\nSubstitute (find and replace) &#8220;foo&#8221; with &#8220;bar&#8221; on each line<\/p>\n<p>awk &#8216;{sub(\/foo\/,&#8221;bar&#8221;);print}&#8217;           # replaces only 1st instance<br \/>\ngawk &#8216;{$0=gensub(\/foo\/,&#8221;bar&#8221;,4);print}&#8217;  # replaces only 4th instance<br \/>\nawk &#8216;{gsub(\/foo\/,&#8221;bar&#8221;);print}&#8217;          # replaces ALL instances in a line<br \/>\nSubstitute &#8220;foo&#8221; with &#8220;bar&#8221; ONLY for lines which contain &#8220;baz&#8221;<\/p>\n<p>awk &#8216;\/baz\/{gsub(\/foo\/, &#8220;bar&#8221;)};{print}&#8217;<br \/>\nSubstitute &#8220;foo&#8221; with &#8220;bar&#8221; EXCEPT for lines which contain &#8220;baz&#8221;<\/p>\n<p>awk &#8216;!\/baz\/{gsub(\/foo\/, &#8220;bar&#8221;)};{print}&#8217;<br \/>\nChange &#8220;scarlet&#8221; or &#8220;ruby&#8221; or &#8220;puce&#8221; to &#8220;red&#8221;<\/p>\n<p>awk &#8216;{gsub(\/scarlet|ruby|puce\/, &#8220;red&#8221;); print}&#8217;<br \/>\nReverse order of lines (emulates &#8220;tac&#8221;)<\/p>\n<p>awk &#8216;{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j&#8211;] }&#8217; file*<br \/>\nIf a line ends with a backslash, append the next line to it (fails if there are multiple lines ending with backslash&#8230;)<\/p>\n<p>awk &#8216;\/\\\\$\/ {sub(\/\\\\$\/,&#8221;&#8221;); getline t; print $0 t; next}; 1&#8217; file*<br \/>\nPrint and sort the login names of all users<\/p>\n<p>awk -F &#8220;:&#8221; &#8216;{ print $1 | &#8220;sort&#8221; }&#8217; \/etc\/passwd<br \/>\nPrint the first 2 fields, in opposite order, of every line<\/p>\n<p>awk &#8216;{print $2, $1}&#8217; file<br \/>\nSwitch the first 2 fields of every line<\/p>\n<p>awk &#8216;{temp = $1; $1 = $2; $2 = temp}&#8217; file<br \/>\nPrint every line, deleting the second field of that line<\/p>\n<p>awk &#8216;{ $2 = &#8220;&#8221;; print }&#8217;<br \/>\nPrint in reverse order the fields of every line<\/p>\n<p>awk &#8216;{for (i=NF; i>0; i&#8211;) printf(&#8220;%s &#8220;,i);printf (&#8220;\\n&#8221;)}&#8217; file<br \/>\nRemove duplicate, consecutive lines (emulates &#8220;uniq&#8221;)<\/p>\n<p>awk &#8216;a !~ $0; {a=$0}&#8217;<br \/>\nRemove duplicate, nonconsecutive lines<\/p>\n<p>awk &#8216;! a[$0]++&#8217;                     # most concise script<br \/>\nawk &#8216;!($0 in a) {a[$0];print}&#8217;      # most efficient script<br \/>\nConcatenate every 5 lines of input, using a comma separator between fields<\/p>\n<p>awk &#8216;ORS=%NR%5?&#8221;,&#8221;:&#8221;\\n&#8221;&#8216; file<br \/>\nSelective Printing of Certain Lines<\/p>\n<p>Print first 10 lines of file (emulates behavior of &#8220;head&#8221;)<\/p>\n<p>awk &#8216;NR < 11'\nPrint first line of file (emulates \"head -1\")\n\nawk 'NR>1{exit};1&#8242;<br \/>\nPrint the last 2 lines of a file (emulates &#8220;tail -2&#8221;)<\/p>\n<p>awk &#8216;{y=x &#8220;\\n&#8221; $0; x=$0};END{print y}&#8217;<br \/>\nPrint the last line of a file (emulates &#8220;tail -1&#8221;)<\/p>\n<p>awk &#8216;END{print}&#8217;<br \/>\nPrint only lines which match regular expression (emulates &#8220;grep&#8221;)<\/p>\n<p>awk &#8216;\/regex\/&#8217;<br \/>\nPrint only lines which do NOT match regex (emulates &#8220;grep -v&#8221;)<\/p>\n<p>awk &#8216;!\/regex\/&#8217;<br \/>\nPrint the line immediately before a regex, but not the line containing the regex<\/p>\n<p>awk &#8216;\/regex\/{print x};{x=$0}&#8217;<br \/>\n awk &#8216;\/regex\/{print (x==&#8221;&#8221; ? &#8220;match on line 1&#8221; : x)};{x=$0}&#8217;<br \/>\nPrint the line immediately after a regex, but not the line containing the regex<\/p>\n<p>awk &#8216;\/regex\/{getline;print}&#8217;<br \/>\nGrep for AAA and BBB and CCC (in any order)<\/p>\n<p>awk &#8216;\/AAA\/; \/BBB\/; \/CCC\/&#8217;<br \/>\nGrep for AAA and BBB and CCC (in that order)<\/p>\n<p>awk &#8216;\/AAA.*BBB.*CCC\/&#8217;<br \/>\nPrint only lines of 65 characters or longer<\/p>\n<p>awk &#8216;length > 64&#8217;<br \/>\nPrint only lines of less than 65 characters<\/p>\n<p>awk &#8216;length < 64'\nPrint section of file from regular expression to end of file\n\nawk '\/regex\/,0'\nawk '\/regex\/,EOF'\nPrint section of file based on line numbers (lines 8-12, inclusive)\n\nawk 'NR==8,NR==12'\nPrint line number 52\n\nawk 'NR==52'\nawk 'NR==52 {print;exit}'          # more efficient on large files\nPrint section of file between two regular expressions (inclusive)\n\nawk '\/Iowa\/,\/Montana\/'             # case sensitive\nSelective Deletion of Certain Lines:\n\nDelete ALL blank lines from a file (same as \"grep '.' \")\n\nawk NF\nawk '\/.\/'\n\n\n\n\n\n\nawk one-liner Tips\nPrint column1, column5 and column7 of a data file or output of any columns list\n\n$awk \u2018{print $1, $5, $7}\u2019 data_file\n\n$cat file_name |awk \u2018{print $1 $5 $7}\u2019\n\n$ls \u2013al |awk \u2018{print $1, $5, $7}\u2019 -- Prints file_permissions,size and date\n\nSyntax of running an awk program\n\nAwk \u2018program\u2019 input file(s)\n\nList all files names whose file size greater than zero.\n\n$ls \u2013al |awk \u2018$5 > 0 {print $9}\u2019<\/p>\n<p>List all files whose file size equal to 512bytes.<\/p>\n<p>$ls \u2013al |awk \u2018$5 == 0 {print $9}\u2019<\/p>\n<p>print all lines<\/p>\n<p>$awk \u2018{print }\u2019 file_name<\/p>\n<p>$awk \u2018{print 0}\u2019 file_name<\/p>\n<p>Number of lines in a file<\/p>\n<p>$awk \u2018 END {print NR}\u2019 file_name<\/p>\n<p>Number of columns in each row of a file<\/p>\n<p>$awk \u2018 {print NF\u2019} file_name<\/p>\n<p>Sort the output of file and eliminate duplicate rows<\/p>\n<p>$awk \u2018{print $1, $5, $7}\u2019 |sort \u2013u<\/p>\n<p>List all file names whose file size is greater than 512bytes and owner is &#8220;oracle&#8221;<\/p>\n<p>$ls \u2013al |awk \u2018$3 == &#8220;oracle&#8221; &#038;&#038; $5 > 512 {print $9}\u2019<\/p>\n<p>List all file names whose owner could be either &#8220;oracle&#8221; or &#8220;root&#8221;<\/p>\n<p>$ls \u2013al |awk \u2018$3 == &#8220;oracle&#8221; || $3 == &#8220;root&#8221; {print $9}\u2019<\/p>\n<p>list all the files whose owner is not &#8220;oracle<\/p>\n<p>$ls \u2013al |awk \u2018$3 != &#8220;oracle&#8221; {print $9}\u2019<\/p>\n<p>List all lines which has atlease one or more characters<\/p>\n<p>$awk \u2018NF > 0 {print }\u2019 file_name<\/p>\n<p>List all lines longer that 50 characters<\/p>\n<p>$awk \u2018length($0) > 50 \u2018{print }\u2019 file_name<\/p>\n<p>List first two columns<\/p>\n<p>$awk \u2018{print $1, $2}\u2019 file_name<\/p>\n<p>Swap first two columns of a file and print<\/p>\n<p>$awk \u2018{temp = $1; $1 = $2; $2 = temp; print }\u2019 file_name<\/p>\n<p>Replace first column as &#8220;ORACLE&#8221; in a data file<\/p>\n<p>$awk \u2018{$1 = &#8220;ORACLE&#8221;; print }\u2019 data_file<\/p>\n<p>Remove first column values in a data file<\/p>\n<p>$awk \u2018{$1 =&#8221;&#8221;; print }\u2019 data_file<\/p>\n<p>Calculate total size of a directory in Mb<\/p>\n<p>$ls \u2013al |awk \u2018{total +=$5};END {print &#8220;Total size: &#8221; total\/1024\/1024 &#8221; Mb&#8221;}\u2019<\/p>\n<p>Calculate total size of a directory including sub directories in Mb<\/p>\n<p>$ls \u2013lR |awk \u2018{total +=$5};END {print &#8220;Total size: &#8221; total\/1024\/1024 &#8221; Mb&#8221;}\u2019<\/p>\n<p>Find largest file in a directory including sub directories<\/p>\n<p>$ls \u2013lR |awk \u2018{print $5 &#8220;\\t&#8221; $9}\u2019 |sort \u2013n |tail -1<\/p>\n<p>awk<\/p>\n<p>awk [ -f progfile ] [ -Fc ] [ &#8216;prog&#8217; ] [ parameters ] [ filename ] &#8230;<br \/>\nAn awk program is a sequence of pattern-action commands, each of the form<br \/>\n       pattern{action}<br \/>\nawk scans each input filename (multiple ones can be given and a hyphen, -, can be used as the &#8220;filename&#8221; for standard input) for lines which match patterns specified in a program given either by the prog string (which is enclosed in single quotes) or in the file progfile.<br \/>\nIf no filename present, the program operates on standard input.<br \/>\nFor each pattern in the program, the corresponding action is done on all lines of the input which the pattern matches.<br \/>\n-f progfile<br \/>\nTake the pattern-action statments\/commands from progfile rather than from prog.<br \/>\n-Fc<br \/>\nUse character c, rather than space\/blank as the field separator on each line of the input.<br \/>\nawk Commands<\/p>\n<p>Examples<\/p>\n<p>% cat in<br \/>\nAlice   20   4<br \/>\nBob     12   3<br \/>\nCarol   18   3<br \/>\nDave    24   4<br \/>\nEd      30   5<br \/>\n%<br \/>\n% awk &#8216;\/0\/{print}&#8217; in<br \/>\nAlice   20   4<br \/>\nEd      30   5<br \/>\n%<br \/>\n% awk &#8216;{print NR&#8221;:&#8221;$0&#8243; =&#8221;$3&#8243;-&#8220;$2&#8221;-&#8220;$1}&#8217; < in\n1:Alice   20   4 =4-20-Alice\n2:Bob     12   3 =3-12-Bob\n3:Carol   18   3 =3-18-Carol\n4:Dave    24   4 =4-24-Dave\n5:Ed      30   5 =5-30-Ed\n%\n% awk -F2 '{print \"=\"$2\"=\"$1\"=\"}' in\n=0   4=Alice   =\n=   3=Bob     1=\n==Carol   18   3=\n=4   4=Dave    =\n==Ed      30   5=\n%\n% awk 'BEGIN{print \"Hi\"}{s+=$2}END{print \"Sum2=\",s}' in\nHi\nSum2= 104\n%\n% awk '$3==4{s+=$2}END{print s}' in\n44\n%\nls -l | awk '\/^d\/{d++}\/^-\/{f++}END{print d\" dirs + \"f\" files\"}'\n3 dirs + 6 files\n%\n% awk '$2~\/[3-8]\/{print;s+=$3}END{print s}' in\nCarol   18   3\nDave    24   4\nEd      30   5\n12\n%\n% awk '{printf \"%s = %4.1f\\n\",$0,$2\/$3}' in\nAlice   20   4 =  5.0\nBob     12   3 =  4.0\nCarol   18   3 =  6.0\nDave    24   4 =  6.0\nEd      30   5 =  6.0\n%\n% awk '{s+=$2;t+=$3}END{printf \"%4d\/%d=%4.2f\\n\",s,t,s\/t}' in\n 104\/19=5.47\n%\n% awk 'BEGIN{step=2}{n++; if(n>=step){print NR&#8221;: &#8220;$0;n=0}}&#8217; in<\/p>\n<p>    or<\/p>\n<p>% awk &#8216;{n++; if(n>=step){print NR&#8221;: &#8220;$0;n=0}}&#8217; step=2 in<br \/>\n2: Bob     12   3<br \/>\n4: Dave    24   4<br \/>\n%<br \/>\nExplanations<\/p>\n<p>awk &#8216;\/0\/{print}&#8217; in<br \/>\nPrint all lines from the file in which contain a &#8220;0&#8221;<br \/>\nawk &#8216;{print $0 &#8221; = &#8221; $3 &#8220;-&#8221; $2 &#8220;-&#8221; $1}&#8217; < in\nPrint each line preceded by its number (and a colon) and followed by the 3 fields of the line in reverse order, using \"=\" and \"-\" as separators. \nNote: NR = \"Number of this Record,\" a built-in variable.\nawk -F2 '{print \"=\"$2\"=\"$1\"=\"}' in\nUsing \"2\" as a field separator, print fields 2 and 1 in that order, using \"=\" to delimit them.\nawk 'BEGIN{print \"Hi\"}{s+=$2}END{print \"Sum2=\",s}' in\nPrint \"Hi\" at the start, sum up field 2 of each line, and print the sum at the end. \nNote the use of BEGIN and END as special patterns. \nNote also that \"+=\" means that s is to be incremented by the value of the 2nd field and that there is no \"$\" before the s.\nawk '$3==4{s+=$2}END{print s}' in\nPrint the sum of the 2nd field of each line whose 3rd field is 4. \nNote the use of \"==\" to test for equality.\nls -l|awk '\/^d\/{d++}\/^-\/{f++}END{print d\" dirs + \"f\" files\"}'\nCount separately the lines beginning \"d\" and those beginning \"-\" \nNote that ++ means that the variable's value is incremented by 1.\nawk '$2~\/[3-8]\/{print;s+=$3}END{print s}' in\nPrint each line whose 2nd field matches the pattern \"[3-8]\" and then print the sum of the 3rd field of each of these lines.\nawk '{printf \"%s = %4.1f\\n\",$0,$2\/$3}' in\nAppend to each line field 2 divided by field 3.\nawk '{s+=$2;t+=$3}END{printf \"%4d\/%d=%4.2f\\n\",s,t,s\/t}' in\nTotal columns 3 and 4 and print the average of the totals.\n% awk 'BEGIN{step=2}{n++; if(n>=step){print NR&#8221;: &#8220;$0;n=0}}&#8217; in<br \/>\n% awk &#8216;{n++; if(n>=step){print NR&#8221;: &#8220;$0;n=0}}&#8217; step=2 in<br \/>\nPrint, preceded by its line number, every line whose number is a multiple of step.<br \/>\nOne can also do this as follows:<br \/>\n      awk &#8216;{if(step<=++n){print NR\": \"$0;n=0}}' step=2 in \nor even \n      awk '0==NR%step{print NR\": \"$0}' step=2 in\nawk programs\n\nA awk program consists of one or more commands of the form: \n       pattern{action}\nThe action is performed on all lines of the input which match the pattern.\n\nEvery input line matches an empty pattern.\n\nawk patterns\n\nempty\naction is done for each line.\nBEGIN\naction is done before first line.\nEND\naction is done after last line.\n\/RegularExpression\/\naction is done if the line matches RegularExpression.\nPatternMatchingExpression\naction is done if the PatternMatchingExpression is true for this line. \nSuch expressions are composed of the ~ (match) and !~ (no match) operators\nRelationalExpression\naction is done if the RelationalExpression is true for this line. \nThe relational operations are:   <   <=   ==   !=   >=   ><br \/>\nBooleanExpression<br \/>\naction is done if the BooleanExpression is true for this line.<br \/>\nSuch expressions are the combination of pattern matching and relational expressions using boolean operations (&#038;&#038;, ||, and !) and (if\/as needed) parentheses.<br \/>\nawk Built-in Variables<\/p>\n<p>FILENAME<br \/>\nName of the current input file<br \/>\nFS<br \/>\ninput Field Separator regular expression (default blank and tab)<br \/>\nNF<br \/>\nNumber of Fields in the current record<br \/>\nNR<br \/>\nNumber of the current Record<br \/>\nOFMT<br \/>\nOutput ForMaT for numbers (default %.6g)<br \/>\nOFS<br \/>\nOutput Field Separator (default blank)<br \/>\nORS<br \/>\nOutput Record Separator (default newline)<br \/>\nRS<br \/>\ninput Record Separator (default newline)<br \/>\nSpecial Characters and Regular Expressions in awk<\/p>\n<p>\\\tCombines with following character to give it special meaning (see below) or, if it would have had a special meaning without the \\, to make it revert to its literal meaning.<br \/>\n\\a\tAlert\/Bell (CTRL\/G = ASCII 7)<br \/>\n\\b\tBackspace (CTRL\/H = ASCII 8)<br \/>\n\\f\tForm feed (CTRL\/L = ASCII 12)<br \/>\n\\n\tNewline (CTRL\/J = ASCII 10)<br \/>\n\\r\tCarriage return (CTRL\/M = ASCII 13)<br \/>\n\\t\tTab (CTRL\/I = ASCII 9)<br \/>\n\\\/\tLiteral slash (in regular expressions)<br \/>\n\\nnn \tOctal value nnn<br \/>\n.\tMatch any character<br \/>\n^\tMatch start of line<br \/>\n$\tMatch end of line<br \/>\n[&#8230;]\tMatch any character in brackets<br \/>\nExample: [abcA-Z7]<br \/>\n[^&#8230;]  \tMatch any character except those in brackets<br \/>\nExample: [^abcA-Z7]<br \/>\n*\tMatch 0 or more repetitions of previous item<br \/>\n+\tMatch 1 or more repetitions of previous item<br \/>\n?\tMatch 0 or 1 repetitions of previous item<br \/>\n(&#8230;) \tTreat enclosed text as a group\/item<br \/>\n|\tSeparator for items which are considered alternatives.<br \/>\nExample: (NY|LA|SF)<br \/>\nExamples<\/p>\n<p>% cat in<br \/>\nAlice   20   4<br \/>\nBob     12   3<br \/>\nCarol   18   3<br \/>\nDave    24   4<br \/>\nEd      30   5<br \/>\n% awk &#8216;\/^[^A-CE]\/{print}&#8217; in<br \/>\nDave    24   4<br \/>\n% awk &#8216;($1~\/e\/)||(NR>4){print}&#8217; in<br \/>\nAlice   20   4<br \/>\nDave    24   4<br \/>\nEd      30   5<br \/>\n% awk &#8216;($1~\/e\/)&#038;&#038;($2\\!~\/0\/){print}&#8217; in<br \/>\nDave    24   4<br \/>\n% sh<br \/>\n$ awk &#8216;($1~\/e\/)&#038;&#038;($2!~\/0\/){print}&#8217; in<br \/>\nDave    24   4<br \/>\n$ echo $0<br \/>\nsh<br \/>\n$ exit<br \/>\n% echo $0<br \/>\ntcsh<br \/>\n% awk &#8216;{s+=$2*$3}END{print s}&#8217; in<br \/>\n416<br \/>\n% ls -la ~ | awk &#8216;\/^-\/{s+=$5;n++}END{print n&#8221; files, Avg=&#8221;s\/n&#8221; bytes&#8221;}&#8217;<br \/>\n32 files, Avg=1376.21 bytes<br \/>\nawk Arithmetic<\/p>\n<p>+   &#8211;   *   \/   %   ^<br \/>\nAddition, subtraction, multiplication, division, modulus(remainder), exponentiation.<br \/>\n++   &#8212;<br \/>\nIncrement and Decrement the value of a variable: prefix (++x) before the value is used or postfix (x++) after it is used.<br \/>\n=   +=   -=   *=   \/=   %=   ^=<br \/>\nAssignment: x ?= y    is the same as x = x ? y<br \/>\nString concatenation is indicated by a blank or simple juxtaposition when token boundaries are clear.<br \/>\nawk Statements<\/p>\n<p>An awk action is a sequence of statments, each terminated by a semicolon, newline, or right brace.<br \/>\nif ( expression ) statement [else statement ]<br \/>\nwhile ( expression ) statement<br \/>\ndo statement while ( expression ) # In nawk<br \/>\nfor ( expression ;expression ; expression) statement<br \/>\nfor ( var in array ) statement<br \/>\nbreak<br \/>\ncontinue<br \/>\n{ [ statement ] . . . }<br \/>\nexpression # commonly variable = expression<br \/>\nprint [ expression-list ] [ > expression ]<br \/>\nprintf format [ ,expression-list ] [ > expression ]<br \/>\nnext  # skip remaining patterns on this input line<br \/>\nexit [expr] # skip the rest of the input; exit status is expr<br \/>\nExamples<\/p>\n<p>% cat in<br \/>\nAlice   20   4<br \/>\nBob     12   3<br \/>\nCarol   18   3<br \/>\nDave    24   4<br \/>\nEd      30   5<br \/>\n%<br \/>\n% awk &#8216;$2<20{print $1\" is small\"}' in\nBob is small\nCarol is small\n%\n% awk '{if($2<20)print $1\" is small\"}' in\nBob is small\nCarol is small\n%\n% awk '{if($2<20)print $1\" is small \" else print $1 \" is big\"}' in\nawk: syntax error near line 1\nawk: illegal statement near line 1\n%\n% awk '{if($2<20){print $1\" is small \"} else print $1 \" is big\"}' in\nAlice is big\nBob is small\nCarol is small\nDave is big\nEd is big\n%\n% awk '{if ($2<20) print $1\" is small \" else {print $1 \" is big\"}}' in\nawk: syntax error near line 1\nawk: illegal statement near line 1\nawk: syntax error near line 1\nawk: bailing out near line 1\n%\n% awk 'END{while(i++<3)print i}' < \/dev\/null\n1\n2\n3\n%\n% awk 'END{while(++i<3)print i}' < \/dev\/null\n1\n2\n%\n% awk 'END{do print i}while (++i<3)}' < \/dev\/null\nawk: syntax error near line 1\nawk: illegal statement near line 1\nawk: bailing out near line 1\n%\n% nawk 'END{do{print i}while (++i<3)}' < \/dev\/null\n\n1\n2\n%\n% nawk 'END{while(++i<3)print i}' < \/dev\/null\n1\n2\n%\n% awk 'END{for(i=0;i<3;i++)print i}' < \/dev\/null\n0\n1\n2\n%\n% awk 'END{for(;i<3;i++)print i}' < \/dev\/null\n\n1\n2\n%\n%\n% cat in2\nAlice:20:4\nBob:12:3\nCarol:18:3\nDave:24:4\nEd:30:5\n%\n% cat 2.awk\n\/^[A-C]\/{print \"A-C: \"$0}\n\/e\/{print \"--e: \"$0}\n%\n% awk -F':' -f 2.awk in2\nA-C: Alice:20:4\n--e: Alice:20:4\nA-C: Bob:12:3\nA-C: Carol:18:3\n--e: Dave:24:4\n%\n% cat 2n.awk\n\/^[A-C]\/{print \"A-C: \"$0; next}\n\/e\/{print \"--e: \"$0}\n%\n% awk -F: -f 2n.awk in2\nA-C: Alice:20:4\nA-C: Bob:12:3\nA-C: Carol:18:3\n--e: Dave:24:4\n%\n% awk -F: '\/o\/{exit}END{print \"Exit at\\n\"NR\"=\"$0}' in2\nExit at\n2=Bob:12:3\n%\nawk Arrays\n\nawk's array variables are associative arrays.\nEach array variable is, in fact, a collection of variables written in the form avar[index] where avar is a name they share and index (called a \"subscript\" or \"index\") can be an integer or string value.\n\nLike all other, user-defined variables, avar[index] is automatically created when it is first used. Thus the array variable avar is created and extended simply by being used.\n\nExamples\n\n% cat in\nAlice   20   4\nBob     12   3\nCarol   18   3\nDave    24   4\nEd      30   5\n%\n%\n% awk '{myline[$1]=NR}END{for(n in myline)print n\"=\"myline[n]}' in\nEd=5\nBob=2\nAlice=1\nDave=4\nCarol=3\n%\n% awk '{name[NR]=$1}END{for(n in name)print n\"=\"name[n]}' in\n2=Bob\n3=Carol\n4=Dave\n5=Ed\n1=Alice\n%\n% awk '{name[NR]=$1}END{for(n=1;n<=NR;n++)print n\"=\"name[n]}' in\n1=Alice\n2=Bob\n3=Carol\n4=Dave\n5=Ed\n%\n% cat in3\nAlice   20\nBob     12\nCarol   18\nDave    24\nEd      30\nAlice   16\nBob     12\nCarol   12\nDave    14\nEd      20\n%\n% awk '{sum[$1]+=$2}END{for(n in sum)print n\"=\"sum[n]}' in3\nEd=50\nBob=24\nAlice=36\nDave=38\nCarol=30\n%\nawk functions\n\nawk has a generous set of built-in functions.\nThe on-line form of Chapter 11: The awk Programming Language of UNIX in a Nutshell: System V Edition, 3rd Edition by Arnold Robbins, has an excellent Group Listing and Alphabetic Summary of awk Functions and Commands.\n\nStandard\/minimal awk doesn't include user-defined functions.\n\nnawk, gawk, and other extended versions of awk do provide user-defined functions.\n\nExamples\n\n% cat in.val\nAlice   1 2 3 4\nBob     2 5 4 1\nCarol   3 2 4 2\nDave    4 3 2 1\nEd      5 3 4 2\n%\n% awk -f valsum.awk in.val\nawk: syntax error near line 4\nawk: bailing out near line 4\n%\n% cat valsum.awk\n# valsum.awk -- sum values in each record\n# input: name followed by a series of values\n\nfunction sum(s) {\n  for (i=2; i<=NF; ++i) s+=$i\n  return s\n}\n\n{print($0\" = \" sum())}\n%\n% nawk -f valsum.awk in.val\nAlice   1 2 3 4 = 10\nBob     2 5 4 1 = 12\nCarol   3 2 4 2 = 11\nDave    4 3 2 1 = 10\nEd      5 3 4 2 = 14\n%\n% nawk -f valsort.awk in.val\n  Alice:  1 2 3 4\n    Bob:  1 2 4 5\n  Carol:  2 2 3 4\n   Dave:  1 2 3 4\n     Ed:  2 3 4 5\n%\n% cat valsort.awk\n# valsort.awk -- sort values in each record\n# input: name followed by a series of values\n# based on grade.sort.awk script from chapter 9 of\n# \"sed &#038; awk, 2nd ed,\" Dougherty &#038; Robbins, O'Reilly, 1997\n\n# sort function -- sort numbers in ascending order\nfunction sort(A,n) {\n  for (i=2; i<=n; ++i)\n    for (j=i; (j>1) in A &#038;&#038; A[j-1]>A[j]; &#8211;j) {<br \/>\n      tmp=A[j]; A[j]=A[j-1]; A[j-1]=tmp<br \/>\n  }<br \/>\n  return<br \/>\n}<\/p>\n<p># main routine<br \/>\n{<br \/>\nfor (i=2; i<=NF; ++i) val[i-1]=$i\nsort(val, NF-1)\nprintf(\"%7s:  \", $1)\nfor (j=1; j<=NF-1; ++j) printf(\"%d \", val[j])\nprintf(\"\\n\")\n}\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>USAGE<\/p>\n<p>Unix: awk &#8216;\/pattern\/ {print &#8220;$1&#8221;}&#8217; # standard Unix shells DOS\/Win: awk &#8216;\/pattern\/ {print &#8220;$1&#8221;}&#8217; # okay for DJGPP compiled awk &#8220;\/pattern\/ {print \\&#8221;$1\\&#8221;}&#8221; # required for Mingw32 Most of my experience comes from version of GNU awk (gawk) compiled for Win32. Note in particular that DJGPP compilations permit the awk script to follow Unix [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3380"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3380"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3380\/revisions"}],"predecessor-version":[{"id":3443,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3380\/revisions\/3443"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}