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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 6: Line 6:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Hello and Welcome to this spoken tutorial on''' built-in functions''' in '''awk'''.
+
| Hello and welcome to this '''spoken tutorial''' on''' built-in functions''' in '''awk'''.
  
 
|-
 
|-
 
| 00:07
 
| 00:07
|  In this tutorial we will learn about different types of '''built-in functions''' like-
+
|  In this tutorial, we will learn about different types of '''built-in functions''' like-
  
'''Arithmetic functions'''
+
'''Arithmetic function'''s,
  
 
|-
 
|-
 
| 00:15
 
| 00:15
|'''String functions'''
+
|'''String function'''s,
  
 
|-
 
|-
 
| 00:17
 
| 00:17
|'''Input/Output functions '''and  '''Time-stamp functions'''
+
|'''Input/Output function'''s and   
 +
'''Time-stamp function'''s.
  
 
|-
 
|-
Line 28: Line 29:
 
|-
 
|-
 
| 00:26
 
| 00:26
|  To record this tutorial, I am using  '''Ubuntu Linux '''16.04 operating system and  
+
|  To record this tutorial, I am using: '''Ubuntu Linux '''16.04 '''operating system''' and  
  
 
'''gedit text editor '''3.20.1
 
'''gedit text editor '''3.20.1
Line 34: Line 35:
 
|-
 
|-
 
| 00:38
 
| 00:38
|You can use any text editor of your choice.
+
|You can use any '''text editor''' of your choice.
  
 
|-
 
|-
Line 52: Line 53:
 
|  The files used in this tutorial are available in the '''Code Files''' link on this tutorial page.
 
|  The files used in this tutorial are available in the '''Code Files''' link on this tutorial page.
  
Please download and extract them.
+
Please download and '''extract''' them.
  
 
|-
 
|-
 
| 01:12
 
| 01:12
|'''Built-in functions''' are always available for '''awk''' to call.
+
|'''Built-in functions''' are always available for '''awk''' to '''call'''.
  
 
|-
 
|-
 
| 01:17
 
| 01:17
|First we will learn about the '''arithmetic functions.'''
+
|First we will learn about the '''arithmetic''' functions.
  
'''square root function (sqrt (x))''' returns positive square root of a number '''x'''
+
'''square root function (sqrt (x))''' returns positive square root of a number '''x'''.
  
 
|-
 
|-
 
| 01:27
 
| 01:27
| '''int''' function truncates '''x''' to an integer value
+
| '''int()''' function truncates '''x''' to an integer value.
  
 
|-
 
|-
 
| 01:32
 
| 01:32
| '''exponential  function''' gives the exponential of '''x'''
+
| '''exponential  function''' gives the exponential of '''x'''.
  
 
|-
 
|-
 
| 01:37
 
| 01:37
| '''log function''' returns natural '''logarithm''' value of '''x'''
+
| '''log function''' returns '''natural logarithm''' value of '''x'''.
  
 
|-
 
|-
 
| 01:43
 
| 01:43
| '''sin''' and '''cos''' gives '''sine(x)''' and '''cosine(x)''' respectively
+
| '''sin''' and '''cos''' gives '''sine(x)''' and '''cosine(x)''' respectively.
  
 
|-
 
|-
 
| 01:49
 
| 01:49
| Please note that '''argument x '''should be mentioned in '''radians'''.
+
| Please note that '''argument x '''should be mentioned in '''radian'''s.
  
 
|-
 
|-
 
| 01:55
 
| 01:55
| Let’s look at an example to understand these '''functions'''.
+
| Let’s look at an example to understand these '''function'''s.
  
 
|-
 
|-
 
| 02:00
 
| 02:00
|  I have already written the code in a file '''arithmetic underscore function dot awk'''
+
|  I have already written the '''code''' in a file '''arithmetic underscore function dot awk'''.
  
 
The same is available in the '''Code Files''' link.
 
The same is available in the '''Code Files''' link.
Line 100: Line 101:
 
|-
 
|-
 
| 02:17
 
| 02:17
|  Next we are printing the integer value for a positive and negative number respectively.
+
|  Next, we are printing the integer value for a positive and negative number respectively.
  
 
|-
 
|-
Line 108: Line 109:
 
|-
 
|-
 
| 02:31
 
| 02:31
|  After that, natural '''logarithm''' of positive and negative numbers are printed.
+
|  After that, '''natural logarithm''' of positive and negative numbers are printed.
  
 
|-
 
|-
Line 114: Line 115:
 
|  We are also printing '''sine''' and '''cosine''' values of '''0.52 radian''', that is actually '''30 degree.'''
 
|  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.'''
+
Let us '''execute''' the file in the '''terminal.'''
  
 
|-
 
|-
 
| 02:50
 
| 02:50
|  Open the terminal by pressing Ctrl, Alt and T Keys.
+
|  Open the terminal by pressing '''Ctrl, Alt''' and '''T''' Keys.
  
 
|-
 
|-
 
| 02:55
 
| 02:55
|Next go to the folder where you have downloaded and extracted the file using '''cd''' command.
+
|Next, go to the folder where you have downloaded and extracted the file using '''cd''' command.
  
 
|-
 
|-
 
|03:03
 
|03:03
|  Now type '''awk space -f space arithmetic_function.awk '''
+
|  Now, type: '''awk space -f space arithmetic_function.awk '''
  
And press '''Enter '''to see the output.
+
and press '''Enter '''to see the output.
  
 
|-
 
|-
Line 144: Line 145:
 
|-
 
|-
 
| 03:29
 
| 03:29
| '''int()''' gives the truncated integer of any positive or negative number.
+
| '''int() function''' gives the truncated integer of any positive or negative number.
  
 
|-
 
|-
 
| 03:36
 
| 03:36
| '''exp()''' gives exponential of a number.  
+
| '''exp() function''' gives exponential of a number.  
  
 
If the number is very large, the '''function''' will return '''inf'''.
 
If the number is very large, the '''function''' will return '''inf'''.
Line 154: Line 155:
 
|-
 
|-
 
| 03:47
 
| 03:47
| Natural '''logarithm''' of positive number is given by '''log() function'''.
+
| '''natural logarithm''' of positive number is given by '''log() function'''.
  
 
|-
 
|-
Line 162: Line 163:
 
|-
 
|-
 
| 03:58
 
| 03:58
| '''Sine '''and '''cosine functions '''return corresponding values.
+
| '''Sine '''and '''cosine function'''s return corresponding values.
  
You can verify the value using your calculator.
+
You can verify the values using your calculator.
  
 
|-
 
|-
 
| 04:07
 
| 04:07
|  Now, let us look at '''random functions.'''
+
|  Now, let us look at '''random function'''s.
  
 
|-
 
|-
 
| 04:11
 
| 04:11
|  '''rand()''' returns any random number between 0 and 1. But never returns 0 or 1.
+
|  '''rand() function''' returns any random number between 0 and 1. But never returns 0 or 1.
  
 
|-
 
|-
Line 180: Line 181:
 
|-
 
|-
 
| 04:27
 
| 04:27
|  But predictable across different executions of the '''awk''' program.
+
|  But, predictable across different executions of the '''awk''' program.
  
 
|-
 
|-
 
|04:33
 
|04:33
|  '''srand(x)''' is used to provide '''seed value x''' for '''random function.'''
+
|  '''srand(x) function''' is used to provide '''seed value x''' for '''random function.'''
  
 
|-
 
|-
Line 194: Line 195:
 
|-
 
|-
 
| 04:49
 
| 04:49
|  I have written a code for the '''random function '''and saved it as '''random.awk'''
+
|  I have written a code for the '''random function '''and saved it as '''random.awk'''.
  
 
|-
 
|-
Line 210: Line 211:
 
|-
 
|-
 
|05:16
 
|05:16
|  Switch to the '''terminal''' and execute the file.
+
|  Switch to the '''terminal''' and '''execute''' the file.
  
 
Let me clear the '''terminal'''.
 
Let me clear the '''terminal'''.
Line 228: Line 229:
 
|-
 
|-
 
| 05:39
 
| 05:39
| Press the Up arrow key to get the previously executed command and press '''Enter'''.
+
| Press the '''up arrow key''' to get the previously executed command and press '''Enter'''.
  
 
|-
 
|-
Line 242: Line 243:
 
|-
 
|-
 
| 06:06
 
| 06:06
|  Before the '''for loop''', type '''srand() function'''
+
|  Before the '''for loop''', type '''srand() function'''.
  
 
|-
 
|-
 
| 06:11
 
| 06:11
|  Press Crtl and S keys to the save the file.
+
|  Press '''Ctrl''' and '''S''' keys to '''save''' the file.
  
 
|-
 
|-
 
| 06:16
 
| 06:16
|  Now switch to the '''terminal.'''
+
|  Now, switch to the '''terminal.'''
  
 
|-
 
|-
 
| 06:19
 
| 06:19
|  Press the Up arrow key to get the previously executed command and press '''Enter'''.
+
|  Press the '''up arrow key''' to get the previously executed command and press '''Enter'''.
  
 
|-
 
|-
Line 262: Line 263:
 
|-
 
|-
 
| 06:31
 
| 06:31
| So, we can generate a new set of random numbers using '''srand function''', when it’s used without an '''argument'''.  
+
| So, we can generate a new set of random numbers using '''srand function''' when it’s used without an '''argument'''.  
  
 
|-
 
|-
 
| 06:40
 
| 06:40
|  Next will see some '''string functions'''.  
+
|  Next we will see some '''string functions'''.  
  
'''length function''' gives the length of a particular string '''s'''
+
'''length() function''' gives the length of a particular string '''s'''.
  
 
|-
 
|-
 
| 06:49
 
| 06:49
| '''index 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.'''
  
 
|-
 
|-
 
| 06:57
 
| 06:57
| For example, '''index within parentheses within double quotes linux comma within double quotes n''', returns 3.
+
| For example- '''index within parentheses within double quotes linux comma within double quotes n''', returns 3.
  
 
Let us see an example.
 
Let us see an example.
Line 302: Line 303:
 
|-
 
|-
 
| 07:36
 
| 07:36
|  Now type the '''command '''as shown here.
+
|  Now, type the '''command '''as shown here.
  
 
Here we are checking the length of the 1st field is equal to 4 or not.  
 
Here we are checking the length of the 1st field is equal to 4 or not.  
Line 308: Line 309:
 
|-
 
|-
 
| 07:46
 
| 07:46
| If not, then that particular record will get printed.
+
| If not, then that particular '''record''' will get printed.
  
 
Press '''Enter'''.  
 
Press '''Enter'''.  
Line 326: Line 327:
 
|-
 
|-
 
| 08:14
 
| 08:14
| Let me explain the parameters.
+
| Let me explain the '''parameter'''s.
  
 
|-
 
|-
 
| 08:17
 
| 08:17
|  Here '''s''' is the '''string'''
+
|  Here '''s''' is the '''string'''.
  
 
|-
 
|-
 
| 08:20
 
| 08:20
|  '''a''' denotes the position in '''s '''from which the extraction would start
+
|  '''a''' denotes the position in '''s '''from which the extraction would start.
  
 
|-
 
|-
Line 348: Line 349:
 
|-
 
|-
 
| 08:37
 
| 08:37
The first letter of the roll numbers represents the '''Hostel code''' where the particular student resides.  
+
Here, the first letter of the roll numbers represents the '''Hostel code''' where the particular student resides.  
  
 
|-
 
|-
 
| 08:46
 
| 08:46
| Say we want to find the list of students, who are staying in Hostel '''A'''.
+
| Say, we want to find the list of students who are staying in Hostel '''A'''.
  
 
|-
 
|-
Line 360: Line 361:
 
|-
 
|-
 
| 08:56
 
| 08:56
|  Type the command as shown here.
+
|  Type the '''command''' as shown here.
  
 
|-
 
|-
 
| 09:00
 
| 09:00
|  Here we take the '''string''' denoted by '''$1'''.
+
|  Here, we take the '''string''' denoted by '''$1'''.
  
 
|-
 
|-
 
| 09:05
 
| 09:05
| As we know '''$1''' represents the first '''field''', that is roll number in our case.
+
| As we know, '''$1''' represents the first '''field''' that is roll number in our case.
  
 
|-
 
|-
Line 376: Line 377:
 
|-
 
|-
 
|09:19
 
|09:19
|  Then, if it is equal to capital '''A''', then that line from the file will get printed.
+
|  Then, if it is equal to capital '''A''' then that line from the file will get printed.
  
 
Press '''Enter''' to see the output.
 
Press '''Enter''' to see the output.
Line 386: Line 387:
 
|-
 
|-
 
| 09:34
 
| 09:34
|  We have seen the '''function split''' earlier.
+
|  We have seen the '''function split()''' earlier.
  
 
So, I am not explaining the details here.
 
So, I am not explaining the details here.
Line 398: Line 399:
 
|  There are some other '''functions''' which are related to '''Input/Output'''.
 
|  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 406: Line 407:
 
|-
 
|-
 
|10:01
 
|10:01
|  In the '''terminal''' type the command as shown here.
+
|  In the '''terminal''', type the command as shown here.
  
 
And press '''Enter.'''
 
And press '''Enter.'''
Line 428: Line 429:
 
|-
 
|-
 
| 10:34
 
| 10:34
|  There are some '''functions''' dealing with '''time stamps''' like
+
|  There are some '''functions''' dealing with '''time stamps''' like -
  
'''systime()''',  '''strftime() '''etc.
+
'''systime()''',  '''strftime() ''' etc.
  
 
|-
 
|-
Line 444: Line 445:
 
|-
 
|-
 
| 10:53
 
| 10:53
| In this tutorial we learnt about different types of '''built-in functions''' like
+
| In this tutorial, we learnt about different types of '''built-in functions''' like-
  
'''Arithmetic functions''',  '''String functions''',  '''Input/Output functions''' and  '''Time stamps functions'''
+
'''Arithmetic functions''',   
 +
'''String functions''',   
 +
'''Input/Output functions''' and   
 +
'''Time stamps functions.'''
  
 
|-
 
|-
 
| 11:06
 
| 11:06
|  As an assignment-Write an '''awk''' program to print the last '''field''' of every record  
+
|  As an assignment-
 +
Write an '''awk''' program to print the last '''field''' of every record  
  
 
|-
 
|-
 
| 11:13
 
| 11:13
|  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.
  
 
|-
 
|-
 
| 11:22
 
| 11:22
|  The video at the following link summarises the Spoken Tutorial project.
+
|  The video at the following link summarises the '''Spoken Tutorial project'''.
  
 
Please download and watch it.
 
Please download and watch it.
Line 466: Line 471:
 
|  The '''Spoken Tutorial Project''' team conducts workshops using spoken tutorials.  
 
|  The '''Spoken Tutorial Project''' team conducts workshops using spoken tutorials.  
  
And gives certificates on passing online tests.
+
And, gives certificates on passing online tests.
  
 
For more details, please write to us.
 
For more details, please write to us.
Line 476: Line 481:
 
|-
 
|-
 
| 11:47
 
| 11:47
|  Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
+
|  Spoken Tutorial Project is funded by '''NMEICT, MHRD''', Government of India.
  
 
More information on this mission is available at this link.
 
More information on this mission is available at this link.
Line 482: Line 487:
 
|-
 
|-
 
| 11:59
 
| 11:59
|  The script has been contributed by Antara. And this is Praveen from IIT Bombay signing off.
+
|  The script has been contributed by Antara. And this is Praveen from '''IIT Bombay''', signing off.
  
Thank you for joining
+
Thank you for joining.
  
 
|}
 
|}

Revision as of 11:20, 11 July 2019

Time
Narration
00:01 Hello and welcome to this spoken tutorial on built-in functions in awk.
00:07 In this tutorial, we will learn about different types of built-in functions like-

Arithmetic functions,

00:15 String functions,
00:17 Input/Output functions and

Time-stamp functions.

00:23 We will do this through some examples.
00:26 To record this tutorial, I am using: Ubuntu Linux 16.04 operating system and

gedit text editor 3.20.1

00:38 You can use any text editor of your choice.
00:42 To practice this tutorial, you should have gone through the earlier awk tutorials on this website.
00:49 You should have some knowledge of any programming language like C or C++.
00:56 If not, then please go through the corresponding tutorials on our website.
01:02 The files used in this tutorial are available in the Code Files link on this tutorial page.

Please download and extract them.

01:12 Built-in functions are always available for awk to call.
01:17 First we will learn about the arithmetic functions.

square root function (sqrt (x)) returns positive square root of a number x.

01:27 int() function truncates x to an integer value.
01:32 exponential function gives the exponential of x.
01:37 log function returns natural logarithm value of x.
01:43 sin and cos gives sine(x) and cosine(x) respectively.
01:49 Please note that argument x should be mentioned in radians.
01:55 Let’s look at an example to understand these functions.
02:00 I have already written the code in a file arithmetic underscore function dot awk.

The same is available in the Code Files link.

02:10 Here, we are printing the square root of a positive and negative number respectively.
02:17 Next, we are printing the integer value for a positive and negative number respectively.
02:24 Then we are printing exponential of a small number and a very large number.
02:31 After that, natural logarithm of positive and negative numbers are printed.
02:38 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.

02:50 Open the terminal by pressing Ctrl, Alt and T Keys.
02:55 Next, go to the folder where you have downloaded and extracted the file using cd command.
03:03 Now, type: awk space -f space arithmetic_function.awk

and press Enter to see the output.

03:14 Couple of things are clear from this output.
03:18 sqrt() function gives square root of a positive number.
03:23 It returns nan or not a number if the number is negative.
03:29 int() function gives the truncated integer of any positive or negative number.
03:36 exp() function gives exponential of a number.

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

03:47 natural logarithm of positive number is given by log() function.
03:53 If the number is negative, the function returns nan.
03:58 Sine and cosine functions return corresponding values.

You can verify the values using your calculator.

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

Let us understand these with an example.

04:49 I have written a code for the random function and saved it as random.awk.
04:56 Here, inside the for loop, rand() function will generate a random number between 0 and 1.
05:04 Then the generated number will be multiplied by 50 and get printed.
05:10 So, this code will generate 5 random numbers within 50.
05:16 Switch to the terminal and execute the file.

Let me clear the terminal.

05:23 Type: awk space hyphen f space random dot awk and press Enter.
05:31 See, it is giving 5 random numbers.
05:35 What happens if I execute the code again?
05:39 Press the up arrow key to get the previously executed command and press Enter.
05:47 We are getting the same output. Which means, awk is generating the same set of random numbers for every execution of the script.
05:57 Then how can we get a new set of random numbers in every execution?

Switch to the code once again.

06:06 Before the for loop, type srand() function.
06:11 Press Ctrl and S keys to save the file.
06:16 Now, switch to the terminal.
06:19 Press the up arrow key to get the previously executed command and press Enter.
06:27 It is giving a different set of random numbers.
06:31 So, we can generate a new set of random numbers using srand function when it’s used without an argument.
06:40 Next we will see some string functions.

length() function gives the length of a particular string s.

06:49 index() function determines the position of string s2 within the larger string s1.
06:57 For example- index within parentheses within double quotes linux comma within double quotes n, returns 3.

Let us see an example.

07:10 Open the file awkdemo.txt
07:14 We know that each student in the awkdemo.txt file has a 4 digit roll number.
07:21 Due to typing error, the roll numbers may have wrong number of digits.

We can easily detect these using awk commands.

07:30 Switch to the terminal .

Let me clear the terminal.

07:36 Now, type the command as shown here.

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

07:46 If not, then that particular record will get printed.

Press Enter.

07:53 See, there is one roll-number S02 that has been typed incorrectly.
08:00 It has three digits, whereas all others have four digits.
08:07 The substr(s,a,b) function extracts a substring from a larger string s.
08:14 Let me explain the parameters.
08:17 Here s is the string.
08:20 a denotes the position in s from which the extraction would start.
08:26 b denotes the number of characters that would be extracted.

Let us see one example.

08:33 Switch to the awkdemo.txt file.
08:37 Here, the first letter of the roll numbers represents the Hostel code where the particular student resides.
08:46 Say, we want to find the list of students who are staying in Hostel A.
08:52 To get that, let’s switch to the terminal.
08:56 Type the command as shown here.
09:00 Here, we take the string denoted by $1.
09:05 As we know, $1 represents the first field that is roll number in our case.
09:12 Next, we extract a substring that starts at position one with the character length one.
09:19 Then, if it is equal to capital A then that line from the file will get printed.

Press Enter to see the output.

09:29 We got the list of students who are in Hostel A.
09:34 We have seen the function split() earlier.

So, I am not explaining the details here.

09:40 Please refer to the earlier awk tutorials if you have any doubt.
09:45 There are some other functions which are related to Input/Output.

system() function helps us to run any unix command within awk.

09:56 Now, we will run the unix command date through awk command.
10:01 In the terminal, type the command as shown here.

And press Enter.

10:09 Today’s date and time is displayed on the terminal as an output.
10:15 Now, why do we need this? We have kept only the BEGIN section of the awk command.
10:21 In real world scenarios, we may want to print the system date, before displaying the required output.
10:28 In that case, we would need to execute system commands from awk command.
10:34 There are some functions dealing with time stamps like -

systime(), strftime() etc.

10:43 Browse through the Internet to know about these functions.
10:48 This brings us to the end of this tutorial.

Let us summarize.

10:53 In this tutorial, we learnt about different types of built-in functions like-

Arithmetic functions, String functions, Input/Output functions and Time stamps functions.

11:06 As an assignment-

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

11:13 where name of the student has small u as the third letter using the awkdemo.txt file.
11:22 The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

11:30 The Spoken Tutorial Project team conducts workshops using spoken tutorials.

And, gives certificates on passing online tests.

For more details, please write to us.

11:43 Please post your timed queries in this forum.
11:47 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

11:59 The script has been contributed by Antara. And this is Praveen from IIT Bombay, signing off.

Thank you for joining.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14