Linux-AWK/C2/Built-in-Functions-in-awk/English
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()
|
|
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-
We will do this through some examples. |
Slide 3: System requirement | To record this tutorial, I am using
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.
|
Slide 5: Code Files | The files used in this tutorial are available in the Code Files link on this tutorial page.
|
Slide 6: Arithmetic Functions | Built-in functions are always available for awk to call.
|
Slide 6: Arithmetic Functions |
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
|
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.
|
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
|
Highlight Square root of positive
|
sqrt() function gives square root of a positive number.
|
Slide 7: Random Functions | Now, let us look at random functions.
|
Slide 7: Random Functions |
|
Slide 7: Random Functions |
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 ()
|
Here, inside the for loop, rand() function will generate a random number between 0 and 1.
|
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?
|
Show the output and highlight | We are getting the same output.
|
Switch to the editor |
Then how can we get a new set of random numbers in every execution?
|
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.
|
Slide 8: String Functions | Next will see some string functions.
|
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.
|
Switch to the terminal .
Let me clear the terminal. | |
In terminal type:
awk -F"|" 'length($1)!=4 {print}' awkdemo.txt
|
Now type the command as shown here.
|
Show the output | See, there is one roll-number S02 that has been typed incorrectly.
|
Slide 9: Substring function | The substr(s,a,b) function extracts a substring from a larger string s.
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.
|
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.
|
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.
|
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.
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.
|
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.
|
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.
|
Slide 12: Time Stamps | There are some functions dealing with time stamps like
Browse through the Internet to know about these functions. |
Slide 13: Summary | This brings us to the end of this tutorial.
Let us summarize.
|
Slide 14: Assignment
|
As an assignment-
|
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.
|
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.
this link. |
Slide 19: Thank You | The script has been contributed by Antara.
Thank you for joining |