Difference between revisions of "Linux-AWK/C2/Basics-of-Single-Dimensional-Array-in-awk/English"
(Created page with "'''Title of script: Basics of single Dimensional Array in Awk''' '''Author: Antara Roy Choudhury''' '''Keywords: Array, assigning array element, referring array element, che...") |
Nancyvarkey (Talk | contribs) |
||
Line 17: | Line 17: | ||
| 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- | ||
− | |||
* '''Arrays''' in '''awk''' | * '''Arrays''' in '''awk''' | ||
Line 32: | 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 64: | Line 63: | ||
* '''Arrays '''in '''awk '''are '''''associative'''''. | * '''Arrays '''in '''awk '''are '''''associative'''''. | ||
* This means that each '''array element '''is an '''index-value pair.''' | * This means that each '''array element '''is an '''index-value pair.''' | ||
− | |||
− | |||
|- | |- | ||
Line 77: | Line 74: | ||
* First, we do not need to '''declare '''an '''array''' before using it. | * First, we do not need to '''declare '''an '''array''' before using it. | ||
* Also there is no need to specify how many '''elements''' the '''array '''will contain. | * Also there is no need to specify how many '''elements''' the '''array '''will contain. | ||
− | |||
− | |||
|- | |- | ||
| 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;"| Slide 7 | | 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;"| Slide 7 | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * In programming languages, '''array index '''is generally a positive integer. | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| |
+ | * In programming languages, '''array index '''is generally a positive integer. | ||
* Usually the '''index''' starts from 0, then 1, then 2 and so on. | * Usually the '''index''' starts from 0, then 1, then 2 and so on. | ||
− | |||
− | |||
|- | |- | ||
| 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;"| Slide 8 | | 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;"| Slide 8 | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| But in '''awk''', the '''index '''can be anything – any '''number '''or a '''string.''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 108: | Line 100: | ||
|- | |- | ||
| 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;"| Slide 10 | | 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;"| Slide 10 | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Strings''' have to be written inside double quotes, whether it is | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Strings''' have to be written inside double quotes, whether it is '''index '''name or a '''value'''. |
Line 134: | Line 126: | ||
|- | |- | ||
| 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;"| <nowiki>Highlight day[“first”] = “Sunday”</nowiki> | | 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;"| <nowiki>Highlight day[“first”] = “Sunday”</nowiki> | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In | + | | 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 '''array element''', I have used a '''string '''as the '''index.''' |
Line 157: | Line 149: | ||
I have declared '''day four '''before '''day three'''. | I have declared '''day four '''before '''day 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;"| Display slide 11 | | 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 11 | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * In '''awk arrays, index '''need not to be in a sequential manner. | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| |
+ | * In '''awk arrays, index '''need not to be in a sequential manner. | ||
* Advantage of '''associative array''' is that new pairs can be added at any time. | * Advantage of '''associative array''' is that new pairs can be added at any time. | ||
− | |||
− | |||
|- | |- | ||
Line 182: | Line 170: | ||
Then type the following | Then type the following | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 201: | Line 186: | ||
− | Let us try this. | + | Let us try this. |
|- | |- | ||
Line 222: | Line 207: | ||
|- | |- | ||
| 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;"| | | 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;"| | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Open the '''terminal''' by pressing '''Ctrl''', '''Alt''' and '''T''' keys | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Open the '''terminal''' by pressing '''Ctrl''', '''Alt''' and '''T''' keys. |
|- | |- | ||
Line 294: | Line 279: | ||
print "Index7 is present." | print "Index7 is present." | ||
− | |||
− | |||
− | |||
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now I have added two '''if conditions.''' | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now I have added two '''if conditions.''' | ||
Line 362: | Line 344: | ||
| 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;"| Highlight '''"Index 7 is present after null comparison."''' | | 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;"| Highlight '''"Index 7 is present after null comparison."''' | ||
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next, we’ll change the '''print statement '''of the '''condition 7 '''in '''day.''' | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next, we’ll change the '''print statement '''of the '''condition 7 '''in '''day.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| 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;"| Save the code | | 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;"| Save the code | ||
− | |||
− | |||
− | |||
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save the code. | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save the code. | ||
Line 380: | Line 356: | ||
− | + | Press Up arrow key | |
Line 388: | Line 364: | ||
Press the up arrow key to get the previous command. | Press the up arrow key to get the previous command. | ||
− | |||
Press '''Enter''' to execute. | Press '''Enter''' to execute. | ||
Line 453: | Line 428: | ||
* How it is different from '''arrays''' in other programming languages | * How it is different from '''arrays''' in other programming languages | ||
* Refer the '''elements '''of an '''array ''' | * Refer the '''elements '''of an '''array ''' | ||
− | |||
− | |||
|- | |- | ||
Line 468: | Line 441: | ||
* Index will be the names of the flowers | * Index will be the names of the flowers | ||
* Value will be the corresponding color of the flowers | * Value will be the corresponding color of the flowers | ||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 481: | Line 449: | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * Insert entries for any five flowers of your choice | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| |
+ | * Insert entries for any five flowers of your choice | ||
* Print the color of the fourth flower | * Print the color of the fourth flower | ||
* Check if the flower “Lotus” is present in the '''array''' | * Check if the flower “Lotus” is present in the '''array''' | ||
− | |||
− | |||
|- | |- | ||
Line 521: | Line 488: | ||
− | More information on this mission is available at | + | More information on this mission is available at this link. |
− | + | ||
− | this link. | + | |
|- | |- |
Revision as of 10:38, 16 February 2018
Title of script: Basics of single Dimensional Array in Awk
Author: Antara Roy Choudhury
Keywords: Array, assigning array element, referring array element, checking for presence of an element
|
|
Display Slide 1 | Welcome to this spoken tutorial on Basics of 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
pre-requisite |
To practice this tutorial, you should have gone through previous awk 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.
|
Display Slide 5 | What is an array in awk?
|
Display Slide 6 | It looks very similar to arrays in any other programming language.
|
Slide 7 |
|
Slide 8 | But in awk, the index can be anything – any number or a string. |
Slide 9:
|
This is the syntax of assigning an array element in awk.
|
Slide 10 | Strings have to be written inside double quotes, whether it is index name or a value.
|
Show array_intro.awk in gedit | I have already written the code and saved it as array_intro.awk
Please download and use it. |
Highlight day[1] = “Sunday” | Here I have taken weekdays as an example and written it inside the BEGIN section.
|
Highlight day[“first”] = “Sunday” | In this array element, I have used a string as the index.
|
Highlight the portion
day["fourth"]="wednesday" day[3]="tuesday" day["third"]="tuesday" |
Notice here, the array elements are not in a sequence.
|
Display slide 11 |
|
Switch to gedit and Type:
day["sixth"]="Friday" |
Let me add day 6 in the array.
|
Press Ctrl+S | Save the file. |
Display slide 12 | We have declared the array.
|
Switch to gedit
Type: print day[6] |
Switch to the code once again.
Place the cursor in front of the closing curly brace.
|
Press Ctrl+S | Save the code. |
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 array_intro.awk [Enter] |
Now type
awk space hyphen small f space array_intro.awk Press Enter |
Show the output | See, we get Friday as the output. |
Display slide 12 | Next we will check whether any element exists in an array at a certain index.
|
Switch to gedit
Type: if (2 in day) [Enter] print "Index 2 is present." [Enter] if (7 in day) [Enter] print "Index7 is present." |
Switch to the code in the editor window.
|
Highlight Both the if statements
if (7 in day) print "Index7 is present." |
Now I have added two if conditions.
|
Switch to terminal | Switch to the terminal.
Press the Up arrow key to get back the previously executed command.
|
Highlight Index 2 is present. in the output | We get the output as expected. |
In gedit, type
if (day[7] != "") print "Index 7 is not null"
print "Index 7 is present after null comparison." |
We will now make some more changes to the code.
|
Highlight if (day[7] != "") | Below the 7 in day condition, I have added one more condition.
|
Highlight "Index 7 is present after null comparison." | Next, we’ll change the print statement of the condition 7 in day. |
Save the code | Save the code.
|
Switch to terminal
|
Switch to terminal.
Press Enter to execute. |
Highlight "Index 7 is present after null comparison." | We got an unexpected output.
is printed.
|
Switch to gedit window
Highlight if (day[7] != "") |
When we write, day[7] not equal to null, we are trying to access the element at index 7.
|
Highlight
if (7 in day) |
Next, we are trying to check if any element is actually present at index 7.
|
Display slide 13 | So, remember this.
|
Display slide 14 | Instead, we have to use the in operator.
This brings us to the end of this tutorial. |
Display Slide 17
Summary |
In this tutorial we learnt about-
|
Display Slide 18
Assignment
|
As an assignment-
|
Display Slide 19
Assignment(Cont.)
|
|
Display Slide 21
About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project.
|
Display Slide 22
Spoken Tutorial workshops |
The Spoken Tutorial Project team conducts workshops using spoken tutorials.
|
Display Slide 23
Forum for specific questions: |
Please post your timed queries in this forum. |
Display Slide 25
Acknowledgement |
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
|
The script has been contributed by Antara.
Thanks for joining |