Difference between revisions of "Linux-AWK/C2/Conditional-statements-in-awk/English"
(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-...") |
Nancyvarkey (Talk | contribs) 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) |
||
(One intermediate revision by the same user not shown) | |||
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 | + | | 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 | + | | 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 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. | |
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 | + | | 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. |
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
|
|
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 |
|
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
|
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
[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:
|
show cond.awk | Switch to the cond dot 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.
|
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.
In this tutorial we learnt about Conditional statements like:
|
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.
|