Linux-AWK/C2/More-on-Single-Dimensional-Array-in-awk/English
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 | * So, any array element can be deleted using the delete command.
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,
|
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.
and the average. |
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.
this link. |
The script has been contributed by Antara.
Thank you for joining |