Difference between revisions of "Linux-AWK/C2/User-Defined-Functions-in-awk/English"
(Created page with "'''Title of script: User Defined Function in Awk''' '''Author: Antara Roy Choudhury''' '''Keywords: Function definition syntax, Function call, Return statement, User defined...") |
Nancyvarkey (Talk | contribs) |
||
Line 12: | Line 12: | ||
|- | |- | ||
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 1: Introduction | | style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 1: Introduction | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Welcome to this | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Welcome to this Spoken Tutorial on '''User-defined function''' in '''awk'''. |
|- | |- | ||
Line 27: | Line 27: | ||
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 3: System requirement | | style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Slide 3: System requirement | ||
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| To record this tutorial, I am using | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| To record this tutorial, I am using | ||
− | |||
* '''Ubuntu Linux '''16.04 '''OS '''and | * '''Ubuntu Linux '''16.04 '''OS '''and | ||
Line 85: | Line 84: | ||
Suppose, '''stipend '''is 8900. | Suppose, '''stipend '''is 8900. | ||
− | + | * Print it as 8 thousand 9 hundred in words. | |
− | * | + | * If stipend is 0, print as zero in words. |
− | * If stipend is 0, print as zero | + | |
− | + | ||
− | + | ||
|- | |- | ||
Line 100: | Line 96: | ||
− | Here | + | Here '''argval''' is basically our sixth '''field '''which is '''stipend'''. |
|- | |- | ||
Line 128: | Line 124: | ||
− | And | + | And we'll store the values in an '''array a '''at different '''indices.''' |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 138: | Line 131: | ||
− | Since we have only four '''digits''', I used four | + | Since we have only four '''digits''', I used four indices. |
|- | |- | ||
Line 149: | Line 142: | ||
|- | |- | ||
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight printf("\n") | | style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight printf("\n") | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| At the end, we print a '''backslash n '''character to provide new line break in the output. | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| At the end, we print a '''backslash n '''character, to provide new '''line break''' in the output. |
|- | |- | ||
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight $2 in the last print statement | | style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight $2 in the last print statement | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Then inside the '''awk script''', we have printed '''dollar 2''', which is the second '''field ''' | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Then inside the '''awk script''', we have printed '''dollar 2''', which is the second '''field '''that is '''name'''. |
|- | |- | ||
Line 161: | Line 154: | ||
Let’s execute the file. | Let’s execute the file. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to terminal >> cd into the saved folder. | ||
+ | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the '''terminal'''. | ||
+ | |||
+ | |||
+ | Next go to the folder where you have downloaded and extracted the file, using '''cd command.''' | ||
|- | |- | ||
Line 169: | Line 169: | ||
<nowiki>[Enter]</nowiki> | <nowiki>[Enter]</nowiki> | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now type the following '''command '''and press''' Enter.''' |
|- | |- | ||
Line 269: | Line 269: | ||
− | And stored | + | And stored that value in '''variable ret.''' |
Line 276: | Line 276: | ||
|- | |- | ||
| style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight appropriately in the file | | style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight appropriately in the file | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Inside the '''BEGIN section '''we have defined '''array nums '''with 5 different numbers. | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Inside the '''BEGIN section, '''we have defined '''array nums '''with 5 different numbers. |
|- | |- | ||
Line 282: | Line 282: | ||
print avg(nums) | print avg(nums) | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In the '''print statement''', we | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In the '''print statement''', we call the '''function avg() '''with one '''argument''', which is the '''array name.''' |
So, you do not have to pass '''local variables '''as '''arguments'''. | So, you do not have to pass '''local variables '''as '''arguments'''. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the terminal. | ||
+ | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch back to the terminal. | ||
+ | |||
+ | |||
+ | Let me clear the terminal. | ||
|- | |- | ||
Line 293: | Line 300: | ||
<nowiki>[Enter]</nowiki> | <nowiki>[Enter]</nowiki> | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Type the following command- |
− | '''awk | + | '''awk space hyphen f space average dot awk''' |
Line 332: | Line 339: | ||
− | The code for the same is provided as reverse_roll.awk in the Code Files link. | + | The code for the same is provided as '''reverse_roll.awk''' in the '''Code Files''' link. |
|- | |- | ||
Line 349: | Line 356: | ||
* '''Function call '''and | * '''Function call '''and | ||
* '''Return statement''' | * '''Return statement''' | ||
− | |||
− | |||
|- | |- |
Revision as of 08:45, 22 March 2018
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 |