Linux-AWK/C2/Built-in-Functions-in-awk/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Built-in functions in awk

Author: Antara Roy Choudhury

Keywords: awk Built-in function, Arithmetic function, String function, Input/Output function, Timestamps function, length(), substr(), int(), system()


Visual Cue
Narration
Slide 1: Introduction Welcome to this spoken tutorial on built-in functions in awk.
Slide 2: Learning Objectives In this tutorial we will learn about different types of built-in functions like-
  • Arithmetic functions
  • String functions
  • Input/Output functions and
  • Time-stamp functions

We will do this through some examples.

Slide 3: System requirement To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system and
  • gedit text editor 3.20.1

You can use any text editor of your choice.

Slide 4: Prerequisite To practice this tutorial, you should have gone through the earlier awk tutorials on this website.


You should have some knowledge of any programming language like C or C++.


If not, then please go through the corresponding tutorials on our website.

Slide 5: Code Files The files used in this tutorial are available in the Code Files link on this tutorial page.


Please download and extract them.

Slide 6: Arithmetic Functions Built-in functions are always available for awk to call.


First we will learn about the arithmetic functions.

  • sqrt(x) function returns positive square root of a number x
  • int(x) truncates x to an integer value
  • exp(x) gives the exponential of x
Slide 6: Arithmetic Functions
  • log(x) returns natural logarithm value of x
  • sin(x) and cos(x) gives sine(x) and cosine(x) respectively
  • Please note that argument x should be mentioned in radians.

Let’s look at an example to understand these functions.

Show arithmetic_function.awk

in Gedit

I have already written the code in a file arithmetic underscore function dot awk


The same is available in the Code Files link.

Highlight print statements for sqrt () Here, we are printing the square root of a positive and negative number respectively.
Highlight print statements for

int ()

Next we are printing the integer value for a positive and negative number respectively.
Highlight print statements for exp () Then we are printing exponential of a small number and a very large number.
Highlight print statements for

log ()

After that, natural logarithm of positive and negative numbers are printed.
Highlight print statements for sine and cosine () We are also printing sine and cosine values of 0.52 radian, that is actually 30 degree.


Let us execute the file in the terminal.

Open the terminal by pressing Ctrl, Alt and T Keys.
cd /<saved folder> Next go to the folder where you have downloaded and extracted the file using cd command.
Type:

awk -f arithmetic_function.awk

Now type awk space -f arithmetic_function.awk


And press Enter to see the output.


Couple of things are clear from this output.

Highlight Square root of positive


Highlight Square root of negative


Highlight int for positive & negative


Highlight exp of positive


Highlight exp of negative


Highlight log of positive


Highlight log of negative


Highlight sine & cosine

sqrt() function gives square root of a positive number.


It returns nan or not a number if the number is negative.


int() gives the truncated integer of any positive or negative number.


exp() gives exponential of a number.


If the number is very large, the function will return inf.


Natural logarithm of positive number is given by log() function.


If the number if negative, the function returns nan.


Sine and cosine functions return corresponding values.


You can verify the value using your calculator.

Slide 7: Random Functions Now, let us look at random functions.
  • rand() returns any random number between 0 and 1.
  • But never returns 0 or 1.
Slide 7: Random Functions
  • Generated numbers will be random within one awk execution.
  • But predictable across different executions of the awk program.
Slide 7: Random Functions
  • srand(x) is used to provide seed value x for random function.
  • In absence of x, date and time of day is used as the seed value.

Let us understand these with an example.

Show random.awk in Gedit I have written a code for the random function and saved it as random.awk
Highlight rand ()


Highlight rand()*50


Highlight for (i=1;i<=5;i++)

Here, inside the for loop, rand() function will generate a random number between 0 and 1.


Then the generated number will be multiplied by 50 and get printed.


So, this code will generate 5 random numbers within 50.

Switch to terminal and clear it Switch to the terminal and execute the file.

Let me clear the terminal.

Type

awk -f random.awk

Type:

awk space hyphen f space random dot awk and press Enter.

Show the output See, it is giving 5 random numbers.
Press up key and Enter What happens if I execute the code again?


Press the Up arrow key to get the previously executed command and press Enter.

Show the output and highlight We are getting the same output.


Which means, awk is generating the same set of random numbers for every execution of the script.

Switch to the editor

Then how can we get a new set of random numbers in every execution?


Switch to the code once again.

Type

srand() and [enter]

Before the for loop, type srand() function
Press Ctrl+S keys

Press Crtl and S keys to the save the file.

Now switch to the terminal.
Press up key and enter Press the Up arrow key to get the previously executed command and press Enter.
Show the output It is giving a different set of random numbers.


So, we can generate a new set of random numbers using srand function, when it’s used without an argument.

Slide 8: String Functions Next will see some string functions.


length function gives the length of a particular string s


index function determines the position of string s2 within the larger string s1.


For example, index within parentheses within double quotes linux comma within double quotes n, returns 3.


Let us see an example.

show awkdemo.txt in gedit Open the file awkdemo.txt
Show the file opened and highlight rollnumber column We know that each student in the awkdemo.txt file has a 4 digit roll number.


Due to typing error, the roll numbers may have wrong number of digits.


We can easily detect these using awk commands.

Switch to the terminal .

Let me clear the terminal.

In terminal type:

awk -F"|" 'length($1)!=4 {print}' awkdemo.txt


Highlight 'length($1)!=4 {print}'


[Enter]

Now type the command as shown here.


Here we are checking the length of the 1st field is equal to 4 or not.


If not, then that particular record will get printed.


Press Enter.

Show the output See, there is one roll-number S02 that has been typed incorrectly.


It has three digits, whereas all others have four digits.

Slide 9: Substring function The substr(s,a,b) function extracts a substring from a larger string s.


Let me explain the parameters.

  • Here s is the string
  • a denotes the position in s from which the extraction would start
  • b denotes the number of characters that would be extracted.

Let us see one example.

Show awkdemo.txt in Gedit Switch to the awkdemo.txt file.
Show the file opened


The first letter of the roll numbers represents the Hostel code where the particular student resides.


Say we want to find the list of students, who are staying in Hostel A.


To get that, let’s switch to the terminal.

Type

awk -F"|" 'substr($1,1,1)=="A" {print $1,$2}' awkdemo.txt

Type the command as shown here.



Highlight substr($1,1,1) Here we take the string denoted by $1.


As we know $1 represents the first field, that is roll number in our case.

Highlight substr($1,1,1) Next, we extract a substring that starts at position one with the character length one.
Highlight =="A" Then, if it is equal to capital A, then that line from the file will get printed.


Press Enter to see the output.

Show the output We got the list of students who are in Hostel A.
Slide 10: Split function We have seen the function split earlier.


So, I am not explaining the details here.

Please refer to the earlier awk tutorials if you have any doubt.

Slide 11: I/O Functions


There are some other functions which are related to Input/Output.
  • system() function - helps us to run any unix command within awk.
Now, we will run the unix command date through awk command.
Type

awk 'BEGIN{system("date")}'

[Enter]

In the terminal type the command as shown here.


And press Enter.

Show the output Today’s date and time is displayed on the terminal as an output.
Hover your mouse on the command

awk 'BEGIN{system("date")}'

Now, why do we need this?

We have kept only the BEGIN section of the awk command.

Hover your mouse on the output In real world scenarios, we may want to print the system date, before displaying the required output.


In that case, we would need to execute system commands from awk command.

Slide 12: Time Stamps There are some functions dealing with time stamps like
  • systime()
  • strftime()
  • etc.

Browse through the Internet to know about these functions.

Slide 13: Summary This brings us to the end of this tutorial.

Let us summarize.


In this tutorial we learnt about different types of built-in functions like

  • Arithmetic functions
  • String functions
  • Input/Output functions
  • Time stamps functions
Slide 14: Assignment


As an assignment-


Write an awk program to print the last field of every record

  • where name of the student has small u as the third letter
  • using the awkdemo.txt file.
Slide 15:

About Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Slide 16:

Spoken Tutorial workshops

The Spoken Tutorial Project team conducts workshops using spoken tutorials.


And gives certificates on passing online tests.


For more details, please write to us.

Slide 17:

Forum for specific questions:

Please post your timed queries in this forum.
Slide 18: Acknowledgement Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.


More information on this mission is available at

this link.

Slide 19: Thank You The script has been contributed by Antara.


And this is Praveen from IIT Bombay signing off.

Thank you for joining

Contributors and Content Editors

Antarade, Nancyvarkey