Difference between revisions of "Linux-AWK/C2/Built-in-Functions-in-awk/English"
(Created page with "'''Title of script: Built-in functions in awk ''' '''Author: Antara Roy Choudhury''' '''Keywords: awk Built-in function, Arithmetic function, String function, Input/Output f...") |
Nancyvarkey (Talk | contribs) m (Nancyvarkey moved page Linux/C3/Built-in-Functions-in-awk/English to Linux-AWK/C2/Built-in-Functions-in-awk/English without leaving a redirect: New series) |
||
(One intermediate revision by the same user not shown) | |||
Line 17: | Line 17: | ||
|- | |- | ||
| 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 2: Learning Objectives | | 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 2: Learning Objectives | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In this tutorial we will learn about | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In this tutorial we will learn about different types of '''built-in functions''' like- |
− | + | ||
* '''Arithmetic functions''' | * '''Arithmetic functions''' | ||
* '''String functions''' | * '''String functions''' | ||
* '''Input/Output functions '''and | * '''Input/Output functions '''and | ||
− | * '''Time | + | * '''Time-stamp functions''' |
We will do this through some examples. | We will do this through some examples. | ||
Line 31: | Line 30: | ||
| 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 operating system and | |
− | * '''Ubuntu Linux '''16.04 | + | |
* '''gedit text editor '''3.20.1 | * '''gedit text editor '''3.20.1 | ||
Line 61: | Line 59: | ||
First we will learn about the '''arithmetic functions.''' | First we will learn about the '''arithmetic functions.''' | ||
− | + | * '''sqrt(x) function''' returns positive '''square root''' of a number '''x''' | |
− | * '''sqrt(x) function''' returns positive square root of a number x | + | * '''int(x)''' truncates '''x''' to an integer value |
− | * '''int(x)''' truncates '''x''' | + | * '''exp(x)''' gives the exponential of '''x''' |
− | * '''exp(x)''' gives the exponential of | + | |
− | + | ||
− | + | ||
|- | |- | ||
| 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 6: Arithmetic Functions | | 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 6: Arithmetic Functions | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| * '''log(x)''' returns natural logarithm value of | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| |
− | * '''sin(x)''' and '''cos(x)''' | + | * '''log(x)''' returns natural '''logarithm''' value of '''x''' |
+ | * '''sin(x)''' and '''cos(x)''' gives '''sine(x)''' and '''cosine(x)''' respectively | ||
* Please note that '''argument x '''should be mentioned in '''radians'''. | * Please note that '''argument x '''should be mentioned in '''radians'''. | ||
Line 83: | Line 79: | ||
− | The same is available in the ''' | + | The same is available in the '''Code Files''' link. |
|- | |- | ||
| 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 print statements for sqrt () | | 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 print statements for sqrt () | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Here, we are printing the square root of a positive and negative number respectively. | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Here, we are printing the '''square root''' of a positive and negative number respectively. |
|- | |- | ||
Line 103: | Line 99: | ||
log () | log () | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| After that, natural | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| After that, natural '''logarithm''' of positive and negative numbers are printed. |
|- | |- | ||
Line 158: | Line 154: | ||
− | It returns '''nan''' or not a number if the number is negative. | + | It returns '''nan''' or '''not a number''' if the number is negative. |
Line 170: | Line 166: | ||
− | Natural logarithm of positive number is given by '''log() function'''. | + | Natural '''logarithm''' of positive number is given by '''log() function'''. |
Line 183: | Line 179: | ||
|- | |- | ||
| 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 7: Random Functions | | 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 7: Random Functions | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now, | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now, let us look at '''random functions.''' |
− | + | ||
* '''rand()''' returns any random number between 0 and 1. | * '''rand()''' returns any random number between 0 and 1. | ||
* But never returns 0 or 1. | * But never returns 0 or 1. | ||
− | |||
− | |||
|- | |- | ||
| 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 7: Random Functions | | 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 7: Random Functions | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| * Generated numbers will be random within one '''awk '''execution. | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| |
− | * But predictable across different executions of the awk program. | + | * Generated numbers will be random within one '''awk '''execution. |
− | + | * But predictable across different executions of the '''awk''' program. | |
− | + | ||
|- | |- | ||
| 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 7: Random Functions | | 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 7: Random Functions | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| * '''srand(x)''' is used to provide '''seed value x''' for '''random function.''' | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| |
+ | * '''srand(x)''' is used to provide '''seed value x''' for '''random function.''' | ||
* In absence of '''x''', date and time of day is used as the '''seed value.''' | * In absence of '''x''', date and time of day is used as the '''seed value.''' | ||
Line 225: | Line 218: | ||
So, this code will generate 5 random numbers within 50. | So, this code will generate 5 random numbers within 50. | ||
+ | |- | ||
+ | |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 and clear it | ||
+ | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the '''terminal''' and execute the file. | ||
− | + | Let me clear the '''terminal'''. | |
|- | |- | ||
Line 238: | Line 234: | ||
|- | |- | ||
| 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;"| Show 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;"| Show the output | ||
− | |||
− | |||
− | |||
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| See, it is giving 5 random numbers. | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| See, it is giving 5 random numbers. | ||
− | + | |- | |
− | What happens if I execute the code again? | + | | 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;"| Press up key and Enter |
+ | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| What happens if I execute the code again? | ||
Line 256: | Line 250: | ||
Which means, '''awk '''is generating the same set of random numbers for every execution of the script. | Which means, '''awk '''is generating the same set of random numbers for every execution of the script. | ||
− | + | |- | |
+ | | 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 editor | ||
+ | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | ||
Then how can we get a new set of random numbers in every execution? | Then how can we get a new set of random numbers in every execution? | ||
Line 266: | Line 262: | ||
<nowiki>srand() and [enter]</nowiki> | <nowiki>srand() and [enter]</nowiki> | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Before the '''for loop''', type '''srand()''' | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Before the '''for loop''', type '''srand() function''' |
− | + | ||
+ | |- | ||
+ | | 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;"| Press Ctrl+S keys | ||
+ | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | ||
Press Crtl and S keys to the save the file. | Press Crtl and S keys to the save the file. | ||
Line 278: | 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;"| Press up key and enter | | 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;"| Press up key and enter | ||
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press the Up arrow key to get the previously executed command and press '''Enter'''. | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Press the Up arrow key to get the previously executed command and press '''Enter'''. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 302: | Line 289: | ||
− | '''length | + | '''length function''' gives the length of a particular string '''s''' |
− | '''index | + | '''index function''' determines the position of''' string s2 '''within the larger '''string s1.''' |
− | For example, '''index | + | For example, '''index within parentheses within double quotes linux comma within double quotes n''', returns 3. |
Line 319: | Line 306: | ||
|- | |- | ||
| 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;"| Show the file opened and highlight rollnumber column | | 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;"| Show the file opened and highlight rollnumber column | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We know that each student in the '''awkdemo.txt '''file has a 4 digit | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We know that each student in the '''awkdemo.txt '''file has a 4 digit roll number. |
Line 328: | Line 315: | ||
|- | |- | ||
− | | 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;"| In terminal | + | | 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;"| |
+ | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the '''terminal '''. | ||
+ | |||
+ | Let me clear the '''terminal'''. | ||
+ | |||
+ | |- | ||
+ | | 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;"| In terminal type: | ||
awk -F"|" 'length($1)!=4 {print}' awkdemo.txt | awk -F"|" 'length($1)!=4 {print}' awkdemo.txt | ||
Line 337: | Line 330: | ||
<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 '''command '''as shown here. |
Line 350: | Line 343: | ||
|- | |- | ||
| 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;"| Show 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;"| Show the output | ||
− | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| See, there is | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| See, there is one roll-number '''S02''' that has been typed incorrectly. |
Line 361: | Line 354: | ||
Let me explain the parameters. | Let me explain the parameters. | ||
− | |||
* Here '''s''' is the '''string''' | * Here '''s''' is the '''string''' | ||
Line 418: | Line 410: | ||
|- | |- | ||
− | | style=" | + | | 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 10: Split function |
− | | style=" | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We have seen the '''function split''' earlier. |
So, I am not explaining the details here. | So, I am not explaining the details here. | ||
− | Please refer to the earlier awk tutorials if you have any doubt. | + | Please refer to the earlier '''awk''' tutorials if you have any doubt. |
|- | |- | ||
Line 432: | Line 424: | ||
| style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| There are some other '''functions''' which are related to '''Input/Output'''. | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| There are some other '''functions''' which are related to '''Input/Output'''. | ||
− | |||
* '''system() function''' - helps us to run any '''unix command '''within '''awk'''. | * '''system() function''' - helps us to run any '''unix command '''within '''awk'''. | ||
− | |||
− | |||
|- | |- | ||
Line 448: | Line 437: | ||
<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;"| In the '''terminal''' type | + | | style="border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In the '''terminal''' type the command as shown here. |
− | + | ||
− | + | ||
Line 497: | Line 484: | ||
* '''Input/Output functions''' | * '''Input/Output functions''' | ||
* '''Time stamps functions''' | * '''Time stamps functions''' | ||
− | |||
− | |||
|- | |- | ||
Line 510: | Line 495: | ||
Write an '''awk''' program to print the last '''field''' of every record | Write an '''awk''' program to print the last '''field''' of every record | ||
− | * where name of the student has small '''u''' as the third letter using the '''awkdemo.txt '''file. | + | * where name of the student has small '''u''' as the third letter |
− | + | * using the '''awkdemo.txt '''file. | |
− | + | ||
|- | |- |
Latest revision as of 16:38, 22 March 2018
Title of script: Built-in functions in awk
Author: Antara Roy Choudhury
Keywords: awk Built-in function, Arithmetic function, String function, Input/Output function, Timestamps function, length(), substr(), int(), system()
|
|
Slide 1: Introduction | Welcome to this spoken tutorial on built-in functions in awk. |
Slide 2: Learning Objectives | In this tutorial we will learn about different types of built-in functions like-
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: Arithmetic Functions | Built-in functions are always available for awk to call.
|
Slide 6: Arithmetic Functions |
Let’s look at an example to understand these functions. |
Show arithmetic_function.awk
in Gedit |
I have already written the code in a file arithmetic underscore function dot awk
|
Highlight print statements for sqrt () | Here, we are printing the square root of a positive and negative number respectively. |
Highlight print statements for
int () |
Next we are printing the integer value for a positive and negative number respectively. |
Highlight print statements for exp () | Then we are printing exponential of a small number and a very large number. |
Highlight print statements for
log () |
After that, natural logarithm of positive and negative numbers are printed. |
Highlight print statements for sine and cosine () | We are also printing sine and cosine values of 0.52 radian, that is actually 30 degree.
|
Open the terminal by pressing Ctrl, Alt and T Keys. | |
cd /<saved folder> | Next go to the folder where you have downloaded and extracted the file using cd command. |
Type:
awk -f arithmetic_function.awk |
Now type awk space -f arithmetic_function.awk
|
Highlight Square root of positive
|
sqrt() function gives square root of a positive number.
|
Slide 7: Random Functions | Now, let us look at random functions.
|
Slide 7: Random Functions |
|
Slide 7: Random Functions |
Let us understand these with an example. |
Show random.awk in Gedit | I have written a code for the random function and saved it as random.awk |
Highlight rand ()
|
Here, inside the for loop, rand() function will generate a random number between 0 and 1.
|
Switch to terminal and clear it | Switch to the terminal and execute the file.
Let me clear the terminal. |
Type
awk -f random.awk |
Type:
awk space hyphen f space random dot awk and press Enter. |
Show the output | See, it is giving 5 random numbers. |
Press up key and Enter | What happens if I execute the code again?
|
Show the output and highlight | We are getting the same output.
|
Switch to the editor |
Then how can we get a new set of random numbers in every execution?
|
Type
srand() and [enter] |
Before the for loop, type srand() function |
Press Ctrl+S keys |
Press Crtl and S keys to the save the file. |
Now switch to the terminal. | |
Press up key and enter | Press the Up arrow key to get the previously executed command and press Enter. |
Show the output | It is giving a different set of random numbers.
|
Slide 8: String Functions | Next will see some string functions.
|
show awkdemo.txt in gedit | Open the file awkdemo.txt |
Show the file opened and highlight rollnumber column | We know that each student in the awkdemo.txt file has a 4 digit roll number.
|
Switch to the terminal .
Let me clear the terminal. | |
In terminal type:
awk -F"|" 'length($1)!=4 {print}' awkdemo.txt
|
Now type the command as shown here.
|
Show the output | See, there is one roll-number S02 that has been typed incorrectly.
|
Slide 9: Substring function | The substr(s,a,b) function extracts a substring from a larger string s.
Let us see one example. |
Show awkdemo.txt in Gedit | Switch to the awkdemo.txt file. |
Show the file opened
|
The first letter of the roll numbers represents the Hostel code where the particular student resides.
|
Type
awk -F"|" 'substr($1,1,1)=="A" {print $1,$2}' awkdemo.txt |
Type the command as shown here.
|
Highlight substr($1,1,1) | Here we take the string denoted by $1.
|
Highlight substr($1,1,1) | Next, we extract a substring that starts at position one with the character length one. |
Highlight =="A" | Then, if it is equal to capital A, then that line from the file will get printed.
|
Show the output | We got the list of students who are in Hostel A. |
Slide 10: Split function | We have seen the function split earlier.
Please refer to the earlier awk tutorials if you have any doubt. |
Slide 11: I/O Functions
|
There are some other functions which are related to Input/Output.
|
Now, we will run the unix command date through awk command. | |
Type
awk 'BEGIN{system("date")}' [Enter] |
In the terminal type the command as shown here.
|
Show the output | Today’s date and time is displayed on the terminal as an output. |
Hover your mouse on the command
awk 'BEGIN{system("date")}' |
Now, why do we need this?
We have kept only the BEGIN section of the awk command. |
Hover your mouse on the output | In real world scenarios, we may want to print the system date, before displaying the required output.
|
Slide 12: Time Stamps | There are some functions dealing with time stamps like
Browse through the Internet to know about these functions. |
Slide 13: Summary | This brings us to the end of this tutorial.
Let us summarize.
|
Slide 14: Assignment
|
As an assignment-
|
Slide 15:
About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it. |
Slide 16:
Spoken Tutorial workshops |
The Spoken Tutorial Project team conducts workshops using spoken tutorials.
|
Slide 17:
Forum for specific questions: |
Please post your timed queries in this forum. |
Slide 18: Acknowledgement | Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
this link. |
Slide 19: Thank You | The script has been contributed by Antara.
Thank you for joining |