Linux-AWK/C2/User-Defined-Functions-in-awk/English
Title of script: User Defined Function in Awk
Author: Antara Roy Choudhury
Keywords: Function definition syntax, Function call, Return statement, User defined function in awk
|
|
Slide 1: Introduction | Welcome to this Spoken Tutorial on User-defined function in awk. |
Slide 2: Learning Objective | In this tutorial we will learn about-
We will do this through some examples. |
Slide 3: System requirement | To record this tutorial, I am using
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.
|
Slide 5: Code Files | The files used in this tutorial are available in the Code Files link on this tutorial page.
|
Slide 6: User defined functions | Now let us learn about user defined functions.
|
In Slide 6, highlight function keyword | Here the keyword function is mandatory. |
Slide 7: Function Call | To call a function, write the name of the function, followed by the arguments in parentheses.
|
Slide 8: Example | We will see one example now.
|
Show user_function.awk in gedit | I have already written the code in a file named user_function.awk |
Highlight function changeit(argval) | Here I have written a function named changeit with a single argument argval.
|
Highlight if(argval==0) | Inside the function, first the code will check if argval is zero or not. |
Highlight printf("zero") | If yes, it will print “Zero” |
Highlight else part | If not, then the else part of the code will be executed. |
Highlight
a[2]=substr(value,2,1) a[3]=substr(value,3,1) a[4]=substr(value,4,1) |
In the else part, first we will extract each digit one by one using the substr function.
|
Highlight a[1]=substr(value,1,1) | For example, a[1] will give the first digit from left hand side or the thousand’s place digit.
|
Highlight all the if loops inside else block | Next, we will check whether the elements are not equal to zero.
|
Highlight printf("\n") | At the end, we print a backslash n character, to provide new line break in the output. |
Highlight $2 in the last print statement | Then inside the awk script, we have printed dollar 2, which is the second field that is name. |
Highlight changeit($6) | Then we call the function changeit with the parameter dollar 6, which is stipend.
|
Switch to terminal >> cd into the saved folder. | Switch to the terminal.
|
Type:
[Enter] |
Now type the following command and press Enter. |
Show the output | We get the output as expected. |
Slide 9: Return statement | A user-defined function can also include a return statement.
|
Slide 9: Return statement | It looks like this:return space expression
|
Let’s write a function to return average of an array. | |
Show in gedit average.awk
|
I have written the code in the file average.awk
|
Highlight appropriately in
function avg(arr, i, sum,n,ret)
|
We have defined a function named avg for this purpose.
|
Highlight extra space
function avg(arr, i, sum,n,ret) |
The extra space before i indicates that the variables i, n, sum and ret are local variables.
|
Highlight inside
for (i in arr) { n++ sum += arr[i] } |
Inside the for loop, we have calculated the total number and summation of array elements. |
Highlight
ret=sum/n return ret |
We have calculated the average by dividing summation with the total number of elements.
|
Highlight appropriately in the file | Inside the BEGIN section, we have defined array nums with 5 different numbers. |
Highlight
print avg(nums) |
In the print statement, we call the function avg() with one argument, which is the array name.
|
Switch to the terminal. | Switch back to the terminal.
|
Type at command prompt
awk -f average.awk [Enter] |
Type the following command-
awk space hyphen f space average dot awk
|
Show the output | We get the output as 3.6.
|
Let us look at one more example. | |
Show reverse.awk in Gedit | I have written a code to reverse a string and named it as reverse.awk
|
Display Slide | As an assignment, use the function rev to reverse the Roll number field in awkdemo.txt file.
|
Display Slide 9
Summary |
This brings us to the end of this tutorial.
|
Display Slide 10
Assignment
|
As an assignment-
2. Write a function to return the minimum value element from an array. |
Display Slide 11
About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it. |
Display Slide 12
Spoken Tutorial workshops |
The Spoken Tutorial Project team conducts workshops using spoken tutorials.
|
Display Slide 13
Forum for specific questions: |
Please post your timed queries in this forum. |
Display Slide 14
Acknowledgement |
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
this link. |
The script has been contributed by Antara.
Thank you for joining |