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

From Script | Spoken-Tutorial
Jump to: navigation, search
m (Nancyvarkey moved page Linux/C3/Conditional-statements-in-awk/English to Linux-AWK/C2/Conditional-statements-in-awk/English without leaving a redirect: New series)
 
(No difference)

Latest revision as of 16:35, 22 March 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