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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "{|border=1 | <center>'''Time'''</center> | <center>'''Narration'''</center> |- |00:01 | Hello and Welcome to this spoken tutorial on '''Loops''' in '''awk'''. |- | 00:06 | I...")
 
Line 5: Line 5:
 
|-
 
|-
 
|00:01
 
|00:01
| Hello and Welcome to this spoken tutorial on '''Loops''' in '''awk'''.
+
| Hello and welcome to this '''spoken tutorial''' on '''Loops in awk'''.
  
 
|-
 
|-
 
| 00:06
 
| 00:06
| In this tutorial we will learn about- '''while'''
+
| In this tutorial, we will learn about-  
 
+
'''while''', '''do-while''', '''for ''' and more '''looping constructs '''in''' awk'''.
'''do-while''', '''for ''' and more '''looping constructs '''in''' awk'''
+
  
 
|-
 
|-
Line 19: Line 18:
 
|-
 
|-
 
| 00:20
 
| 00:20
| To record this tutorial, I am using  
+
| To record this tutorial, I am using:
  
'''Ubuntu Linux 16.04 Operating System '''and''' ''' '''gedit text editor 3.20.1'''
+
'''Ubuntu Linux 16.04 Operating System '''and
 +
'''gedit text editor 3.20.1'''.
  
 
|-
 
|-
Line 33: Line 33:
 
|-
 
|-
 
| 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 43: Line 43:
 
| 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 55: Line 55:
 
|-
 
|-
 
|01:18
 
|01:18
| The syntax of '''while''' '''loop''' can be seen here.
+
| The syntax of ''''while' loop''' can be seen here.
  
 
|-
 
|-
 
| 01:22
 
| 01:22
|'''While loop '''first checks whether the specified '''condition''' is true.  
+
|'''While loop '''first checks whether the specified '''condition''' is '''true'''.  
  
 
|-
 
|-
 
| 01:27
 
| 01:27
|If yes, then it executes the code within the '''body.'''
+
|If yes, then it '''execute'''s the '''code''' within the '''body.'''
  
 
This '''loop '''will be repeated as long as the specified '''while condition '''is '''true'''.
 
This '''loop '''will be repeated as long as the specified '''while condition '''is '''true'''.
Line 69: Line 69:
 
|-
 
|-
 
| 01:37
 
| 01:37
| 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.  
  
 
|-
 
|-
 
| 01:43
 
| 01:43
| I have already written a script named '''while_loop.awk'''
+
| I have already written a '''script''' named '''while_loop.awk'''.
  
 
|-
 
|-
Line 81: Line 81:
 
|-
 
|-
 
| 01:53
 
| 01:53
| Here we have set the '''field separator''' as '''Pipe symbol.'''
+
| Here, we have set the '''field separator''' as '''Pipe''' symbol.
  
 
|-
 
|-
 
| 01:58
 
| 01:58
| Initially, we have to the set the value of the '''loop variable i '''as 1.  
+
| Initially, we have to set the value of the '''loop variable i '''as 1.  
  
 
|-
 
|-
Line 93: Line 93:
 
|-
 
|-
 
| 02:10
 
| 02:10
|'''Variable f''' represents the '''field counter '''or the position of the '''fields''' for each '''record'''.
+
|'''Variable f''' represents the '''field counter '''or the position of the '''field'''s for each '''record'''.
  
 
|-
 
|-
Line 101: Line 101:
 
|-
 
|-
 
| 02:23
 
| 02:23
| If yes, then it will print the value in the '''f'''<sup>th</sup>''' field, '''for that record in '''awkdemo.txt '''file.
+
| If yes, then it will '''print''' the value in the '''f'''<sup>th</sup>''' field, '''for that '''record''' in '''awkdemo.txt '''file.
  
 
|-
 
|-
Line 121: Line 121:
 
|-
 
|-
 
| 02:55
 
| 02:55
|Which means the first 3 fields will get printed for each record.
+
|Which means, the first 3 fields will get printed for each record.
  
 
|-
 
|-
Line 133: Line 133:
 
|-
 
|-
 
| 03:09
 
| 03:09
| 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:16
 
|03:16
| Now type: '''awk space hyphen small f space while_loop.awk space awkdemo.txt'''
+
| Now, type: '''awk space hyphen small f space while_loop.awk space awkdemo.txt'''.
  
Press '''Enter'''
+
Press '''Enter'''.
  
 
|-
 
|-
Line 159: Line 159:
 
|-
 
|-
 
| 03:47
 
| 03:47
|Then it checks the specified '''condition.''' And repeats the code inside the '''body''', as long as the specified '''condition'''is '''true'''.
+
|Then it checks the specified '''condition.''' And, repeats the code inside the '''body''', as long as the specified '''condition'''is '''true'''.
  
 
|-
 
|-
 
| 03:56
 
| 03:56
| I have already written a script and named it as '''do_loop.awk'''
+
| I have already written a script and named it as '''do_loop.awk'''.
  
 
The same file is available in the '''Code Files '''link.  
 
The same file is available in the '''Code Files '''link.  
Line 169: Line 169:
 
|-
 
|-
 
| 04:06
 
| 04:06
| In this code, these are the '''statements '''within the '''do loop, '''which will be executed first.
+
| In this code, these are the '''statements '''within the '''do loop '''which will be executed first.
  
 
This is the '''condition '''that will be checked.
 
This is the '''condition '''that will be checked.
Line 179: Line 179:
 
|-
 
|-
 
| 04:23
 
| 04:23
| This '''loop''' will iterate for all the records in the '''awkdemo.txt''' file
+
| This '''loop''' will iterate for all the records in the '''awkdemo.txt''' file.
  
Which means the first 3 fields will get printed for all the records.
+
Which means, the first 3 fields will get printed for all the records.
  
 
|-
 
|-
Line 189: Line 189:
 
|-
 
|-
 
| 04:38
 
| 04:38
|Now type: '''awk space hyphen small f space do underscore loop dot awk space awkdemo dot txt'''
+
|Now, type: '''awk space hyphen small f space do underscore loop dot awk space awkdemo dot txt'''.
  
Press '''Enter'''
+
Press '''Enter'''.
  
 
|-
 
|-
Line 203: Line 203:
 
|-
 
|-
 
| 05:00
 
| 05:00
| Switch to the file '''while underscore loop dot awk'''
+
| Switch to the file '''while underscore loop dot awk'''.
  
 
|-
 
|-
Line 211: Line 211:
 
|-
 
|-
 
| 05:11
 
| 05:11
| This will make the specified '''condition false''' from the beginning.
+
| This will make the specified condition '''false''' from the beginning.
  
So this means, we should not get any output.
+
So, this means, we should not get any output.
  
 
|-
 
|-
 
| 05:19
 
| 05:19
| Save the file and switch to the '''terminal'''.
+
| '''Save''' the file and switch to the '''terminal'''.
  
 
|-
 
|-
Line 223: Line 223:
 
| Clear the '''terminal'''.
 
| Clear the '''terminal'''.
  
Now press  the Up arrow key until you get the command for executing the '''while loop.'''
+
Now, press  the up-arrow key until you get the '''command''' for executing the '''while loop.'''
  
 
|-
 
|-
Line 243: Line 243:
 
|-
 
|-
 
| 05:48
 
| 05:48
| Switch to the file '''do underscore loop dot awk'''
+
| Switch to the file '''do underscore loop dot awk'''.
  
 
|-
 
|-
Line 251: Line 251:
 
|-
 
|-
 
| 05:57
 
| 05:57
|Save the file and switch to the '''terminal.'''
+
|'''Save''' the file and switch to the '''terminal.'''
  
 
|-
 
|-
Line 257: Line 257:
 
| Clear the '''terminal'''.
 
| Clear the '''terminal'''.
  
Now press the Up arrow key until you get the command for '''do loop.'''
+
Now press the up-arrow key until you get the command for '''do loop.'''
  
 
Press '''Enter.'''
 
Press '''Enter.'''
Line 269: Line 269:
 
|-
 
|-
 
| 06:16
 
| 06:16
| For each row, '''awk''' first prints the value at the first''' field, ''' because value of '''variable f '''is initialized to '''1.'''
+
| For each row, '''awk''' first prints the value at the first''' field ''' because value of '''variable f '''is initialized to '''1.'''
  
 
Then the '''condition '''is checked.
 
Then the '''condition '''is checked.
Line 285: Line 285:
 
|-
 
|-
 
| 06:44
 
| 06:44
|Which means the first '''field''' for each record will get printed.
+
|Which means, the first '''field''' for each '''record''' will get printed.
  
 
|-
 
|-
Line 293: Line 293:
 
|-
 
|-
 
| 06:53
 
| 06:53
|Use the '''do-while loop, '''for a job to be executed at least once, irrespective of any other '''condition'''.  
+
|Use the '''do-while loop '''for a job to be executed at least once, irrespective of any other '''condition'''.  
  
 
|-
 
|-
Line 305: Line 305:
 
|-
 
|-
 
| 07:09
 
| 07:09
|The '''for statement''' starts by executing '''''initialization'''''.  
+
|The ''''for' statement''' starts by executing '''''initialization'''''.  
  
 
|-
 
|-
 
| 07:14
 
| 07:14
|Then, as long as the '''''condition''''' is true, it repeatedly executes the '''''statements''' ''within and then '''''increments'''''
+
|Then, as long as the '''''condition''''' is true, it repeatedly executes the '''''statements''' ''within and then '''''increments'''''.
  
 
|-
 
|-
Line 329: Line 329:
 
|-
 
|-
 
| 07:46
 
| 07:46
| There are some more''' looping constructs '''
+
| There are some more''' looping constructs: '''
  
'''break''',  '''continue''',  '''exit'''
+
'''break''',  '''continue''',  '''exit'''.
  
 
|-
 
|-
Line 343: Line 343:
 
|-
 
|-
 
| 08:03
 
| 08:03
|Here notice that the single line '''comments''' are declared with single '''hash '''(#) symbol'''.'''
+
|Here, notice that the single line '''comments''' are declared with single '''hash '''(#) symbol.
  
 
|-
 
|-
Line 369: Line 369:
 
|-
 
|-
 
| 08:40
 
| 08:40
| I have created a file named''' next.awk '''as shown here for this execution.
+
| I have created a file named''' next.awk '''as shown here, for this execution.
  
 
|-
 
|-
 
|08:47
 
|08:47
| Now, what does this command mean?
+
| Now, what does this '''command''' mean?
  
 
|-
 
|-
 
| 08:50
 
| 08:50
| '''awk''' will search for the pattern, '''caret sign hash symbol(^#) '''at the beginning of each line.
+
| '''awk''' will search for the pattern '''caret sign hash symbol(^#) '''at the beginning of each line.
  
 
|-
 
|-
Line 391: Line 391:
 
|-
 
|-
 
| 09:12
 
| 09:12
| Switch to the '''terminal''' and type the command as shown here and
+
| Switch to the '''terminal''' and type the command as shown here
  
 
Press '''Enter.'''
 
Press '''Enter.'''
Line 403: Line 403:
 
| Suppose, we have the students’ records in multiple files with the same format.
 
| Suppose, we have the students’ records in multiple files with the same format.
  
Say in '''awkdemo_mod.txt''' and '''awkdemo2.txt'''
+
Say, in '''awkdemo_mod.txt''' and '''awkdemo2.txt'''.
  
 
|-
 
|-
Line 411: Line 411:
 
|-
 
|-
 
| 09:41
 
| 09:41
|It also has '''comments''' preceeded with '''hash ''' sysmbol.
+
|It also has '''comments''' preceded with '''hash ''' sysmbol.
  
 
|-
 
|-
 
| 09:45
 
| 09:45
|And it has large text at the end with double '''hash #<nowiki>#</nowiki>''' symbol.
+
|And, it has large text at the end with double '''hash #<nowiki>#</nowiki>''' symbol.
  
 
|-
 
|-
 
| 09:50
 
| 09:50
| So our data is in two different files.
+
| So, our data is in two different files.
  
 
'''awk''' should process both the files to give an increment to all the students.
 
'''awk''' should process both the files to give an increment to all the students.
Line 439: Line 439:
 
|-
 
|-
 
| 10:17
 
| 10:17
|I have added '''dollar zero tilde slash caret symbol double hash slash within braces nextfile semicolon '''below the '''begin statement'''.
+
|I have added: '''dollar zero tilde slash caret symbol double hash slash within braces nextfile semicolon, '''below the '''BEGIN''' statement.
  
 
|-
 
|-
Line 451: Line 451:
 
|-
 
|-
 
| 10:39
 
| 10:39
| Save this file.
+
| '''Save''' this file.
  
 
|-
 
|-
Line 457: Line 457:
 
| Switch to the the '''terminal '''and type the following command.
 
| Switch to the the '''terminal '''and type the following command.
  
Press''' Enter'''
+
Press''' Enter'''.
  
 
|-
 
|-
Line 471: Line 471:
 
|-
 
|-
 
| 10:58
 
| 10:58
|In this tutorial we learnt about-  '''while''',  '''do… while''',  '''for''',  '''next''',  '''nextfile '''in''' awk'''
+
|In this tutorial we learnt about-  '''while''',  '''do… while''',  '''for''',  '''next''',  '''nextfile '''in''' awk'''.
  
 
|-
 
|-
 
| 11:06
 
| 11:06
| As an assignment for the student records of '''awkdemo2.txt''',  print only even '''fields'''(i.e. '''field''' 2, '''field''' 4 etc, irrespective of how many '''fields''' are there in the '''input file'''.
+
| As an assignment for the student records of '''awkdemo2.txt''',  print only even '''fields'''(i.e. field 2, field 4 etc.) irrespective of how many '''fields''' are there in the '''input file'''.
  
 
|-
 
|-
 
| 11:22
 
| 11:22
| 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 485: Line 485:
 
|-
 
|-
 
| 11:30
 
| 11:30
| The Spoken Tutorial Project team conducts workshops using spoken tutorials and gives certificates on passing online tests.
+
| The '''Spoken Tutorial Project''' team conducts workshops using spoken tutorials and gives certificates on passing online tests.
  
 
For more details, please write to us.
 
For more details, please write to us.
Line 497: Line 497:
 
|-
 
|-
 
| 11:49
 
| 11:49
| 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 505: Line 505:
 
| This script has been contributed by Antara.
 
| This script has been contributed by Antara.
  
And this is Praveen from IIT Bombay signing off.
+
And this is Praveen from '''IIT Bombay''', signing off.
  
 
Thanks for joining.
 
Thanks for joining.
  
 
|}
 
|}

Revision as of 14:46, 10 July 2019

Time
Narration
00:01 Hello and welcome to this spoken tutorial on Loops in awk.
00:06 In this tutorial, we will learn about-

while, do-while, for and more looping constructs in awk.

00:16 We will do this through some examples.
00:20 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 loop allows us to perform one or more actions repeatedly.
01:12 while, do-while and for are the available loops in awk.
01:18 The syntax of 'while' loop can be seen here.
01:22 While loop first checks whether the specified condition is true.
01:27 If yes, then it executes the code within the body.

This loop will be repeated as long as the specified while condition is true.

01:37 We will use the same awkdemo.txt file that we have used earlier.
01:43 I have already written a script named while_loop.awk.
01:48 The same file is available in the Code Files link of this tutorial.
01:53 Here, we have set the field separator as Pipe symbol.
01:58 Initially, we have to set the value of the loop variable i as 1.
02:04 Here, we have taken one more variable f and initialized it to 1.
02:10 Variable f represents the field counter or the position of the fields for each record.
02:17 Now, in the while condition, we check if i is less than or equal to 3.
02:23 If yes, then it will print the value in the fth field, for that record in awkdemo.txt file.
02:31 Then we will increment the field counter f by 1.
02:36 After that, we will also increment the value of loop variable i by 1.
02:43 This printf is for printing a newline character at the end of each row.
02:49 This loop will be executed for all the records in the awkdemo.txt file.
02:55 Which means, the first 3 fields will get printed for each record.
03:00 Let’s execute this code now.
03:03 Open the terminal by pressing Ctrl, Alt and T keys.
03:09 Go to the folder in which you downloaded and extracted the Code Files using cd command.
03:16 Now, type: awk space hyphen small f space while_loop.awk space awkdemo.txt.

Press Enter.

03:29 Observe that we get the first three fields of all the rows in the output.
03:35 Let us do the same with the do-while loop.
03:38 The syntax of do-while loop can be seen here.
03:42 The do-while loop always executes the code inside the body once.
03:47 Then it checks the specified condition. And, repeats the code inside the body, as long as the specified conditionis true.
03:56 I have already written a script and named it as do_loop.awk.

The same file is available in the Code Files link.

04:06 In this code, these are the statements within the do loop which will be executed first.

This is the condition that will be checked.

04:15 After that, the statements inside the loop will be executed repeatedly as long as the condition is true.
04:23 This loop will iterate for all the records in the awkdemo.txt file.

Which means, the first 3 fields will get printed for all the records.

04:33 Let’s switch to the terminal. Let me clear the terminal.
04:38 Now, type: awk space hyphen small f space do underscore loop dot awk space awkdemo dot txt.

Press Enter.

04:52 We get the same output. Then why do we have both while and do-while loops?
04:58 Let us understand the difference.
05:00 Switch to the file while underscore loop dot awk.
05:05 Now, change the value of loop counter i from 1 to 4.
05:11 This will make the specified condition false from the beginning.

So, this means, we should not get any output.

05:19 Save the file and switch to the terminal.
05:22 Clear the terminal.

Now, press the up-arrow key until you get the command for executing the while loop.

05:30 Now press Enter.
05:32 See, we are not getting any output apart from blank lines.
05:37 For each record in the awkdemo.txt file, blank lines are getting printed in the output.
05:44 Now, let us make some changes in the do loop file.
05:48 Switch to the file do underscore loop dot awk.
05:53 Change the value of i from 1 to 4.
05:57 Save the file and switch to the terminal.
06:01 Clear the terminal.

Now press the up-arrow key until you get the command for do loop.

Press Enter.

06:10 In the output, only the first field for each row is printed.

What is the reason?

06:16 For each row, awk first prints the value at the first field because value of variable f is initialized to 1.

Then the condition is checked.

06:28 Since the value of the loop counter i is 4, then the condition is false.

Hence, the loop is terminated there only, for that record.

06:39 This loop will iterate for all the records in the awkdemo.txt file.
06:44 Which means, the first field for each record will get printed.
06:49 We are getting the output at least once for each record.
06:53 Use the do-while loop for a job to be executed at least once, irrespective of any other condition.
07:01 We can do the same with the for loop also.
07:05 The syntax of for loop can be seen here.
07:09 The 'for' statement starts by executing initialization.
07:14 Then, as long as the condition is true, it repeatedly executes the statements within and then increments.
07:23 Assuming your familiarity with a language like C or C++, I am not explaining the syntax in detail.
07:30 This is how for loop for this condition looks like.
07:35 Here, initialization, condition checking and variable incrementation are done in the same line.
07:43 Try this out by yourself.
07:46 There are some more looping constructs:

break, continue, exit.

07:53 We will see some relevant examples on these in further tutorials.
07:58 We may have a single and multiline comments in our file.
08:03 Here, notice that the single line comments are declared with single hash (#) symbol.
08:10 The multiline comments are declared with the help of double hash (##) symbol.
08:16 Now, there is no point of checking and printing these comments in the output.
08:22 We have to skip the lines starting with hash (##) symbol.

How can we do this?

08:28 Recall the case of giving 50% increment in the stipend for those who are getting more than 8000.
08:36 We will use the same example for skipping the comments.
08:40 I have created a file named next.awk as shown here, for this execution.
08:47 Now, what does this command mean?
08:50 awk will search for the pattern caret sign hash symbol(^#) at the beginning of each line.
08:57 If the pattern is found, the keyword next tells awk to skip the current line immediately.
09:04 Then awk will start processing from the next line in the file.

This will save the processing time.

09:12 Switch to the terminal and type the command as shown here.

Press Enter.

09:20 We get the output without any comments.
09:24 Suppose, we have the students’ records in multiple files with the same format.

Say, in awkdemo_mod.txt and awkdemo2.txt.

09:37 See, it is similar to our previous file.
09:41 It also has comments preceded with hash sysmbol.
09:45 And, it has large text at the end with double hash ## symbol.
09:50 So, our data is in two different files.

awk should process both the files to give an increment to all the students.

09:59 Once we reach double hash(##) symbol of the first file, awk should stop processing that file entirely.
10:06 Then it has to start the execution from the next file.

This will save the processing time.

10:13 Modify the next.awk as shown here.
10:17 I have added: dollar zero tilde slash caret symbol double hash slash within braces nextfile semicolon, below the BEGIN statement.
10:29 This will search for double hash # symbol at the beginning of each line.
10:34 If found, awk will skip the current file to process the next file.
10:39 Save this file.
10:41 Switch to the the terminal and type the following command.

Press Enter.

10:48 See, we are getting the output from both the files.
10:53 This brings us to the end of this tutorial.

Let us summarize.

10:58 In this tutorial we learnt about- while, do… while, for, next, nextfile in awk.
11:06 As an assignment for the student records of awkdemo2.txt, print only even fields(i.e. field 2, field 4 etc.) irrespective of how many fields are there in the input file.
11:22 The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

11:30 The Spoken Tutorial Project team conducts workshops using spoken tutorials and gives certificates on passing online tests.

For more details, please write to us.

11:43 Do you have questions in THIS Spoken Tutorial?

Please visit this site.

11:49 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

12:01 This 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