Linux-AWK/C2/Conditional-statements-in-awk/English-timed

From Script | Spoken-Tutorial
Revision as of 12:39, 20 June 2019 by PoojaMoolya (Talk | contribs)

Jump to: navigation, search
Time
Narration
00:01 Hello and Welcome to this spoken tutorial on conditional statements in awk.
00:07 In this tutorial we will learn about- if, else, else if in awk
00:15 We will do this through some examples.
00:19 To record this tutorial, I am using Ubuntu Linux 16.04 Operating System and gedit text editor 3.20.1
00:32 You can use any text editor of your choice.
00:36 To practice this tutorial, you should have gone through the previous awk tutorials on our website.
00:43 You should have familiarity with any programming language like C or C++
00:50 If not, then please go through the corresponding tutorials on our website.
00:56 The files used in this tutorial are available in the Code Files link on this tutorial page.

Please download and extract them.

01:06 A conditional statement allows us to check a specified condition before performing an action.
01:14 Let’s learn how conditional statements like if, else, else-if work in awk.
01:22 Like any programming language, the syntax of if -else statement is:
01:28 if conditional-expression1 is true then perform action1.
01:34 else if conditional-expression2 is true then perform action 2.
01:41 A number of else if statements may follow after this.
01:46 At the end, if none of the specified conditional expressions are true, then perform action n.
01:54 The else and else-if portions are optional.

Let us go through an example.

02:02 We will use the same awkdemo.txt file, that we have used earlier.
02:10 Say, we need to give 50% increment in stipend to the students getting more than 8000 rupees.
02:19 Let us create an awk file for this condition.
02:23 Type the following code as shown in a text editor and save it as cond dot awk

I have already done this.

02:34 The same file is also available in the Code Files link.
02:39 In this code, we have set the Output Field Separator as colon.
02:45 The first print statement prints the field headings.
02:50 Next, the if statement will check whether the value of the 6th field is greater than 8000.
02:58 If yes, the second print statement will get executed.
03:03 $6 into 1.5 inside this print statement will multiply the 6th field value by 1.5%
03:13 Let’s execute this code now.
03:16 Open the terminal by pressing CTRL, ALT and T keys.
03:22 Go to the folder in which you downloaded and extracted the Code Files using cd command
03:29 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.

03:49 The output shows only one student’s record with the incremented stipend who fulfilled the condition.
03:57 Now, suppose the rule changes: 50% increment in stipend for students who get more than 8000 rupees.
04:07 Otherwise give 30% increment.

How can we do that?

04:13 We need to add an else block.
04:16 Switch to the cond dot awk file once again.
04:21 Let us add the following line of code.

Before the last closing curly brace press Enter.

04:30 else press Enter.
04:33 print space dollar 2 comma dollar 6 comma dollar 6 into 1.3
04:42 Save the file and switch to the terminal.
04:46 Press up arrow key to get the previously executed command

and press Enter.

04:53 Observe the output now.

Yojna Chaudhury was previously getting 1000. Now she is getting 1300.

05:04 Let’s change the rules again.

50% increment for student getting more than 8000 rupees

05:13 40% increment for student getting more than 4000 rupees

Otherwise give 30% increment

05:23 Switch to the code. Update the code as shown.
05:29 Save the file and switch to the terminal.
05:33 Let me clear the terminal.
05:36 Now press up arrow key to get the previously executed command

and press Enter.

05:44 This time, notice that the student Mira Nair has got 40% increment.
05:51 This brings us to the end of this tutorial.
05:54 Let us summarize. In this tutorial we learnt about Conditional statements like:

if , else, else if in awk

06:05 As an assignment give grades according to the rules: If mark is greater than equal to 90, grade will be A.
06:15 If mark is greater than equal to 80 but less than 90, grade will be B.
06:23 If mark is greater than equal to 70 but less than 80, grade will be C.
06:30 If mark is greater than equal to 60 but less than 70, grade will be D.

Otherwise grade will be F.

06:41 The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

06:49 The Spoken Tutorial Project team conducts workshops using spoken tutorials

and gives certificates on passing online tests.

06:58 For more details, please write to us.
07:02 Do you have questions in THIS Spoken Tutorial?

Please visit this site

07:08 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

07:20 The script has been contributed by Antara. And this is Praveen from IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14