Difference between revisions of "Linux-AWK/C2/Variables-and-Operators-in-awk/English"
Pravin1389 (Talk | contribs) (Created page with "'''Title of script''': Variables and Operators in Awk '''Author: '''Antara Roy Choudhury '''Keywords: '''Linux, awk commands, Variables in awk, operators in awk, String Conc...") |
Nancyvarkey (Talk | contribs) m (Nancyvarkey moved page Linux/C3/Variables-and-Operators-in-awk/English to Linux-AWK/C2/Variables-and-Operators-in-awk/English without leaving a redirect: New series) |
||
(One intermediate revision by the same user not shown) | |||
Line 13: | Line 13: | ||
|- | |- | ||
| style="border-top:1pt solid #000001;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| Slide 1: Introduction | | style="border-top:1pt solid #000001;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| Slide 1: Introduction | ||
− | | style="border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| Hello and welcome to this spoken tutorial on variables and operators in awk command. | + | | style="border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| Hello and welcome to this spoken tutorial on variables and operators in '''awk command'''. |
|- | |- | ||
| style="border-top:1pt solid #000001;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| Slide 2: Learning Objectives | | style="border-top:1pt solid #000001;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| Slide 2: Learning Objectives | ||
| style="border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| In this tutorial we will learn about- | | style="border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| In this tutorial we will learn about- | ||
− | |||
− | |||
* '''User defined variables ''' | * '''User defined variables ''' | ||
* '''Operators ''' | * '''Operators ''' | ||
Line 77: | Line 75: | ||
− | ''' | + | '''Variables '''are case-sensitive. |
Line 136: | Line 134: | ||
|- | |- | ||
| style="border-top:1pt solid #000001;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| a ''<nowiki>[enter]</nowiki>'' | | style="border-top:1pt solid #000001;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| a ''<nowiki>[enter]</nowiki>'' | ||
− | | style="border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| Since we have not given a filename, '''awk '''would need some input from ''' | + | | style="border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0313in;padding-right:0.075in;"| Since we have not given a filename, '''awk '''would need some input from '''standard input'''. |
− | + | ||
− | + | ||
− | + | ||
− | say '''a '''and then press '''Enter.''' | + | And hence, we can type any letter, say '''a '''and then press '''Enter.''' |
|- | |- |
Latest revision as of 16:34, 22 March 2018
Title of script: Variables and Operators in Awk
Author: Antara Roy Choudhury
Keywords: Linux, awk commands, Variables in awk, operators in awk, String Concatenation in awk, Regular expressions in awk, BEGIN and END statement in awk, Spoken Tutorial, Video Tutorial.
|
|
Slide 1: Introduction | Hello and welcome to this spoken tutorial on variables and operators in awk command. |
Slide 2: Learning Objectives | In this tutorial we will learn about-
We will do this through some examples. |
Slide 3: System requirement | To record this tutorial, I am using Ubuntu Linux 16.04 |
Slide 4: Prerequisite | To practice this tutorial, you should have gone through the earlier Linux tutorials on this website.
|
Slide 5: awk | awk combines the power of a filter and a programming language.
|
Slide 6: Variable | Let’s see what a variable in awk is.
|
Slide 7: Variable
|
A variable must begin with a letter and continue with letters, digits and underscores.
|
Let us look at some examples now. | |
Open the terminal by pressing CTRL + ALT and T keys | Open the terminal by pressing CTRL + ALT and T keys. |
Type
print x [enter] print X [enter] print a [enter] print b [enter] print a b print x b print x+X}' [enter]
|
On the terminal, type-
type print x Press Enter print capital X Press Enter print a Press Enter print b Press Enter print a space b Press Enter print x space b Press Enter print small x plus capital X closing curly brace closing single quote and press Enter |
a [enter] | Since we have not given a filename, awk would need some input from standard input.
|
Hover your mouse over x=1; X="A" and a="awk" | This example shows a couple of things.
|
Hover your mouse over the values of small x and capital X | We can see the values of the variables.
|
Hover your mouse over the output of “print a b” | Also, it shows how two strings can be concatenated.
|
Hover your mouse over the output of “print x b”
|
Similarly, when we concatenate small x, which is a number and string b, x is auto-converted into string.
|
Continue to hover your mouse over the output of “print x b” | Why does the auto-conversion to string happen?
|
Hover your mouse over the output of print x+X | Now, look at the output of small x plus capital X.
|
Until now, we have seen a couple of operators.
| |
Slide 8: Operators | A variety of operators can be used in expressions.
|
Same slide but highlight the relevant line item | I am not going to discuss the working of all these operators in detail.
|
A file named awkdemo.txt has been provided in the Code files link.
| |
Switch to terminal
|
Switch to the terminal
|
cd /<saved folder> | Now go to the folder in which you saved the awkdemo.txt file using the cd command. |
Let us have a look at this file now. | |
Show the file opened | Let's say we want to find the students who have passed but have marks less than 80.
|
awk -F "|" '$5=="Pass" && $4<80 {print ++x,$2,$4,$5}' awkdemo.txt
[enter] |
So, on the terminal type
|
Show the output of the previous command
|
This command shows a number of things.
|
Show the awkdemo.txt file and highlight Computers with different case. | Now suppose, we want to find computer science students who have passed.
|
Type:
|
We would type
|
If we want to negate the comparison, we can do so using the exclamation tilde operator. | |
Type:
|
Say now we want a list of all non-computer students who passed.
|
Next, let's count the number of blank lines in the same file. | |
Show the awkdemo.txt file and highlight blank lines | Open the file and check how many blank lines are there are.
|
Type
awk '/^$/ {x=x+1; print x}' awkdemo.txt [enter]
|
Now to count the number of empty lines using awk, type
|
Highlight the output | We get 3 as our final answer. |
Highlight the caret sign (^) >> then the dollar($) sign | The caret sign signifies start of a line while dollar signifies the end of a line. |
Highlight caret-dollar (^$). | Hence an empty line would be matched by the regular expression caret-dollar. |
Highlight x | Note, we have not initialized the value of x.
|
Highlight the output of the command executed | This command gives us the running count of blank lines.
|
Slide 9: BEGIN and END sections | In our last command, we have seen running count of blank lines.
|
Slide 10: BEGIN and END sections | The BEGIN section contains procedures for pre-processing.
post-processing.
|
Type
/^$/ {x=x+1} [press enter] end {print x}' awkdemo.txt[press enter]
|
Let's learn how to do this.
|
Highlight the output | See, we did not get the desired output!
|
Point to end in lowercase
|
What do you think happened?
|
So, let us modify the command. | |
awk 'BEGIN{print "The number of empty lines in awkdemo are"} [press enter]
/^$/ {x=x+1} [press enter] END {print x}' awkdemo.txt [press enter] |
Press up arrow key to get the previous executed command on the terminal.
|
Highlight the output | Now the total number of empty lines is displayed in the output. |
Next, let's find the average salary of all the students that we found in the awkdemo.txt file. | |
Type
awk -F"|" 'BEGIN {printf "%-3s %-25s %10s\n", "Sl","Student","Stipend"} [enter] {x=x+1; total = total+$6; printf"%-3d %-25s %-10d\n",x,$2,$6} [enter] END{avg=total/x; print "The average stipend is" avg}' awkdemo.txt [Enter] |
To get that, type the command as shown in the terminal
|
Slide 11: Summary | This brings us to the end of this tutorial. Let us summarize.
|
Slide 12: Assignment
|
As an assignment print every line where the value of the last field is more than 5000
|
Slide 13: About Spoken Tutorial project | The video at the following link summarises the Spoken Tutorial project.
|
Slide 14: Spoken Tutorial workshops | The Spoken Tutorial Project team conducts workshops using spoken tutorials
|
Slide 15: Forum for specific questions: | Do you have questions in THIS Spoken Tutorial?
|
Slide 16: Forum for specific questions: | The Spoken Tutorial forum is for specific questions on this tutorial.
Please do not post unrelated and general questions on them.
|
Slide 17: Acknowledgement | Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
this link. |
Slide 18: Thank You | The script has been contributed by Antara and this is Praveen from IIT Bombay signing off.
|