Difference between revisions of "Linux-AWK/C2/More-on-Single-Dimensional-Array-in-awk/English"
Nancyvarkey (Talk | contribs) |
Nancyvarkey (Talk | contribs) |
||
Line 13: | Line 13: | ||
|- | |- | ||
| style="background-color:#ffffff;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.191cm;padding-right:0.191cm;"| Display Slide 1 | | style="background-color:#ffffff;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.191cm;padding-right:0.191cm;"| Display Slide 1 | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Hello, welcome to this spoken tutorial '''More on | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Hello, welcome to this spoken tutorial '''More on single dimensional array''' in '''awk'''. |
|- | |- | ||
| style="background-color:#ffffff;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.191cm;padding-right:0.191cm;"| Display Slide 2 | | style="background-color:#ffffff;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.191cm;padding-right:0.191cm;"| Display Slide 2 | ||
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about- | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about- | ||
− | |||
− | |||
* Using''' awk array '''with file | * Using''' awk array '''with file | ||
* Scan the '''elements '''of an '''array''' | * Scan the '''elements '''of an '''array''' | ||
Line 33: | Line 31: | ||
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To record this tutorial, I am using | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To record this tutorial, I am using | ||
− | * '''Ubuntu Linux 16.04 Operating System '''and | + | * '''Ubuntu Linux 16.04 Operating System '''and |
* '''gedit text editor 3.20.1''' | * '''gedit text editor 3.20.1''' | ||
Line 42: | Line 40: | ||
prerequisite | prerequisite | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To practice this tutorial, you should have gone through previous '''awk''' tutorial on array in our website. | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To practice this tutorial, you should have gone through previous '''awk''' tutorial on '''array''' in our website. |
Line 139: | Line 137: | ||
− | Open the '''terminal''' by pressing '''Ctrl | + | Open the '''terminal''' by pressing '''Ctrl, Alt''' and '''T''' keys. |
|- | |- | ||
Line 156: | Line 154: | ||
− | ''' | + | Press '''Enter''' |
|- | |- | ||
Line 176: | Line 174: | ||
<nowiki>delete hra["S02"] before for loop</nowiki> | <nowiki>delete hra["S02"] before for loop</nowiki> | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Before the '''for loop''', | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Before the '''for loop''', press '''Enter''' and type |
− | |||
− | + | '''delete space hra '''within square brackets within double quotes''''' S02''' | |
− | '''delete | + | |
|- | |- | ||
Line 191: | Line 187: | ||
<nowiki>[Enter]</nowiki> | <nowiki>[Enter]</nowiki> | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let me clear the terminal. | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let me clear the '''terminal'''. |
− | Press the Up arrow key to get the previously executed command | + | Press the Up arrow key to get the previously executed command. |
Line 205: | Line 201: | ||
|- | |- | ||
| style="background-color:#ffffff;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.191cm;padding-right:0.191cm;"| Display slide 6 | | style="background-color:#ffffff;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.191cm;padding-right:0.191cm;"| Display slide 6 | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * So, any '''array element '''can be deleted using the '''delete''' | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| |
− | * You have to mention the '''array''' name | + | * So, any '''array element '''can be deleted using the '''delete command'''. |
+ | * You have to mention the '''array''' name with the '''index'''. | ||
* What if I want to delete an entire '''array'''? | * What if I want to delete an entire '''array'''? | ||
* It can be done by specifying only the '''array '''name in the '''delete statement''' | * It can be done by specifying only the '''array '''name in the '''delete statement''' | ||
Line 224: | Line 221: | ||
<nowiki>[Enter]</nowiki> | <nowiki>[Enter]</nowiki> | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Clear the terminal. | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Clear the '''terminal'''. |
Line 283: | Line 280: | ||
|- | |- | ||
| style="background-color:#ffffff;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.191cm;padding-right:0.191cm;"| awk -f argc_argv.awk one two three | | style="background-color:#ffffff;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.191cm;padding-right:0.191cm;"| awk -f argc_argv.awk one two three | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now type on the terminal | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now type on the '''terminal'''- |
− | + | ||
'''awk space hyphen small f space argc underscore argv dot awk space one space two space three''' | '''awk space hyphen small f space argc underscore argv dot awk space one space two space three''' | ||
Line 367: | Line 363: | ||
* '''Delete statement''' | * '''Delete statement''' | ||
* '''ARGV array '''and '''ENVIRON array''' | * '''ARGV array '''and '''ENVIRON array''' | ||
− | |||
− | |||
|- | |- | ||
Line 380: | Line 374: | ||
− | Calculate certain allowances for the students who have passed | + | Calculate certain allowances for the students who have passed. |
# Paper presentation allowance which is 80% of the stipend | # Paper presentation allowance which is 80% of the stipend | ||
# Performance incentive which is 20% of the stipend | # Performance incentive which is 20% of the stipend | ||
− | |||
− | |||
|- | |- | ||
Line 397: | Line 389: | ||
− | Print the total amount required for each allowance | + | Print the total amount required for each allowance and the average. |
− | + | ||
− | and the average. | + | |
|- | |- | ||
Line 441: | Line 431: | ||
− | More information on this mission is available at | + | More information on this mission is available at this link. |
− | + | ||
− | this link. | + | |
|- | |- |
Revision as of 13:13, 20 February 2018
Title of script: More on Single Dimensional Array in Awk
Author: Antara Roy Choudhury
Keywords: awk array with file, scan array element, delete, ARGV array and ENVIRON array
|
|
Display Slide 1 | Hello, welcome to this spoken tutorial More on single dimensional array in awk. |
Display Slide 2 | In this tutorial we will learn about-
We will do this through some examples. |
Display Slide 3
System requirement |
To record this tutorial, I am using
You can use any text editor of your choice. |
Display Slide 4
prerequisite |
To practice this tutorial, you should have gone through previous awk tutorial on array in our website.
|
Slide 5: Code Files | The files used in this tutorial are available in the Code Files link on this tutorial page.
|
Retain same screen | Previously we covered some aspects of awk arrays.
|
Show awkdemo.txt in Gedit | We will use the same file awkdemo.txt that we have used earlier. |
Show file opened | Here the first field is roll number and sixth field is stipend of the student.
|
Show calculate_hra.awk
and highlight appropriately |
Inside the BEGIN section, the field separator is initialized with the Pipe symbol.
|
Display slide 5 | How can we scan all the elements of this array?
You should use this variation of for loop. This loop executes statements once for each index in the array. The variable var will be set to the index values one by one. |
Show calculate_hra.awk
and highlight appropriately |
The code is written inside END section.
This section will be executed after awk has processed all the lines of the input file.
|
Switch to the terminal and execute the file.
| |
cd /<saved folder> | Go to the folder in which you downloaded and extracted the Code Files using cd command |
Type
awk -f calculate_hra.awk awkdemo.txt [Enter] |
Now type:
awk space hyphen small f space calculate_hra.awk space awkdemo.txt
|
Show the output | The output shows the roll number and HRA for all the students. |
Highlight in the output | Now say, I want to delete the record for the student with roll S02.
|
Type:
delete hra["S02"] before for loop |
Before the for loop, press Enter and type
|
Press Ctrl+S | Save the file and switch to the terminal. |
Press up key
[Enter] |
Let me clear the terminal.
|
Show the output | The record for the student with roll number S02 is not printed in the output. |
Display slide 6 |
Let us switch to the code to try this. |
Delete ["S02"] from the Delete statement | Delete the index S02 along with the quotes and square brackets from the delete statement. |
Save the file and switch to the terminal | Save the file and switch to the terminal. |
Press up key
[Enter] |
Clear the terminal.
|
Show the output | See, we are not getting any output.
|
Display Slide 7 | Remember, in an earlier tutorial on awk built-in variables, we had said-
ARGC implies the number of command line arguments. ARGV is an array that stores the command line arguments.
Let us see. |
Switch to the open file argc_argv.awk | I have already written the code in argc_argv.awk.
|
Show argc_argv.awk in gedit
|
The code is written within awk BEGIN section.
|
awk -f argc_argv.awk one two three | Now type on the terminal-
awk space hyphen small f space argc underscore argv dot awk space one space two space three
|
Show the output and highlight | We get the number of arguments as 4.
|
Display slide 8 | Now, let us take one more example.
|
Display slide 9 | The array element indices are the environment variable names.
The array element values are the values of the particular environment variables. |
Display slide 10 | Let us see how we can see the values of different environment variables.
|
Type:
awk 'BEGIN { print ENVIRON["USER"] }' [Enter] |
At the command prompt type the following.
|
Show the output | The output will show the name of the user who logged in.
|
Display Slide 11
Summary |
In this tutorial we learnt about-
|
Display Slide 12
Assignment
|
As an assignment-
|
Display Slide 13
Assignment
|
Store the allowances in two different arrays.
|
Display Slide 14 | Assignment 2.
|
Display Slide 15
About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it. |
Display Slide 16
Spoken Tutorial workshops |
The Spoken Tutorial Project team conducts workshops using spoken tutorials.
|
Display Slide 17
Forum for specific questions: |
Please post your timed queries in this forum. |
Display Slide 18
Acknowledgement |
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
|
The script has been contributed by Antara.
Thank you for joining |