Difference between revisions of "Linux-AWK/C2/More-on-Single-Dimensional-Array-in-awk/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(2 intermediate revisions by the same user not shown)
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''' '''single dimensional array''' in '''awk'''.
+
| 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''', '''Alt''' and '''T''' keys
+
Open the '''terminal''' by pressing '''Ctrl, Alt''' and '''T''' keys.
  
 
|-
 
|-
Line 156: Line 154:
  
  
'''press Enter'''
+
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''', Press Enter and  
+
| 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
  
type
 
  
 
+
'''delete space hra '''within square brackets within double quotes''''' S02'''
'''delete '''''space''''' hra '''''within square brackets within double quotes''''' S02'''
+
  
 
|-
 
|-
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''' command.
+
| 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''' '''with the '''index'''.
+
* 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.
+
  
 
|-
 
|-

Latest revision as of 16:37, 22 March 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


Visual Cue
Narration
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-
  • Using awk array with file
  • Scan the elements of an array
  • Delete statement
  • ARGV array and ENVIRON array

We will do this through some examples.

Display Slide 3

System requirement

To record this tutorial, I am using
  • Ubuntu Linux 16.04 Operating System and
  • gedit text editor 3.20.1

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.


You should have some basic knowledge of any programming language like C or C++.


If not, then please go through the corresponding tutorials on our website.

Slide 5: Code Files The files used in this tutorial are available in the Code Files link on this tutorial page.


Please download and extract them.

Retain same screen Previously we covered some aspects of awk arrays.


Now let us see how we can use awk array with a file.

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.


Let us first calculate the HRA for all students.


Here, HRA is 30% of their stipend amount.


I have already written the code and saved it as calculate_hra.awk.


Let us look in to that file now.

Show calculate_hra.awk

and highlight appropriately

Inside the BEGIN section, the field separator is initialized with the Pipe symbol.


Then, in the action section we are initializing the array elements.


This section will get executed once for each line of an input file.


I have declared HRA as an array variable and dollar one as index.


Here dollar 1 indicates the 1st field, that is roll number.


And the value is dollar 6 multiplied by zero point 3 wherein dollar six is the stipend value.


So, array HRA at index roll number will contain the corresponding HRA amount as a value.

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.


Variable i will be initialized to every index value or roll number one by one.


In each iteration of the for loop, the HRA for a particular roll number will be printed.

Switch to the terminal and execute the file.


Open the terminal by pressing Ctrl, Alt and T keys.

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


Press Enter

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.


So, we have to delete the array element at index S02.


Let me do this using the calculate_hra.awk code.

Type:

delete hra["S02"] before for loop

Before the for loop, press Enter and type


delete space hra within square brackets within double quotes S02

Press Ctrl+S Save the file and switch to the terminal.
Press up key

[Enter]

Let me clear the terminal.


Press the Up arrow key to get the previously executed command.


Press Enter.

Show the output The record for the student with roll number S02 is not printed in the output.
Display slide 6
  • 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?
  • It can be done by specifying only the array name in the delete statement

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.


Press the Up arrow key to get the previously executed command.


Now press Enter.

Show the output See, we are not getting any output.


The entire array has been deleted.

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.


How can we show their values?

Let us see.

Switch to the open file argc_argv.awk I have already written the code in argc_argv.awk.


Let us check the contents.

Show argc_argv.awk in gedit


Point as per narration


The code is written within awk BEGIN section.


First we are printing the number of arguments, that is the value of ARGV.


Next, using the for loop, we are looping for the value of i from 0 to ARGC-1.


And we are printing ARGV at index i


Switch to the terminal and execute the file.

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


Here one two three are the command line arguments.


Press Enter to execute the command.

Show the output and highlight We get the number of arguments as 4.


But recall that we have supplied only 3 arguments.


Let us have a look at the individual arguments.


First argument or argv at index 0 is actually awk, the command name.


Next we have three arguments that we supplied in the command line.


That is why the value of ARGC is always the number of command line arguments supplied plus one.

Display slide 8 Now, let us take one more example.


Built-in variable ENVIRON is an associative array of environment variables.

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.


First, let us print our username.


We need to print the value of environment variable USER.

Type:

awk 'BEGIN { print ENVIRON["USER"] }'

[Enter]

At the command prompt type the following.


Press Enter

Show the output The output will show the name of the user who logged in.


This brings us to the end of this tutorial.


Let us summarize.

Display Slide 11

Summary

In this tutorial we learnt about-
  • Using awk array with file
  • Scan the elements of an array
  • Delete statement
  • ARGV array and ENVIRON array
Display Slide 12

Assignment


As an assignment-


Calculate certain allowances for the students who have passed.

  1. Paper presentation allowance which is 80% of the stipend
  2. Performance incentive which is 20% of the stipend
Display Slide 13

Assignment


Store the allowances in two different arrays.


Print the total amount required for each allowance and the average.

Display Slide 14 Assignment 2.


Print the value of environment variable PATH from awk program.

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.


And gives certificates on passing online tests.


For more details, please write to us.

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.


More information on this mission is available at this link.

The script has been contributed by Antara.


And this is Praveen from IIT Bombay signing off.

Thank you for joining

Contributors and Content Editors

Antarade, Nancyvarkey