Difference between revisions of "Linux-AWK/C2/Built-in-Functions-in-awk/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script: Built-in functions in awk ''' '''Author: Antara Roy Choudhury''' '''Keywords: awk Built-in function, Arithmetic function, String function, Input/Output f...")
 
Line 17: Line 17:
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 2: Learning Objectives
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 2: Learning Objectives
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In this tutorial we will learn about '''different types of built-in functions''' like-
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In this tutorial we will learn about different types of '''built-in functions''' like-
 
+
  
 
* '''Arithmetic functions'''
 
* '''Arithmetic functions'''
 
* '''String functions'''
 
* '''String functions'''
 
* '''Input/Output functions '''and
 
* '''Input/Output functions '''and
* '''Time stamps functions'''
+
* '''Time-stamp functions'''
  
 
We will do this through some examples.
 
We will do this through some examples.
Line 31: Line 30:
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| To record this tutorial, I am using  
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| To record this tutorial, I am using  
  
 
+
* '''Ubuntu Linux '''16.04 operating system and  
* '''Ubuntu Linux '''16.04 OS and  
+
 
* '''gedit text editor '''3.20.1
 
* '''gedit text editor '''3.20.1
  
Line 61: Line 59:
 
First we will learn about the '''arithmetic functions.'''
 
First we will learn about the '''arithmetic functions.'''
  
 
+
* '''sqrt(x) function''' returns positive '''square root''' of a number '''x'''
* '''sqrt(x) function''' returns positive square root of a number x
+
* '''int(x)''' truncates '''x''' to an integer value
* '''int(x)''' truncates '''x''' to an integer value
+
* '''exp(x)''' gives the exponential of '''x'''
* '''exp(x)''' gives the exponential of x
+
 
+
 
+
  
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 6: Arithmetic Functions
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 6: Arithmetic Functions
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| * '''log(x)''' returns natural logarithm value of x
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"|  
* '''sin(x)''' and '''cos(x)''' give sine(x) and cosine(x) respectively
+
* '''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'''.
 
* Please note that '''argument x '''should be mentioned in '''radians'''.
  
Line 83: Line 79:
  
  
The same is available in the '''Codefiles''' link
+
The same is available in the '''Code Files''' link.
  
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight print statements for sqrt ()
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight print statements for sqrt ()
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Here, we are printing the square root of a positive and negative number respectively.
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Here, we are printing the '''square root''' of a positive and negative number respectively.
  
 
|-
 
|-
Line 103: Line 99:
  
 
log ()
 
log ()
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| After that, natural logarithms of positive and negative numbers are printed.
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| After that, natural '''logarithm''' of positive and negative numbers are printed.
  
 
|-
 
|-
Line 158: Line 154:
  
  
It returns '''nan''' or not a number if the number is negative.
+
It returns '''nan''' or '''not a number''' if the number is negative.
  
  
Line 170: Line 166:
  
  
Natural logarithm of positive number is given by '''log() function'''.
+
Natural '''logarithm''' of positive number is given by '''log() function'''.
  
  
Line 183: Line 179:
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 7: Random Functions
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 7: Random Functions
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now, let’s look at '''random functions.'''
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now, let us look at '''random functions.'''
 
+
  
 
* '''rand()''' returns any random number between 0 and 1.
 
* '''rand()''' returns any random number between 0 and 1.
 
* But never returns 0 or 1.
 
* But never returns 0 or 1.
 
 
  
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 7: Random Functions
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 7: Random Functions
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| * Generated numbers will be random within one '''awk '''execution.
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"|  
* But predictable across different executions of the awk program. 
+
* Generated numbers will be random within one '''awk '''execution.
 
+
* But predictable across different executions of the '''awk''' program.
 
+
  
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 7: Random Functions
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 7: Random Functions
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| * '''srand(x)''' is used to provide '''seed value x''' for '''random function.'''
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"|  
 +
* '''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.'''
 
* In absence of '''x''', date and time of day is used as the '''seed value.'''
  
Line 225: Line 218:
 
So, this code will generate 5 random numbers within 50.
 
So, this code will generate 5 random numbers within 50.
  
 +
|-
 +
|style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"|Switch to terminal and clear it
 +
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the '''terminal''' and execute the file.
  
Switch to the '''terminal''' and execute the file.
+
Let me clear the '''terminal'''.
  
 
|-
 
|-
Line 238: Line 234:
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show the output
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show the output
 
 
Press up key and enter
 
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| See, it is giving 5 random numbers.
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| See, it is giving 5 random numbers.
  
 
+
|-
What happens if I execute the code again?
+
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press up key and Enter
 +
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"|  What happens if I execute the code again?
  
  
Line 256: Line 250:
 
Which means, '''awk '''is generating the same set of random numbers for every execution of the script.
 
Which means, '''awk '''is generating the same set of random numbers for every execution of the script.
  
 
+
|-
 +
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the editor
 +
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| 
 
Then how can we get a new set of random numbers in every execution?
 
Then how can we get a new set of random numbers in every execution?
  
Line 266: Line 262:
  
 
<nowiki>srand() and [enter]</nowiki>
 
<nowiki>srand() and [enter]</nowiki>
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Before the '''for loop''', type '''srand()'''
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Before the '''for loop''', type '''srand() function'''
 
+
  
 +
|-
 +
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press Ctrl+S keys
 +
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"|
 
Press Crtl and S keys to the save the file.
 
Press Crtl and S keys to the save the file.
  
Line 278: Line 276:
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press up key and enter
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press up key and enter
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press the Up arrow key to get the previously executed command and press '''Enter'''.
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press the Up arrow key to get the previously executed command and press '''Enter'''.
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show the output
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now we have a new set of random numbers in the output.
 
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press up key and enter
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Let us execute once again
 
 
 
Press the Up arrow key and press '''Enter'''.
 
  
 
|-
 
|-
Line 302: Line 289:
  
  
'''length(s)''' gives the length of a particular string '''s'''
+
'''length function''' gives the length of a particular string '''s'''
  
  
'''index(s1,s2) function''' determines the position of''' string s2 '''within the larger '''string s1.'''
+
'''index function''' determines the position of''' string s2 '''within the larger '''string s1.'''
  
  
For example, '''index("linux","n")''', returns 3.
+
For example, '''index within parentheses within double quotes linux comma within double quotes n''', returns 3.
  
  
Line 319: Line 306:
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show the file opened and highlight rollnumber column
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show the file opened and highlight rollnumber column
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We know that each student in the '''awkdemo.txt '''file has a 4 digit rollnumber.  
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We know that each student in the '''awkdemo.txt '''file has a 4 digit roll number.  
  
  
Line 328: Line 315:
  
 
|-
 
|-
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In terminal Type:
+
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"|
 +
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the '''terminal '''.
 +
 
 +
Let me clear the '''terminal'''.
 +
 
 +
|-
 +
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In terminal type:
  
 
awk -F"|" 'length($1)!=4 {print}' awkdemo.txt
 
awk -F"|" 'length($1)!=4 {print}' awkdemo.txt
Line 337: Line 330:
  
 
<nowiki>[Enter]</nowiki>
 
<nowiki>[Enter]</nowiki>
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the '''terminal '''and''' '''type the '''command '''as shown here.
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now type the '''command '''as shown here.
  
  
Line 350: Line 343:
 
|-
 
|-
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show the output
 
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show the output
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| See, there is '''one '''roll-number '''S02''' that has been typed incorrectly.
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| See, there is one roll-number '''S02''' that has been typed incorrectly.
  
  
Line 361: Line 354:
  
 
Let me explain the parameters.
 
Let me explain the parameters.
 
  
 
* Here '''s''' is the '''string'''
 
* Here '''s''' is the '''string'''
Line 418: Line 410:
  
 
|-
 
|-
| style="background-color:#9fc5e8;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 10: Split function
+
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 10: Split function
| style="background-color:#9fc5e8;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We have seen the '''function split''' earlier.
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We have seen the '''function split''' earlier.
  
  
 
So, I am not explaining the details here.
 
So, I am not explaining the details here.
  
Please refer to the earlier awk tutorials if you have any doubt.
+
Please refer to the earlier '''awk''' tutorials if you have any doubt.
  
 
|-
 
|-
Line 432: Line 424:
  
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| There are some other '''functions''' which are related to '''Input/Output'''.
 
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| There are some other '''functions''' which are related to '''Input/Output'''.
 
  
 
* '''system() function''' - helps us to run any '''unix command '''within '''awk'''.  
 
* '''system() function''' - helps us to run any '''unix command '''within '''awk'''.  
 
 
  
 
|-
 
|-
Line 448: Line 437:
  
 
<nowiki>[Enter]</nowiki>
 
<nowiki>[Enter]</nowiki>
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In the '''terminal''' type:
+
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In the '''terminal''' type the command as shown here.
 
+
'''awk space''' single quote in capital letters '''BEGIN''' opening curly brace '''system '''inside parentheses and double quotes '''date '''closing curly brace single quotes
+
  
  
Line 497: Line 484:
 
* '''Input/Output functions'''
 
* '''Input/Output functions'''
 
* '''Time stamps functions'''
 
* '''Time stamps functions'''
 
 
  
 
|-
 
|-
Line 510: Line 495:
 
Write an '''awk''' program to print the last '''field''' of every record  
 
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.
+
* where name of the student has small '''u''' as the third letter  
 
+
* using the '''awkdemo.txt '''file.
 
+
  
 
|-
 
|-

Revision as of 17:36, 21 March 2018

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