Difference between revisions of "Linux-AWK/C2/Conditional-statements-in-awk/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script: Conditional statements in Awk''' '''Author: Antara Roy Choudhury''' '''Keywords: if, else, else if''' {| style="border-spacing:0;" | style="background-...")
 
Line 28: Line 28:
 
| 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 OS '''and''' '''
+
* '''Ubuntu Linux 16.04 OS '''and  
 
* '''gedit text editor 3.20.1'''
 
* '''gedit text editor 3.20.1'''
  
Line 35: Line 35:
 
|-
 
|-
 
| 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 4: Pre-requisite
 
| 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 4: Pre-requisite
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * To practice this tutorial, you should have gone through the previous '''awk''' tutorials on our website.
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
 +
* To practice this tutorial, you should have gone through the previous '''awk''' tutorials on our website.
 
* You should have familiarity with any '''programming language''' like C or C++  
 
* You should have familiarity with any '''programming language''' like C or C++  
 
* If not, then please go through the corresponding tutorials on our website.
 
* If not, then please go through the corresponding tutorials on our website.
 
 
  
 
|-
 
|-
Line 90: Line 89:
 
|-
 
|-
 
| 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;"| Type: BEGIN{OFS=":"; print "Name:Old stipend:New Stipend"} {if($6>8000) print $2, $6,$6*1.5}
 
| 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;"| Type: BEGIN{OFS=":"; print "Name:Old stipend:New Stipend"} {if($6>8000) print $2, $6,$6*1.5}
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type the following code as shown in a text editor and save it as '''cond''' '''dot''' '''awk file.'''
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type the following code as shown in a text editor and save it as '''cond''' '''dot''' '''awk'''
  
  
Line 116: Line 115:
 
|-
 
|-
 
| 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 $6*1.5
 
| 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 $6*1.5
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| $6*1.5 inside this '''print statement''' will multiply the 6th '''field''' value by 1.5%  
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''$6 into 1.5''' inside this '''print statement''' will multiply the 6th '''field''' value by 1.5%  
  
 
|-
 
|-
Line 140: Line 139:
  
  
'''awk space hyphen capital F pipe symbol within double quotes space hyphen small f space cond.awk space awkdemo.txt'''
+
'''awk space hyphen capital F pipe symbol within double quotes space hyphen small f space cond dot awk space awkdemo dot txt'''
  
  
Line 153: Line 152:
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now, suppose the rule changes:
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now, suppose the rule changes:
  
50% increment in stipend for students who get more than 8000 rupees,
+
* 50% increment in stipend for students who get more than 8000 rupees.
  
otherwise give 30% increment.
+
* Otherwise give 30% increment.
  
  
Line 165: Line 164:
 
|-
 
|-
 
| 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;"| show cond.awk
 
| 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;"| show cond.awk
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to the '''cond.awk''' file once again.
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to the '''cond dot awk''' file once again.
  
 
|-
 
|-
Line 171: Line 170:
  
 
<nowiki>[Enter]</nowiki>
 
<nowiki>[Enter]</nowiki>
 
 
 
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us add the following line of code.
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us add the following line of code.
  
Line 218: Line 214:
  
  
50% increment for student getting more than 8000 rupees,
+
*50% increment for student getting more than 8000 rupees
  
40% increment for student getting more than 4000 rupees,
+
*40% increment for student getting more than 4000 rupees
  
Otherwise give 30% increment
+
*Otherwise give 30% increment
  
 
|-
 
|-
Line 277: Line 273:
  
 
Let us summarize.
 
Let us summarize.
 
  
 
In this tutorial we learnt about '''Conditional statements '''like:
 
In this tutorial we learnt about '''Conditional statements '''like:
 
 
* '''if'''
 
* '''if'''
 
* '''else'''
 
* '''else'''
 
* '''else if '''in''' awk'''
 
* '''else if '''in''' awk'''
 
 
  
 
|-
 
|-
Line 293: Line 285:
  
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an assignment give grades according to the rules:
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an assignment give grades according to the rules:
 
  
 
* If mark is greater than equal to 90, grade will be A.
 
* If mark is greater than equal to 90, grade will be A.
Line 300: Line 291:
 
* If mark is greater than equal to 60 but less than 70, grade will be D.
 
* If mark is greater than equal to 60 but less than 70, grade will be D.
 
* Otherwise grade will be F.
 
* Otherwise grade will be F.
 
 
  
 
|-
 
|-
Line 329: Line 318:
 
Forum for specific questions:
 
Forum for specific questions:
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Do you have questions in THIS Spoken Tutorial?
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Do you have questions in THIS Spoken Tutorial?
 
*
 
  
 
Please visit this site
 
Please visit this site
Line 337: Line 324:
 
| 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 16: Acknowledgement
 
| 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 16: Acknowledgement
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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.

Revision as of 18:38, 31 January 2018

Title of script: Conditional statements in Awk

Author: Antara Roy Choudhury

Keywords: if, else, else if


Visual Cue
Narration
Slide 1: Introduction Welcome to this spoken tutorial on conditional statements in awk.
Slide 2: Learning Objective In this tutorial we will learn about-
  • if
  • else
  • else if in awk

We will do this through some examples.

Slide 3: System requirement To record this tutorial, I am using
  • Ubuntu Linux 16.04 OS and
  • gedit text editor 3.20.1

You can use any text editor of your choice.

Slide 4: Pre-requisite
  • To practice this tutorial, you should have gone through the previous awk tutorials on our website.
  • You should have familiarity with any programming language like C or C++
  • If not, then please go through the corresponding 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.


Please download and extract them.

Slide 6: Conditional Statement A conditional statement allows us to check a specified condition before performing an action.


Let’s learn how conditional statements like if, else, else-if work in awk.

Slide 7: if-else Statement Like any programming language, the syntax of if -else statement is:


if conditional-expression1 is true then perform action1.


else if conditional-expression2 is true then perform action 2.


A number of else if statements may follow after this.


At the end, if none of the specified conditional expressions are true, then perform action n.


The else and else-if portions are optional.


Let us go through an example.

Show awkdemo.txt file in gedit We will use the same awkdemo.txt file, that we have used earlier.
Slide 8: Example 1 Say, we need to give 50% increment in stipend to the students getting more than 8000 rupees.


Let us create an awk file for this condition.

Type: BEGIN{OFS=":"; print "Name:Old stipend:New Stipend"} {if($6>8000) print $2, $6,$6*1.5} Type the following code as shown in a text editor and save it as cond dot awk


I have already done this.


The same file is also available in the Code Files link.

Highlight OFS=":" In this code, we have set the Output Field Separator as colon.
Highlight 1st print statement The first print statement prints the field headings.
Highlight if statement Next, the if statement will check whether the value of the 6th field is greater than 8000.
Highlight 2nd print statement If yes, the second print statement will get executed.
Highlight $6*1.5 $6 into 1.5 inside this print statement will multiply the 6th field value by 1.5%
Let’s execute this code now.
Open the terminal 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
In Terminal


awk -F"|" -f cond.awk awkdemo.txt

[Enter]

Now type:


awk space hyphen capital F pipe symbol within double quotes space hyphen small f space cond dot awk space awkdemo dot txt


Press Enter.

Show the output The output shows only one student’s record with the incremented stipend who fulfilled the condition.
Slide 9: Example 2 Now, suppose the rule changes:
  • 50% increment in stipend for students who get more than 8000 rupees.
  • Otherwise give 30% increment.


How can we do that?


We need to add an else block.

show cond.awk Switch to the cond dot awk file once again.
Type:

[Enter]

Let us add the following line of code.


Before the last closing curly brace press Enter.

else print $2,$6,$6*1.3 else press Enter.


print dollar 2 comma dollar 6 comma dollar 6 astrix 1.3

Press Ctrl+S Save the file and switch to the terminal.
In terminal Press up key

[Enter]

Press up key to get the previously executed command

and press Enter.

Show the output


Highlight

Yojna chaudhury:1000:1300

Observe the output now.


Yojna Chaudhury was previously getting 1000.


Now she is getting 1300.

Slide 10: Example 3 Now let’s change the rules again.


  • 50% increment for student getting more than 8000 rupees
  • 40% increment for student getting more than 4000 rupees
  • Otherwise give 30% increment
In cond.awk


Type the highlighted portion


BEGIN{OFS=":";print "Name:Old stipend:New Stipend"} {if($6>8000)


print $2, $6,$6*1.5 [Enter]

else if($6>4000)

print $2,$6,$6*1.4

else

print $2,$6,$6*1.3}

Switch to the code.


Update the code as shown.



Press Ctrl+S Save the file and switch to the terminal.
Let me clear the terminal.
In terminal Press up key

[Enter]

Press up key to get the previously executed command

and press Enter.

Show the output and highlight appropriately This time, notice that the student Mira Nair has got 40% increment.
Slide 11: Summary This brings us to the end of this tutorial.


Let us summarize.

In this tutorial we learnt about Conditional statements like:

  • if
  • else
  • else if in awk
Slide 12: Assignment


As an assignment give grades according to the rules:
  • If mark is greater than equal to 90, grade will be A.
  • If mark is greater than equal to 80 but less than 90, grade will be B.
  • If mark is greater than equal to 70 but less than 80, grade will be C.
  • If mark is greater than equal to 60 but less than 70, grade will be D.
  • Otherwise grade will be F.
Slide 13:

About Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.


Please download and watch it.

Slide 14:

Spoken Tutorial workshops

The Spoken Tutorial Project team conducts workshops using spoken tutorials


and gives certificates on passing online tests.


For more details, please write to us.

Slide 15:

Forum for specific questions:

Do you have questions in THIS Spoken Tutorial?

Please visit this site

Slide 16: Acknowledgement Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

Slide 17: Thanks The script has been contributed by Antara.


And this is Praveen from IIT Bombay signing off.


Thanks for joining.

Contributors and Content Editors

Antarade, Nancyvarkey