Linux-AWK/C2/User-Defined-Functions-in-awk/English-timed

From Script | Spoken-Tutorial
Revision as of 17:41, 24 July 2019 by Sandhya.np14 (Talk | contribs)

Jump to: navigation, search
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-

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