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

From Script | Spoken-Tutorial
Jump to: navigation, search
(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''...")
 
Line 6: Line 6:
 
|-
 
|-
 
| 00:01
 
| 00:01
|Hello and Welcome to this Spoken Tutorial on '''User-defined function''' in '''awk'''.
+
|Hello and welcome to this '''Spoken Tutorial''' on '''User-defined functions in awk'''.
  
 
|-
 
|-
 
| 00:07
 
| 00:07
| In this tutorial we will learn about- The syntax of '''function definition'''
+
| In this tutorial, we will learn about-  
 +
syntax of '''function''' definition,
  
'''Function call '''and '''Return statement'''
+
'''function call '''and  
 +
'''Return''' statement.
  
 
|-
 
|-
Line 20: Line 22:
 
|-
 
|-
 
| 00:21
 
| 00:21
| To record this tutorial, I am using  '''Ubuntu Linux '''16.04 '''Operating System '''and  '''gedit text editor '''3.20.1
+
| 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 46:
 
| 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 56:
 
|-
 
|-
 
| 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, '''followed by the '''arguments '''in parentheses.
+
| 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 '''open parentheses '''of the '''argument.'''
+
| Note: '''space '''is not allowed between the '''function '''name and the open parentheses of the '''argument.'''
  
 
|-
 
|-
Line 94: Line 98:
 
|-
 
|-
 
| 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 the code will check if '''argval '''is '''zero''' or not.
+
| Inside the '''function''', the first code will check if '''argval '''is zero or not.
  
 
|-
 
|-
Line 114: Line 118:
 
|-
 
|-
 
| 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 '''array a '''at different '''indices.'''
+
|And, we'll store the values in an array '''a '''at different indices.
  
 
|-
 
|-
 
| 03:00
 
| 03:00
| For example, '''a[1] ''' will give the first '''digit '''from left hand side or the '''thousand’s place digit.'''
+
| 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 '''digits''', I have used four indices.
+
|Since we have only four digits, I have used four indices.
  
 
|-
 
|-
Line 132: Line 136:
 
| 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, to provide a new '''line break''' in the output.
+
| 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''', which is the second '''field '''that is '''name'''.
+
| 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 '''function changeit '''with the '''parameter dollar 6,''' which is '''stipend'''.
+
| 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 156:
 
| Switch to the '''terminal'''.
 
| Switch to the '''terminal'''.
  
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: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.
  
 
|-
 
|-
Line 168: Line 172:
 
|-
 
|-
 
| 04:08
 
| 04:08
|This '''statement '''returns '''control '''to the '''calling '''part of the '''awk program'''.  
+
|This statement returns '''control '''to the calling part of the '''awk program'''.  
  
 
|-
 
|-
Line 176: Line 180:
 
|-
 
|-
 
| 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 '''function '''to return '''average '''of an '''array'''.
+
| Let’s write a function to return average of an '''array'''.
  
 
|-
 
|-
Line 192: Line 196:
 
|-
 
|-
 
| 04:41
 
| 04:41
| We have defined a '''function named avg''' for this purpose.
+
| We have defined a function named '''avg''' for this purpose.
  
 
|-
 
|-
Line 198: Line 202:
 
|It has five '''parameters'''.
 
|It has five '''parameters'''.
  
'''arr''' is the '''array''' for which the '''average''' is to be calculated.
+
'''arr''' is the '''array''' for which the average is to be calculated.
  
 
|-
 
|-
Line 214: Line 218:
 
|-
 
|-
 
| 05:07
 
| 05:07
|'''ret''' represents the '''variable '''to be returned from the '''function avg.'''
+
|'''ret''' represents the variable to be returned from the function '''avg.'''
  
'''ret''' will store the calculated '''average'''.  
+
'''ret''' will store the calculated average.  
  
 
|-
 
|-
Line 236: Line 240:
 
|-
 
|-
 
| 05:43
 
| 05:43
| We have calculated the '''average '''by dividing summation with the total number of '''elements.'''
+
| We have calculated the average by dividing summation with the total number of '''elements'''
  
And stored that value in '''variable ret.'''
+
and stored that value in variable '''ret.'''
  
 
|-
 
|-
 
| 05:54
 
| 05:54
|This '''function avg() '''returns the value of the '''variable ret.'''
+
|This function '''avg() '''returns the value of the variable '''ret.'''
  
 
|-
 
|-
 
| 06:01
 
| 06:01
| Inside the '''BEGIN section, '''we have defined '''array nums '''with 5 different numbers.
+
| 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''', which is the '''array name.'''
+
| In the '''print statement''', we call the '''function avg() '''with one '''argument''' which is the array name.
  
 
|-
 
|-
Line 262: Line 266:
 
|-
 
|-
 
| 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 282:
 
|-
 
|-
 
| 06:47
 
| 06:47
| I have written a code to '''reverse '''a '''string '''and named it as '''reverse.awk'''
+
| I have written a code to reverse a '''string '''and named it as '''reverse.awk'''.
  
'''recursive function '''is used to '''reverse '''a '''string.'''
+
'''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''' function rev''' to '''reverse''' the '''Roll number field '''in '''awkdemo.txt file.'''
+
| As an assignment, use the function '''rev''' to reverse the '''Roll number field '''in '''awkdemo.txt file.'''
  
 
|-
 
|-
Line 307: Line 311:
 
|-
 
|-
 
| 07:36
 
| 07:36
| In this tutorial we learnt about-  The syntax of '''function definition'''
+
| In this tutorial we learnt about-   
 +
syntax of function definition,
  
 
|-
 
|-
 
| 07:41
 
| 07:41
| '''Function call '''and '''Return statement'''
+
| '''function call '''and  
 +
'''Return''' statement.
  
 
|-
 
|-
 
| 07:45
 
| 07:45
| As an assignment- 1. Write a '''function '''to create a '''transpose '''of a '''2D matrix.'''
+
| As an assignment-  
 +
1. Write a '''function '''to create a '''transpose '''of a '''2D matrix.'''
  
 
|-
 
|-
Line 323: Line 330:
 
|-
 
|-
 
| 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 337:
 
| 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 349:
 
|-
 
|-
 
| 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 357:
 
| 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.
  
 
|}
 
|}

Revision as of 11:56, 11 July 2019

Time
Narration
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 A 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-

1. 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.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14