Difference between revisions of "Linux-AWK/C2/User-Defined-Functions-in-awk/English-timed"
PoojaMoolya (Talk | contribs) (Created page with " {| border=1 | <center>'''Time'''</center> | <center>'''Narration'''</center> |- | 00:01 |Hello and Welcome to this Spoken Tutorial on '''User-defined function''' in '''awk''...") |
Sandhya.np14 (Talk | contribs) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | |Hello and | + | |Hello and welcome to this '''Spoken Tutorial''' on '''User-defined functions in awk'''. |
|- | |- | ||
| 00:07 | | 00:07 | ||
− | | In this tutorial we will learn about- | + | | In this tutorial, we will learn about- |
+ | syntax of '''function''' definition, | ||
− | ''' | + | '''function call '''and |
+ | |||
+ | ''''Return' statement'''. | ||
|- | |- | ||
Line 20: | Line 23: | ||
|- | |- | ||
| 00:21 | | 00:21 | ||
− | | To record this tutorial, I am using '''Ubuntu Linux | + | | To record this tutorial, I am using: |
+ | '''Ubuntu Linux 16.04 Operating System '''and | ||
+ | |||
+ | '''gedit text editor 3.20.1''' | ||
|- | |- | ||
| 00:34 | | 00:34 | ||
− | | You can use any text editor of your choice. | + | | You can use any '''text editor''' of your choice. |
|- | |- | ||
Line 42: | Line 48: | ||
| 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:08 | | 01:08 | ||
− | | Now let us learn about '''user defined functions.''' | + | | Now, let us learn about '''user defined functions.''' |
The '''function '''syntax is as follows. | The '''function '''syntax is as follows. | ||
Line 52: | Line 58: | ||
|- | |- | ||
| 01:16 | | 01:16 | ||
− | | And the syntax is self-explanatory. | + | | And, the syntax is self-explanatory. |
|- | |- | ||
| 01:20 | | 01:20 | ||
− | | Here the '''keyword function '''is mandatory. | + | | Here, the '''keyword function '''is mandatory. |
|- | |- | ||
| 01:24 | | 01:24 | ||
− | | To '''call '''a '''function''', write the name of the '''function | + | | To '''call '''a '''function''', write the name of the '''function '''followed by the '''arguments '''in parentheses. |
|- | |- | ||
| 01:31 | | 01:31 | ||
− | | Note: '''space '''is not allowed between the '''function '''name and the | + | | Note: '''space '''is not allowed between the '''function '''name and the open parentheses of the '''argument.''' |
|- | |- | ||
Line 76: | Line 82: | ||
|- | |- | ||
| 01:47 | | 01:47 | ||
− | |Assume that '''stipend '''is either zero or consists of four | + | |Assume that '''stipend '''is either zero or consists of four digits. |
|- | |- | ||
Line 94: | Line 100: | ||
|- | |- | ||
| 02:15 | | 02:15 | ||
− | | Here I have written a '''function '''named '''changeit''' with a single '''argument argval. ''' | + | | Here, I have written a '''function '''named '''changeit''' with a single '''argument 'argval'. ''' |
|- | |- | ||
| 02:23 | | 02:23 | ||
− | |Here '''argval''' is basically our sixth '''field '''which is '''stipend'''. | + | |Here, '''argval''' is basically our sixth '''field '''which is '''stipend'''. |
|- | |- | ||
| 02:29 | | 02:29 | ||
− | | Inside the '''function''', the first | + | | Inside the '''function''', the first code will check if '''argval '''is zero or not. |
|- | |- | ||
Line 114: | Line 120: | ||
|- | |- | ||
| 02:46 | | 02:46 | ||
− | | In the '''else '''part, first we will extract each digit one by one using the '''substring function.''' | + | | In the '''else '''part, first we will '''extract''' each digit one by one using the '''substring function.''' |
|- | |- | ||
| 02:54 | | 02:54 | ||
− | |And we'll store the values in an ''' | + | |And, we'll store the values in an array '''a '''at different indices. |
|- | |- | ||
| 03:00 | | 03:00 | ||
− | | For example | + | | For example- '''a[1] ''' will give the first digit from left hand side or the thousand’s place digit. |
|- | |- | ||
| 03:08 | | 03:08 | ||
− | |Since we have only four | + | |Since we have only four digits, I have used four indices. |
|- | |- | ||
Line 132: | Line 138: | ||
| Next, we will check whether the '''elements '''are not equal to zero. | | Next, we will check whether the '''elements '''are not equal to zero. | ||
− | And print them in a proper order. | + | And '''print''' them in a proper order. |
|- | |- | ||
| 03:21 | | 03:21 | ||
− | | At the end, we print a '''backslash n '''character | + | | At the end, we print a '''backslash n '''character to provide a new '''line break''' in the output. |
|- | |- | ||
| 03:28 | | 03:28 | ||
− | | Then inside the '''awk script''', we have printed '''dollar 2''' | + | | Then, inside the '''awk script''', we have printed '''dollar 2''' which is the second '''field '''that is '''name'''. |
|- | |- | ||
| 03:35 | | 03:35 | ||
− | | Then we call the ''' | + | | Then we call the function '''changeit '''with the '''parameter dollar 6''' which is '''stipend'''. |
− | Let’s execute the file. | + | Let’s '''execute''' the file. |
|- | |- | ||
Line 152: | Line 158: | ||
| Switch to the '''terminal'''. | | Switch to the '''terminal'''. | ||
− | Next go to the folder where you have downloaded and extracted the file | + | Next, go to the folder where you have downloaded and extracted the file using '''cd''' command. |
|- | |- | ||
| 03:53 | | 03:53 | ||
− | | Now type the following '''command '''and press''' Enter.''' | + | | Now, type the following '''command '''and press''' Enter.''' |
|- | |- | ||
| 04:00 | | 04:00 | ||
− | | We get the output as expected. | + | | We get the '''output''' as expected. |
|- | |- | ||
| 04:03 | | 04:03 | ||
− | | | + | | An '''user-defined function '''can also include a '''return statement. ''' |
|- | |- | ||
| 04:08 | | 04:08 | ||
− | |This | + | |This statement returns '''control '''to the calling part of the '''awk program'''. |
|- | |- | ||
Line 176: | Line 182: | ||
|- | |- | ||
| 04:20 | | 04:20 | ||
− | | It looks like this:'''return space expression''' | + | | It looks like this: '''return space expression'''. |
− | Here the '''expression '''part is optional. | + | Here, the '''expression '''part is optional. |
|- | |- | ||
| 04:29 | | 04:29 | ||
− | | Let’s write a | + | | Let’s write a function to return average of an '''array'''. |
|- | |- | ||
Line 192: | Line 198: | ||
|- | |- | ||
| 04:41 | | 04:41 | ||
− | | We have defined a ''' | + | | We have defined a function named '''avg''' for this purpose. |
|- | |- | ||
Line 198: | Line 204: | ||
|It has five '''parameters'''. | |It has five '''parameters'''. | ||
− | '''arr''' is the '''array''' for which the | + | '''arr''' is the '''array''' for which the average is to be calculated. |
|- | |- | ||
Line 214: | Line 220: | ||
|- | |- | ||
| 05:07 | | 05:07 | ||
− | |'''ret''' represents the | + | |'''ret''' represents the variable to be returned from the function '''avg.''' |
− | '''ret''' will store the calculated | + | '''ret''' will store the calculated average. |
|- | |- | ||
Line 236: | Line 242: | ||
|- | |- | ||
| 05:43 | | 05:43 | ||
− | | We have calculated the | + | | We have calculated the average by dividing summation with the total number of '''elements''' |
− | + | and stored that value in variable '''ret.''' | |
|- | |- | ||
| 05:54 | | 05:54 | ||
− | |This ''' | + | |This function '''avg() '''returns the value of the variable '''ret.''' |
|- | |- | ||
| 06:01 | | 06:01 | ||
− | | Inside the '''BEGIN | + | | Inside the '''BEGIN''' section, we have defined array '''nums '''with 5 different numbers. |
|- | |- | ||
|06:07 | |06:07 | ||
− | | In the '''print statement''', we call the '''function avg() '''with one '''argument''' | + | | In the '''print statement''', we call the '''function avg() '''with one '''argument''' which is the array name. |
|- | |- | ||
Line 262: | Line 268: | ||
|- | |- | ||
| 06:26 | | 06:26 | ||
− | | Type the following command- '''awk space hyphen f space average dot awk''' | + | | Type the following command- '''awk space hyphen f space average dot awk'''. |
Press '''Enter.''' | Press '''Enter.''' | ||
Line 278: | Line 284: | ||
|- | |- | ||
| 06:47 | | 06:47 | ||
− | | I have written a code to | + | | I have written a code to reverse a '''string '''and named it as '''reverse.awk'''. |
− | '''recursive function '''is used to | + | '''recursive function '''is used to reverse a '''string.''' |
|- | |- | ||
| 06:57 | | 06:57 | ||
− | |Pause the video here and look at the code to understand how the control flows. | + | |Pause the video here and look at the code to understand how the '''control''' flows. |
− | Then execute it to see the output. | + | Then '''execute''' it to see the output. |
|- | |- | ||
| 07:07 | | 07:07 | ||
− | | As an assignment, use the''' | + | | As an assignment, use the function '''rev''' to reverse the '''Roll number field '''in '''awkdemo.txt file.''' |
|- | |- | ||
Line 307: | Line 313: | ||
|- | |- | ||
| 07:36 | | 07:36 | ||
− | | In this tutorial we learnt about- | + | | In this tutorial we learnt about- |
+ | syntax of '''function''' definition, | ||
|- | |- | ||
| 07:41 | | 07:41 | ||
− | | ''' | + | | '''function call '''and |
+ | '''Return''' statement. | ||
|- | |- | ||
| 07:45 | | 07:45 | ||
− | | As an assignment- | + | | As an assignment- |
+ | Write a '''function '''to create a '''transpose of a 2D matrix.''' | ||
|- | |- | ||
| 07:52 | | 07:52 | ||
− | | Write a | + | | Write a function to return the '''minimum value element '''from an''' array'''. |
|- | |- | ||
| 07:58 | | 07:58 | ||
− | | 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 331: | Line 339: | ||
| 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. |
|- | |- | ||
Line 343: | Line 351: | ||
|- | |- | ||
| 08:24 | | 08:24 | ||
− | | 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 351: | Line 359: | ||
| The script has been contributed by Antara. | | The script has been contributed by Antara. | ||
− | And this is Praveen from IIT Bombay signing off. | + | And this is Praveen from '''IIT Bombay''', signing off. |
− | Thank you for joining | + | Thank you for joining. |
|} | |} |
Latest revision as of 11:13, 25 July 2019
|
|
00:01 | Hello and welcome to this Spoken Tutorial on User-defined functions in awk. |
00:07 | In this tutorial, we will learn about-
syntax of function definition, function call and 'Return' statement. |
00:17 | We will do this through some examples. |
00:21 | To record this tutorial, I am using:
Ubuntu Linux 16.04 Operating System and gedit text editor 3.20.1 |
00:34 | You can use any text editor of your choice. |
00:38 | To practice this tutorial, you should have gone through the earlier awk tutorials on this website. |
00:45 | You should have some knowledge of any programming language like C or C++. |
00:52 | If not, then please go through the corresponding tutorials on our website. |
00:58 | The files used in this tutorial are available in the Code Files link on this tutorial page.
Please download and extract them. |
01:08 | Now, let us learn about user defined functions.
The function syntax is as follows. |
01:16 | And, the syntax is self-explanatory. |
01:20 | Here, the keyword function is mandatory. |
01:24 | To call a function, write the name of the function followed by the arguments in parentheses. |
01:31 | Note: space is not allowed between the function name and the open parentheses of the argument. |
01:39 | We will see one example now. |
01:42 | In our awkdemo.txt file, the sixth field represents stipend. |
01:47 | Assume that stipend is either zero or consists of four digits. |
01:54 | Suppose, stipend is 8900.
Print it as 8 thousand 9 hundred in words. |
02:03 | If stipend is 0, print as zero in words. |
02:08 | I have already written the code in a file named user_function.awk |
02:15 | Here, I have written a function named changeit with a single argument 'argval'. |
02:23 | Here, argval is basically our sixth field which is stipend. |
02:29 | Inside the function, the first code will check if argval is zero or not. |
02:36 | If yes, it will print “Zero” in words. |
02:40 | If not, then else part of the code will be executed. |
02:46 | In the else part, first we will extract each digit one by one using the substring function. |
02:54 | And, we'll store the values in an array a at different indices. |
03:00 | For example- a[1] will give the first digit from left hand side or the thousand’s place digit. |
03:08 | Since we have only four digits, I have used four indices. |
03:13 | Next, we will check whether the elements are not equal to zero.
And print them in a proper order. |
03:21 | At the end, we print a backslash n character to provide a new line break in the output. |
03:28 | Then, inside the awk script, we have printed dollar 2 which is the second field that is name. |
03:35 | Then we call the function changeit with the parameter dollar 6 which is stipend.
Let’s execute the file. |
03:43 | Switch to the terminal.
Next, go to the folder where you have downloaded and extracted the file using cd command. |
03:53 | Now, type the following command and press Enter. |
04:00 | We get the output as expected. |
04:03 | An user-defined function can also include a return statement. |
04:08 | This statement returns control to the calling part of the awk program. |
04:13 | It can also be used to return a value for use in the rest of the awk program. |
04:20 | It looks like this: return space expression.
Here, the expression part is optional. |
04:29 | Let’s write a function to return average of an array. |
04:34 | I have written the code in the file average.awk
Let’s view the contents. |
04:41 | We have defined a function named avg for this purpose. |
04:46 | It has five parameters.
arr is the array for which the average is to be calculated. |
04:55 | i is array loop variable. |
04:58 | sum is the summation of all array elements. |
05:03 | n indicates the number of elements in array. |
05:07 | ret represents the variable to be returned from the function avg.
ret will store the calculated average. |
05:17 | The extra space before i indicates that the variables i, sum, n and ret are local variables. |
05:27 | Actually, the local variables are not intended to be arguments. |
05:32 | You should follow this convention when defining functions. |
05:36 | Inside the for loop, we have calculated the total number and summation of array elements. |
05:43 | We have calculated the average by dividing summation with the total number of elements
and stored that value in variable ret. |
05:54 | This function avg() returns the value of the variable ret. |
06:01 | Inside the BEGIN section, we have defined array nums with 5 different numbers. |
06:07 | In the print statement, we call the function avg() with one argument which is the array name. |
06:14 | So, you do not have to pass local variables as arguments. |
06:20 | Switch back to the terminal. Let me clear the terminal. |
06:26 | Type the following command- awk space hyphen f space average dot awk.
Press Enter. |
06:37 | We get the output as 3.6.
You can validate it by using a calculator. |
06:44 | Let us look at one more example. |
06:47 | I have written a code to reverse a string and named it as reverse.awk.
recursive function is used to reverse a string. |
06:57 | Pause the video here and look at the code to understand how the control flows.
Then execute it to see the output. |
07:07 | As an assignment, use the function rev to reverse the Roll number field in awkdemo.txt file. |
07:16 | For example, if the roll number is A001, output should be 100A. |
07:24 | The code for the same is provided as reverse_roll.awk in the Code Files link. |
07:31 | This brings us to the end of this tutorial.
Let us summarize. |
07:36 | In this tutorial we learnt about-
syntax of function definition, |
07:41 | function call and
Return statement. |
07:45 | As an assignment-
Write a function to create a transpose of a 2D matrix. |
07:52 | Write a function to return the minimum value element from an array. |
07:58 | The video at the following link summarises the Spoken Tutorial project.
Please download and watch it. |
08:06 | The Spoken Tutorial Project team conducts workshops using spoken tutorials.
And, gives certificates on passing online tests. |
08:16 | For more details, please write to us. |
08:20 | Please post your timed queries in this forum. |
08:24 | Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
More information on this mission is available at this link. |
08:36 | The script has been contributed by Antara.
And this is Praveen from IIT Bombay, signing off. Thank you for joining. |