Awk : Variables and Functions

15 / Jun / 2014 by gaurav.dixit 0 comments

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 and printing:

We generally use following syntax of Awk as a command,
awk ‘/search pattern1/ { Actions 1} /search pattern 2/ { Action 2 } filename

for example:
awk ‘/$2~aws/ { print $0 } employee.txt

Here, if second column in the file is department of employees then output will contain records of all employees whose department is ‘aws’.

Syntax as programming language:

awk
‘BEGIN { Actions}
{ACTION} # Action for every line
END { Actions }’ filename

Example:
awk ‘BEGIN {print “Name\t Department\t Salary”;}
> {print $1,”\t”,$2,”\t”,$3;}
> END{print “***end of file***”;
> }’ employee.txt

Note :
-Columns are represented as $1,$2,$3,etc and $0 represents whole record.
-Awk reads the input file’s one line at a time.
-Either search pattern or action are optional, But not both.
-Empty braces with out any action does nothing. It wont perform default printing operation.

Awk built-In variables:

Awk comes with eight built-in variables that are widely used in scripts.

-FS : Input field separator variable.
-OFS : Output Field Separator Variable.
-RS : Record Separator variable.
-ORS : Output Record Separator Variable.
-NR : Number of Records Variable.
-NF : Number of Fields in a record.
-FILENAME : Name of the current input file.
-FNR : Number of Records relative to the current input file.

Lets understand the use of all these variables with an example :

We know that /etc/passwd file contains users’ 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.

$ cat /etc/passwd1/

awk1

Now just create a file that will contain awk code. I named it ‘awk_variables.awk’.

$ vim awk_variables.awk

awk2

As we can see in the file, ORS is ā€œ\n\nā€ and OFS is ā€œ=ā€. Hence , each record must be separated by two newline spaces and each field must be separated by ‘=’ symbol in output.
NF must output the value 7 , FILENAME must output ‘passwd1′

we can now execute the code as :

$ awk -f awk_variables.awk passwd1

awk3

Now, lets move further into awk programming world.

There are nine built-in function that are provided by awk and are sometimes very useful.

They are :

-int(n) : truncates the float value and outputs only integer part of n .
-log(n) : outputs logarithmic value of n.
-sqrt(n) : outputs square root of given value n.
-exp(n) : gives the exponential value of n i.e. en.
-sin(n) : gives the sine value of n.
-cos(n) : gives the cos value of n.
-atan2(m,n) : function gives you the arc-tangent of m/n in radians.
-rand() : generates random number between 0 to 1.
-srand(n) : generates random number starting from n. If no argument is passed, it uses time of the day.

We will see more of awk loop structures and arrays in next part of this series. Stay tuned!!

Tag -

FOUND THIS USEFUL? SHARE IT

Leave a comment -