{"id":14138,"date":"2014-06-15T01:03:46","date_gmt":"2014-06-14T19:33:46","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14138"},"modified":"2014-06-16T17:33:18","modified_gmt":"2014-06-16T12:03:18","slug":"awk-variables-and-functions","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/awk-variables-and-functions\/","title":{"rendered":"Awk : Variables and Functions"},"content":{"rendered":"<p>Awk as we all know is an interpreted programming language which is used for text processing and editing, manipulating some data in an easy and time saving way. We will go through the context of Awk in a series of two parts and see its power.<\/p>\n<p>Now, lets dive into the Awk world.<\/p>\n<p><strong>Pattern matching and printing:<\/strong><\/p>\n<p>We generally use following syntax of Awk as a command,<br \/>\nawk &#8216;\/search pattern1\/ { Actions 1} \/search pattern 2\/ { Action 2 } filename<\/p>\n<p>for example:<br \/>\nawk &#8216;\/$2~aws\/ { print $0 } employee.txt<\/p>\n<p>Here, if second column in the file is department of employees then output will contain records of all employees whose department is &#8216;aws&#8217;.<\/p>\n<p>Syntax as programming language:<\/p>\n<p>awk<br \/>\n&#8216;BEGIN { Actions}<br \/>\n{ACTION} # Action for every line<br \/>\nEND { Actions }&#8217; filename<\/p>\n<p>Example:<br \/>\nawk &#8216;BEGIN {print &#8220;Name\\t Department\\t Salary&#8221;;}<br \/>\n&gt; {print $1,&#8221;\\t&#8221;,$2,&#8221;\\t&#8221;,$3;}<br \/>\n&gt; END{print &#8220;***end of file***&#8221;;<br \/>\n&gt; }&#8217; employee.txt<\/p>\n<p>Note :<br \/>\n-Columns are represented as $1,$2,$3,etc and $0 represents whole record.<br \/>\n-Awk reads the input file&#8217;s one line at a time.<br \/>\n-Either search pattern or action are optional, But not both.<br \/>\n-Empty braces with out any action does nothing. It wont perform default printing operation.<\/p>\n<p><strong>Awk built-In variables:<\/strong><\/p>\n<p>Awk comes with eight built-in variables that are widely used in scripts.<\/p>\n<p>-FS : Input field separator variable.<br \/>\n-OFS : Output Field Separator Variable.<br \/>\n-RS : Record Separator variable.<br \/>\n-ORS : Output Record Separator Variable.<br \/>\n-NR : Number of Records Variable.<br \/>\n-NF : Number of Fields in a record.<br \/>\n-FILENAME : Name of the current input file.<br \/>\n-FNR : Number of Records relative to the current input file.<\/p>\n<p>Lets understand the use of all these variables with an example :<\/p>\n<p>We know that \/etc\/passwd file contains users&#8217; information in a specific format where each field is record is separated by a newline (\\n) and each field is separated by a colon (:). Created a similar file (here named passwd1) . In my example, passwd1 contains 8 records with 7 fields each.<\/p>\n<p>$ cat \/etc\/passwd1\/<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/awk1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-14150\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/awk1.png\" alt=\"awk1\" width=\"428\" height=\"159\" \/><\/a><\/p>\n<p>Now just create a file that will contain awk code. I named it &#8216;awk_variables.awk&#8217;.<\/p>\n<p>$ vim awk_variables.awk<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/awk2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-14151\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/awk2.png\" alt=\"awk2\" width=\"613\" height=\"213\" \/><\/a><\/p>\n<p>As we can see in the file, ORS is \u201c\\n\\n\u201d and OFS is \u201c=\u201d. Hence , each record must be separated by two newline spaces and each field must be separated by &#8216;=&#8217; symbol in output.<br \/>\nNF must output the value 7 , FILENAME must output &#8216;passwd1&#8217;<\/p>\n<p>we can now execute the code as :<\/p>\n<p>$ awk -f awk_variables.awk passwd1<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/awk3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-14139\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/awk3.png\" alt=\"awk3\" width=\"237\" height=\"487\" \/><\/a><\/p>\n<p>Now, lets move further into awk programming world.<\/p>\n<p>There are nine<strong> built-in function<\/strong> that are provided by awk and are sometimes very useful.<\/p>\n<p>They are :<\/p>\n<p>-int(n) : truncates the float value and outputs only integer part of n .<br \/>\n-log(n) : outputs logarithmic value of n.<br \/>\n-sqrt(n) : outputs square root of given value n.<br \/>\n-exp(n) : gives the exponential value of n i.e. en.<br \/>\n-sin(n) : gives the sine value of n.<br \/>\n-cos(n) : gives the cos value of n.<br \/>\n-atan2(m,n) : function gives you the arc-tangent of m\/n in radians.<br \/>\n-rand() : generates random number between 0 to 1.<br \/>\n-srand(n) : generates random number starting from n. If no argument is passed, it uses time of the day.<\/p>\n<p>We will see more of awk loop structures and arrays in next part of this series. Stay tuned!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Awk as we all know is an interpreted programming language which is used for text processing and editing, manipulating some data in an easy and time saving way. We will go through the context of Awk in a series of two parts and see its power. Now, lets dive into the Awk world. Pattern matching [&hellip;]<\/p>\n","protected":false},"author":97,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1174],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14138"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/97"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14138"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14138\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}