Linux-AWK/C2/Conditional-statements-in-awk/English
Title of script: Conditional statements in Awk
Author: Antara Roy Choudhury
Keywords: if, else, else if
|
|
Slide 1: Introduction | Welcome to this spoken tutorial on conditional statements in awk. |
Slide 2: Learning Objective | 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
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.
|
Slide 5: Code Files | The files used in this tutorial are available in the Code Files link on this tutorial page.
|
Slide 6: Conditional Statement | A conditional statement allows us to check a specified condition before performing an action.
|
Slide 7: if-else Statement | Like any programming language, the syntax of if -else statement is:
|
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.
|
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 file.
|
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*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
[Enter] |
Now type:
|
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.
|
show cond.awk | Switch to the cond.awk file once again. |
Type:
[Enter]
|
Let us add the following line of code.
|
else print $2,$6,$6*1.3 | else press Enter.
|
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
Yojna chaudhury:1000:1300 |
Observe the output now.
|
Slide 10: Example 3 | Now let’s change the rules again.
40% increment for student getting more than 4000 rupees, Otherwise give 30% increment |
In cond.awk
else if($6>4000) print $2,$6,$6*1.4 else print $2,$6,$6*1.3} |
Switch to the code.
|
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.
|
Slide 12: Assignment
|
As an assignment give grades according to the rules:
|
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?
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.
|