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

From Script | Spoken-Tutorial
Revision as of 17:55, 10 July 2019 by Sandhya.np14 (Talk | contribs)

Jump to: navigation, search


Time
Narration
00:01 Hello and welcome to this spoken tutorial- More on single dimensional array in awk.
00:07 In this tutorial, we will learn about-

Using awk array with file,

00:13 scan the elements of an array,
00:16 Delete statement,
00:18 ARGV array and ENVIRON array.
00:22 We will do this through some examples.
00:25 To record this tutorial, I am using:

Ubuntu Linux 16.04 Operating System and

gedit text editor 3.20.1

00:37 You can use any text editor of your choice.
00:41 To practice this tutorial, you should have gone through previous awk tutorial on array in our website.
00:48 You should have some basic knowledge of any programming language like C or C++.
00:55 If not, then please go through the corresponding tutorials on our website.
01:00 The files used in this tutorial are available in the Code Files link on this tutorial page.

Please download and extract them.

01:10 Previously we covered some aspects of awk arrays.
01:14 Now let us see how we can use awk array with a file.
01:19 We will use the same file awkdemo.txt that we have used earlier.
01:25 Here, the first field is roll number and sixth field is stipend of the student.
01:32 Let us first calculate the HRA for all students.
01:36 Here, HRA is 30% of their stipend amount.
01:41 I have already written the code and saved it as calculate_hra.awk.

Let us look into that file now.

01:51 Inside the BEGIN section, the field separator is initialized with the Pipe symbol.
01:57 Then, in the action section we are initializing the array elements.
02:02 This section will get executed once for each line of an input file.
02:08 I have declared HRA as an array variable and dollar one as index.
02:14 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.

02:27 So, array HRA at index roll number will contain the corresponding HRA amount as a value.
02:35 How can we scan all the elements of this array?
02:39 You should use this variation of for loop.
02:43 This loop executes statements once for each index in the array.
02:48 The variable var will be set to the index values one by one.
02:53 The code is written inside END section.
02:57 This section will be executed after awk has processed all the lines of the input file.
03:04 Variable i will be initialized to every index value or roll number one by one.
03:10 In each iteration of the for loop, the HRA for a particular roll number will be printed.
03:16 Switch to the terminal and execute the file.

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

03:24 Go to the folder in which you downloaded and extracted the Code Files using 'cd' command.
03:31 Now, type: awk space hyphen small f space calculate_hra.awk space awkdemo.txt .

Press Enter.

03:45 The output shows the roll number and HRA for all the students.
03:50 Now say, I want to delete the record for the student with roll S02.
03:56 So, we have to delete the array element at index S02.
04:01 Let me do this using the calculate_hra.awk code.
04:06 Before the for loop, press Enter and type the following code:

delete space hra within square brackets within double quotes S02.

04:19 Save the file and switch to the terminal.
04:23 Let me clear the terminal.
04:26 Press the up arrow key to get the previously executed command.

Press Enter.

04:33 The record for the student with roll number S02 is not printed in the output.
04:39 So, any array element can be deleted using the delete command.
04:44 You have to mention the array name with the index.
04:48 What if I want to delete an entire array?

It can be done by specifying only the array name in the delete statement.

04:56 Let us switch to the code to try this.
04:59 Delete the index S02 along with the quotes and square brackets from the delete statement.
05:07 Save the file and switch to the terminal.
05:10 Clear the terminal. Press the up arrow key to get the previously executed command.

Now press Enter.

05:19 See, we are not getting any output.

The entire array has been deleted.

05:25 Remember, in an earlier tutorial on awk built-in variables, we had said-

ARGC implies the number of command line arguments.

05:36 ARGV is an array that stores the command line arguments.

How can we show their values?

Let us see.

05:45 I have already written the code in argc_argv.awk.

Let us check the contents.

05:53 The code is written within awk BEGIN section.
05:57 First we are printing the number of arguments, that is the value of ARGC.
06:03 Next, using the for loop, we are looping for the value of i from 0 to ARGC-1.
06:11 And, we are printing ARGV at index i.

Switch to the terminal and execute the file.

06:19 Now, type on the terminal- awk space hyphen small f space argc underscore argv dot awk space one space two space three.
06:35 Here, one two three are the command line arguments.

Press Enter to execute the command.

06:43 We get the number of arguments as 4.

But recall that we have supplied only 3 arguments.

06:50 Let us have a look at the individual arguments.

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

07:02 Next, we have three arguments that we supplied in the command line.
07:07 That is why the value of ARGC is always the number of command line arguments supplied plus one.
07:16 Let us take one more example.

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

07:24 The array element indices are the environment variable names.

The array element values are the values of the particular environment variables.

07:35 Let us see how we can see the values of different environment variables.
07:40 First, let us print our username.
07:43 We need to print the value of environment variable USER.
07:48 At the command prompt, type the following.
07:53 Press Enter.
07:55 The output will show the name of the user who logged in.
08:00 This brings us to the end of this tutorial.

Let us summarize.

08:05 In this tutorial, we learnt about-

Using awk array with file,

08:11 Scan the elements of an array,
08:14 Delete statement,

ARGV array and ENVIRON array.

08:20 As an assignment-

Calculate certain allowances for the students who have passed.

08:25 Paper presentation allowance which is 80% of the stipend,
08:30 Performance incentive which is 20% of the stipend.
08:35 Store the allowances in two different arrays.
08:38 Print the total amount required for each allowance and the average.
08:43 Print the value of environment variable PATH from awk program.
08:48 The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

08:56 The Spoken Tutorial Project team conducts workshops using spoken tutorials.

And, gives certificates on passing online tests.

09:05 For more details, please write to us.
09:08 Please post your timed queries in this forum.
09:12 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

09:24 The script has been contributed by Antara.

And, this is Praveen from IIT Bombay, signing off.

Thank you for joining.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14