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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 5: Line 5:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Hello and Welcome to this spoken tutorial on '''conditional statements''' in '''awk'''.
+
| Hello and welcome to this '''spoken tutorial''' on '''conditional statements in awk'''.
  
 
|-
 
|-
 
| 00:07
 
| 00:07
| In this tutorial we will learn about-  '''if''',  '''else''',  '''else if '''in''' awk'''
+
| In this tutorial, we will learn about-  '''if''',  '''else''',  '''else if '''in''' awk'''.
  
 
|-
 
|-
Line 17: Line 17:
 
|-
 
|-
 
| 00:19
 
| 00:19
| To record this tutorial, I am using  '''Ubuntu Linux 16.04 Operating System '''and '''gedit text editor 3.20.1'''
+
| To record this tutorial, I am using:  
 +
'''Ubuntu Linux 16.04 Operating System '''and '''gedit text editor 3.20.1'''
  
 
|-
 
|-
Line 29: Line 30:
 
|-
 
|-
 
| 00:43
 
| 00:43
| You should have familiarity with any '''programming language''' like C or C++  
+
| You should have familiarity with any '''programming language''' like C or C++.
  
 
|-
 
|-
Line 39: Line 40:
 
| The files used in this tutorial are available in the '''Code Files''' link on this tutorial page.
 
| The files used in this tutorial are available in the '''Code Files''' link on this tutorial page.
  
Please download and extract them.
+
Please download and '''extract''' them.
  
 
|-
 
|-
Line 51: Line 52:
 
|-
 
|-
 
| 01:22
 
| 01:22
| Like any programming language, the syntax of '''if -else statement '''is:
+
| Like any programming language, the syntax of ''''if -else' statement '''is:
  
 
|-
 
|-
Line 67: Line 68:
 
|-
 
|-
 
| 01:46
 
| 01:46
| At the end, if none of the specified '''conditional expressions '''are '''true''', then perform '''action n.'''
+
| At the end, if none of the specified '''conditional expressions '''are '''true''' then perform '''action n.'''
  
 
|-
 
|-
Line 77: Line 78:
 
|-
 
|-
 
| 02:02
 
| 02:02
| We will use the same '''awkdemo.txt '''file, that we have used earlier.  
+
| We will use the same '''awkdemo.txt '''file that we have used earlier.  
  
 
|-
 
|-
 
| 02:10
 
| 02:10
| Say, we need to give 50% increment''' '''in stipend to the students getting more than 8000 rupees.  
+
| Say, we need to give '''50% increment '''in stipend to the students getting more than 8000 rupees.  
  
 
|-
 
|-
Line 89: Line 90:
 
|-
 
|-
 
| 02:23
 
| 02:23
| Type the following code as shown in a text editor and save it as '''cond''' '''dot''' '''awk'''
+
| Type the following '''code''' as shown, in a text editor and '''save''' it as '''cond dot awk'''.
  
 
I have already done this.
 
I have already done this.
Line 103: Line 104:
 
|-
 
|-
 
| 02:45
 
| 02:45
| The first '''print statement '''prints the '''field headings.'''
+
| The first '''print statement '''prints the '''field''' headings.
  
 
|-
 
|-
 
| 02:50
 
| 02:50
| Next, the '''if statement''' will check whether the value of the 6th '''field''' is greater than 8000.
+
| Next, the ''''if' statement''' will check whether the value of the 6th '''field''' is greater than 8000.
  
 
|-
 
|-
Line 115: Line 116:
 
|-
 
|-
 
| 03:03
 
| 03:03
| '''$6 into 1.5''' inside this '''print statement''' will multiply the 6th '''field''' value by 1.5%  
+
| '''$6 into 1.5''' inside this '''print statement''' will multiply the 6th '''field''' value by 1.5% .
  
 
|-
 
|-
 
| 03:13
 
| 03:13
| Let’s execute this code now.
+
| Let’s '''execute''' this code now.
  
 
|-
 
|-
Line 127: Line 128:
 
|-
 
|-
 
| 03:22
 
| 03:22
| Go to the folder in which you downloaded and extracted the '''Code Files''' using '''cd command'''
+
| Go to the folder in which you downloaded and extracted the '''Code Files''' using ''''cd' command'''.
  
 
|-
 
|-
 
|03:29
 
|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'''
+
| 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.'''
 
Press '''Enter.'''
Line 137: Line 138:
 
|-
 
|-
 
| 03:49
 
| 03:49
| The output shows only one student’s record with the incremented stipend who fulfilled the '''condition'''.
+
| The output shows only one student’s record with the incremented stipend who fulfilled the condition.
  
 
|-
 
|-
Line 145: Line 146:
 
|-
 
|-
 
| 04:07
 
| 04:07
|  Otherwise give 30% increment.
+
|  Otherwise, give 30% increment.
  
 
How can we do that?
 
How can we do that?
Line 151: Line 152:
 
|-
 
|-
 
| 04:13
 
| 04:13
| We need to add an '''else block.'''
+
| We need to add an ''''else' block.'''
  
 
|-
 
|-
Line 173: Line 174:
 
|-
 
|-
 
| 04:42
 
| 04:42
| Save the file and switch to the '''terminal.'''
+
| '''Save''' the file and switch to the '''terminal.'''
  
 
|-
 
|-
 
| 04:46
 
| 04:46
| Press up arrow key to get the previously executed command  
+
| Press the up-arrow key to get the previously executed command and press '''Enter.'''
 
+
and press '''Enter.'''
+
  
 
|-
 
|-
Line 191: Line 190:
 
| Let’s change the rules again.
 
| Let’s change the rules again.
  
50% increment for student getting more than 8000 rupees
+
50% increment for student getting more than 8000 rupees.
  
 
|-
 
|-
 
| 05:13
 
| 05:13
| 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.
  
 
|-
 
|-
 
| 05:23
 
| 05:23
| Switch to the code. Update the code as shown.
+
| Switch to the '''code'''. Update the code as shown.
  
 
|-
 
|-
 
| 05:29
 
| 05:29
| Save the file and switch to the '''terminal.'''
+
| '''Save''' the file and switch to the '''terminal.'''
  
 
|-
 
|-
Line 213: Line 212:
 
|-
 
|-
 
| 05:36
 
| 05:36
| Now press up arrow key to get the previously executed command  
+
| Now, press up-arrow key to get the previously executed command and press '''Enter.'''
 
+
and press '''Enter.'''
+
  
 
|-
 
|-
Line 227: Line 224:
 
|-
 
|-
 
| 05:54
 
| 05:54
| Let us summarize. In this tutorial we learnt about '''Conditional statements '''like:
+
| Let us summarize. In this tutorial, we learnt about '''Conditional statements '''like:
  
'''if''' ,  '''else''', '''else if '''in''' awk'''
+
'''if''' ,  '''else''', '''else if '''in''' awk'''.
  
 
|-
 
|-
 
| 06:05
 
| 06:05
| As an assignment give grades according to the rules: If mark is greater than equal to 90, grade will be A.
+
| As an assignment, give grades according to the rules:  
 +
If mark is greater than equal to 90, grade will be A.
  
 
|-
 
|-
Line 247: Line 245:
 
|  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.
  
 
|-
 
|-
 
|06:41
 
|06:41
| The video at the following link summarises the Spoken Tutorial project.
+
| The video at the following link summarises the '''Spoken Tutorial''' project.
  
 
Please download and watch it.
 
Please download and watch it.
Line 257: Line 255:
 
|-
 
|-
 
| 06:49
 
| 06:49
| The Spoken Tutorial Project team conducts workshops using spoken tutorials  
+
| The Spoken Tutorial Project team conducts workshops using spoken tutorials.
 
+
And, gives certificates on passing online tests.
and gives certificates on passing online tests.
+
  
 
|-
 
|-
Line 269: Line 266:
 
| Do you have questions in THIS Spoken Tutorial?
 
| Do you have questions in THIS Spoken Tutorial?
  
Please visit this site
+
Please visit this site.
  
 
|-
 
|-
 
| 07:08
 
| 07:08
| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
+
| 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.
Line 279: Line 276:
 
|-
 
|-
 
| 07:20
 
| 07:20
| The script has been contributed by Antara. And this is Praveen from IIT Bombay signing off.
+
| The script has been contributed by Antara. And this is Praveen from '''IIT Bombay''', signing off.
  
 
Thanks for joining.
 
Thanks for joining.
  
 
|}
 
|}

Revision as of 11:55, 10 July 2019

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 the 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