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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| border=1 | <center>'''Time'''</center> | <center>'''Narration'''</center> |- | 00:01 | Hello, welcome to this spoken tutorial '''More on single dimensional array''' in '...")
 
Line 7: Line 7:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Hello, welcome to this spoken tutorial '''More on single dimensional array''' in '''awk'''.
+
| Hello and welcome to this spoken tutorial- '''More on single dimensional array in awk'''.
  
 
|-
 
|-
 
| 00:07
 
| 00:07
| In this tutorial we will learn about-  Using''' awk array '''with file
+
| In this tutorial, we will learn about-   
 +
Using''' awk array '''with file,
  
 
|-
 
|-
 
| 00:13
 
| 00:13
|Scan the '''elements '''of an '''array'''
+
|'''scan''' the '''element'''s of an '''array''',
  
 
|-
 
|-
 
| 00:16
 
| 00:16
|'''Delete statement'''
+
|'''Delete statement''',
  
 
|-
 
|-
 
| 00:18
 
| 00:18
|  '''ARGV array '''and '''ENVIRON array'''
+
|  '''ARGV array '''and '''ENVIRON array'''.
  
 
|-
 
|-
Line 31: Line 32:
 
|-
 
|-
 
| 00:25
 
| 00:25
| To record this tutorial, I am using  '''Ubuntu Linux 16.04 Operating System '''and  
+
| To record this tutorial, I am using:  
 +
'''Ubuntu Linux 16.04 Operating System '''and  
  
 
'''gedit text editor 3.20.1'''
 
'''gedit text editor 3.20.1'''
Line 37: Line 39:
 
|-
 
|-
 
| 00:37
 
| 00:37
| You can use any text editor of your choice.
+
| You can use any '''text editor''' of your choice.
  
 
|-
 
|-
Line 55: Line 57:
 
| The files used in this tutorial are available in the '''Code Files''' link on this tutorial page.
 
| The files used in this tutorial are available in the '''Code Files''' link on this tutorial page.
  
Please download and extract them.
+
Please download and '''extract''' them.
  
 
|-
 
|-
Line 71: Line 73:
 
|-
 
|-
 
| 01:25
 
| 01:25
| Here the first '''field '''is roll number and sixth '''field '''is stipend of the student.
+
| Here, the first '''field '''is roll number and sixth '''field '''is stipend of the student.
  
 
|-
 
|-
Line 83: Line 85:
 
|-
 
|-
 
| 01:41
 
| 01:41
| I have already written the code and saved it as '''calculate_hra.awk'''.
+
| I have already written the '''code''' and saved it as '''calculate_hra.awk'''.
  
 
Let us look into that file now.
 
Let us look into that file now.
Line 89: Line 91:
 
|-
 
|-
 
| 01:51
 
| 01:51
| Inside the '''BEGIN section''', the '''field separator '''is initialized with the '''Pipe symbol'''.
+
| Inside the '''BEGIN '''section, the '''field separator '''is initialized with the '''Pipe '''symbol.
  
 
|-
 
|-
 
| 01:57
 
| 01:57
| Then, in the '''action section '''we are initializing the '''array elements.'''
+
| Then, in the '''action''' section we are initializing the '''array elements.'''
  
 
|-
 
|-
 
| 02:02
 
| 02:02
| This '''section '''will get executed once for each line of an input file.
+
| This section will get executed once for each line of an input file.
  
 
|-
 
|-
 
| 02:08
 
| 02:08
| I have declared '''HRA''' as an '''array '''variable and '''dollar one '''as '''index'''.
+
| I have declared '''HRA''' as an '''array variable''' and '''dollar one '''as '''index'''.
  
 
|-
 
|-
Line 107: Line 109:
 
| Here '''dollar 1 '''indicates the 1st '''field''', that is roll number.
 
| 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.
+
And, the value is '''dollar 6''' multiplied by zero point 3 wherein '''dollar six''' is the stipend value.
  
 
|-
 
|-
Line 115: Line 117:
 
|-
 
|-
 
| 02:35
 
| 02:35
| How can we scan all the '''elements '''of this '''array'''?
+
| How can we '''scan''' all the '''elements '''of this '''array'''?
  
 
|-
 
|-
Line 131: Line 133:
 
|-
 
|-
 
| 02:53
 
| 02:53
| The code is written inside '''END section'''.  
+
| The code is written inside '''END''' section.  
  
 
|-
 
|-
 
| 02:57
 
| 02:57
| This '''section''' will be executed after '''awk''' has processed all the lines of the input file.
+
| This section will be executed after '''awk''' has processed all the lines of the input file.
  
 
|-
 
|-
Line 147: Line 149:
 
|-
 
|-
 
| 03:16
 
| 03:16
| Switch to the '''terminal''' and execute the file.
+
| Switch to the '''terminal''' and '''execute''' the file.
  
 
Open the '''terminal''' by pressing '''Ctrl, Alt''' and '''T''' keys.
 
Open the '''terminal''' by pressing '''Ctrl, Alt''' and '''T''' keys.
Line 153: Line 155:
 
|-
 
|-
 
| 03:24
 
| 03:24
| Go to the folder in which you downloaded and extracted the '''Code Files''' using '''cd command'''
+
| Go to the folder in which you downloaded and extracted the '''Code Files''' using ''''cd' command'''.
  
 
|-
 
|-
 
| 03:31
 
| 03:31
| Now type: '''awk space hyphen small f space calculate_hra.awk space awkdemo.txt '''
+
| Now, type: '''awk space hyphen small f space calculate_hra.awk space awkdemo.txt '''.
  
Press '''Enter'''
+
Press '''Enter'''.
  
 
|-
 
|-
Line 167: Line 169:
 
|-
 
|-
 
| 03:50
 
| 03:50
| Now say, I want to delete the record for the student with roll S02.
+
| Now say, I want to delete the '''record''' for the student with roll '''S02'''.
  
 
|-
 
|-
Line 179: Line 181:
 
|-
 
|-
 
| 04:06
 
| 04:06
| Before the '''for loop''', press '''Enter''' and type the following code
+
| Before the '''for loop''', press '''Enter''' and type the following code:
  
'''delete space hra '''within square brackets within double quotes''''' S02'''
+
'''delete space hra '''within square brackets within double quotes''' S02'''.
  
 
|-
 
|-
 
| 04:19
 
| 04:19
| Save the file and switch to the '''terminal.'''
+
| '''Save''' the file and switch to the '''terminal.'''
  
 
|-
 
|-
Line 193: Line 195:
 
|-
 
|-
 
| 04:26
 
| 04:26
| Press the Up arrow key to get the previously executed command.
+
| Press the '''up arrow key''' to get the previously executed command.
  
 
Press '''Enter'''.
 
Press '''Enter'''.
Line 199: Line 201:
 
|-
 
|-
 
| 04:33
 
| 04:33
| The record for the student with roll number S02 is not printed in the output.
+
| The '''record''' for the student with roll number '''S02''' is not printed in the output.
  
 
|-
 
|-
 
| 04:39
 
| 04:39
|  So, any '''array element '''can be deleted using the '''delete command'''.
+
|  So, any '''array element '''can be deleted using the '''delete''' command.
  
 
|-
 
|-
Line 213: Line 215:
 
|  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 229: Line 231:
 
|-
 
|-
 
| 05:10
 
| 05:10
| Clear the '''terminal'''. Press the Up arrow key to get the previously executed command.
+
| Clear the '''terminal'''. Press the '''up arrow''' key to get the previously executed command.
  
 
Now press '''Enter'''.
 
Now press '''Enter'''.
Line 261: Line 263:
 
|-
 
|-
 
| 05:53
 
| 05:53
| The code is written within '''awk BEGIN section.'''
+
| The code is written within '''awk BEGIN '''section.
  
 
|-
 
|-
 
| 05:57
 
| 05:57
| First we are printing the number of '''arguments''', that is the value of '''ARGV.'''
+
| First we are printing the number of '''arguments''', that is the value of '''ARGC.'''
  
 
|-
 
|-
Line 273: Line 275:
 
|-
 
|-
 
| 06:11
 
| 06:11
| And we are printing '''ARGV''' at '''index i'''
+
| And, we are printing '''ARGV''' at '''index i'''.
  
 
Switch to the '''terminal''' and execute the file.
 
Switch to the '''terminal''' and execute the file.
Line 279: Line 281:
 
|-
 
|-
 
| 06:19
 
| 06:19
| Now type on the '''terminal'''- '''awk space hyphen small f space argc underscore argv dot awk space one space two space three'''
+
| Now, type on the '''terminal'''- '''awk space hyphen small f space argc underscore argv dot awk space one space two space three'''.
  
 
|-
 
|-
 
| 06:35
 
| 06:35
| Here '''one two three''' are the '''command line arguments.'''
+
| Here, '''one two three''' are the '''command line arguments.'''
  
 
Press '''Enter''' to execute the command.
 
Press '''Enter''' to execute the command.
Line 301: Line 303:
 
|-
 
|-
 
| 07:02
 
| 07:02
| Next we have three '''arguments '''that we supplied in the '''command line.'''
+
| Next, we have three '''arguments '''that we supplied in the '''command line.'''
  
 
|-
 
|-
Line 315: Line 317:
 
|-
 
|-
 
| 07:24
 
| 07:24
| The '''array element indices '''are the '''environment variable '''names.
+
| The array element indices are the '''environment variable '''names.
  
 
The '''array element '''values are the values of the particular environment variables.
 
The '''array element '''values are the values of the particular environment variables.
Line 325: Line 327:
 
|-
 
|-
 
| 07:40
 
| 07:40
| First, let us print our '''username'''.
+
| First, let us '''print''' our '''username'''.
  
 
|-
 
|-
Line 333: Line 335:
 
|-
 
|-
 
| 07:48
 
| 07:48
| At the '''command prompt''' type the following.
+
| At the '''command prompt,''' type the following.
  
 
|-
 
|-
 
| 07:53
 
| 07:53
| Press '''Enter'''
+
| Press '''Enter'''.
  
 
|-
 
|-
Line 351: Line 353:
 
|-
 
|-
 
| 08:05
 
| 08:05
| In this tutorial we learnt about- Using''' awk array '''with file
+
| In this tutorial, we learnt about-  
 +
Using''' awk array '''with file,
  
 
|-
 
|-
 
| 08:11
 
| 08:11
|  Scan the '''elements '''of an '''array'''
+
|  Scan the '''elements '''of an '''array''',
  
 
|-
 
|-
 
| 08:14
 
| 08:14
|  '''Delete statement'''
+
|  '''Delete statement''',
  
'''ARGV array '''and '''ENVIRON array'''
+
'''ARGV array '''and '''ENVIRON array'''.
  
 
|-
 
|-
 
| 08:20
 
| 08:20
| As an assignment- Calculate certain allowances for the students who have passed.
+
| As an assignment-  
 +
Calculate certain allowances for the students who have passed.
  
 
|-
 
|-
 
| 08:25
 
| 08:25
|  Paper presentation allowance which is 80% of the stipend  
+
|  Paper presentation allowance which is 80% of the stipend,
  
 
|-
 
|-
 
| 08:30
 
| 08:30
|  Performance incentive which is 20% of the stipend  
+
|  Performance incentive which is 20% of the stipend.
  
 
|-
 
|-
Line 389: Line 393:
 
|-
 
|-
 
| 08:48
 
| 08:48
| The video at the following link summarises the Spoken Tutorial project.
+
| The video at the following link summarises the '''Spoken Tutorial project'''.
  
 
Please download and watch it.
 
Please download and watch it.
Line 397: Line 401:
 
| The '''Spoken Tutorial Project'''&nbsp;team conducts workshops using spoken tutorials.  
 
| The '''Spoken Tutorial Project'''&nbsp;team conducts workshops using spoken tutorials.  
  
And gives certificates on passing online tests.
+
And, gives certificates on passing online tests.
  
 
|-
 
|-
Line 409: Line 413:
 
|-
 
|-
 
| 09:12
 
| 09:12
| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
+
| Spoken Tutorial Project is funded by '''NMEICT, MHRD''', Government of India.
  
 
More information on this mission is available at this link.
 
More information on this mission is available at this link.
Line 417: Line 421:
 
| The script has been contributed by Antara.
 
| The script has been contributed by Antara.
  
And this is Praveen from IIT Bombay signing off.
+
And, this is Praveen from '''IIT Bombay''', signing off.
  
Thank you for joining
+
Thank you for joining.
  
 
|}
 
|}

Revision as of 17:55, 10 July 2019


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