Linux-AWK/C2/More-on-Single-Dimensional-Array-in-awk/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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