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

From Script | Spoken-Tutorial
Jump to: navigation, search
m (Nancyvarkey moved page Linux/C3/Loops-in-awk/English to Linux-AWK/C2/Loops-in-awk/English without leaving a redirect: New series)
 
(One intermediate revision by the same user not shown)
Line 18: Line 18:
 
| 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 2: Learning Objective
 
| 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 2: Learning Objective
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about-
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about-
 
 
 
* '''while'''
 
* '''while'''
 
* '''do-while'''
 
* '''do-while'''
Line 34: Line 32:
 
* '''gedit text editor 3.20.1'''
 
* '''gedit text editor 3.20.1'''
  
* You can use any text editor of your choice.
+
You can use any text editor of your choice.
 
+
 
+
  
 
|-
 
|-
Line 43: Line 39:
 
* 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 84:
 
| 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 i = 1
 
| 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 i = 1
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Initially, we have to the set the value of the '''loop variable i '''as 1.  
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Initially, we have to the set the value of the '''loop variable i '''as 1.  
 
 
 
  
 
|-
 
|-
Line 99: Line 90:
  
  
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 '''fields''' for each '''record'''.
  
 
|-
 
|-
Line 111: Line 102:
 
|-
 
|-
 
| 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 f++
 
| 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 f++
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then we will increase the '''field counter f''' by 1.
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then we will increment the '''field counter f''' by 1.
  
 
|-
 
|-
 
| 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 i++
 
| 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 i++
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| After that, we will also increment the value of '''loop''' variable '''i''' by 1.
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| After that, we will also increment the value of '''loop variable i''' by 1.
  
 
|-
 
|-
Line 179: Line 170:
  
  
The same is available in the '''Code Files '''link.  
+
The same file is available in the '''Code Files '''link.  
  
 
|-
 
|-
Line 189: Line 180:
  
  
After that, the '''statements '''inside the '''loop '''will be executed repeatedly as long as the '''condition '''is '''true'''.
+
After that,  
 +
*the '''statements '''inside the '''loop '''will be executed repeatedly  
 +
*as long as the '''condition '''is '''true'''.
  
 
|-
 
|-
Line 208: Line 201:
 
Now type:
 
Now type:
  
 +
'''awk space hyphen small f do underscore loop dot awk awkdemo dot txt'''
  
'''awk space -f do_loop.awk awkdemo.txt'''
+
Press '''Enter'''
 
+
and press '''Enter'''
+
  
 
|-
 
|-
Line 226: Line 218:
  
 
<nowiki>[code uploaded as while_loop_mod.awk]</nowiki>
 
<nowiki>[code uploaded as while_loop_mod.awk]</nowiki>
| 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 file '''while_loop.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 file '''while underscore loop dot awk'''
  
  
Line 242: Line 234:
 
|-
 
|-
 
| 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;"| Press Ctrl+S
 
| 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;"| Press Ctrl+S
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save the file and switch to '''terminal'''.
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save the file and switch to the '''terminal'''.
  
 
|-
 
|-
Line 248: Line 240:
  
 
<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;"| Press up key until you get the command for executing the '''while loop.'''
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Clear the '''terminal'''.
 +
 
 +
 
 +
Now press Up arrow key until you get the command for executing the '''while loop.'''
  
  
Line 262: Line 257:
 
|-
 
|-
 
| 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;"|  
 
| 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;"|  
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now, let us make the same changes in the '''do loop '''file.
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now, let us make some changes in the '''do loop '''file.
  
 
|-
 
|-
Line 268: Line 263:
  
 
<nowiki>[code uploaded as do_loop_mod.awk]</nowiki>
 
<nowiki>[code uploaded as do_loop_mod.awk]</nowiki>
| 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 file '''do_loop.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 file '''do underscore loop dot awk'''
  
  
Change the value of '''i '''from 1 to 4
+
Change the value of '''i '''from 1 to 4.
  
  
Line 280: Line 275:
  
 
<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;"| Press the Up arrow key until you get the command for '''do loop.'''
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Clear the '''terminal'''.
  
 +
Now press the Up arrow key until you get the command for '''do loop.'''
  
Now press '''Enter.'''
+
 
 +
Press '''Enter.'''
  
 
|-
 
|-
Line 297: Line 294:
 
|-
 
|-
 
| 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 in code file
 
| 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 in code file
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For each row, '''awk''' first prints the value at the first''' field, '''because value of variable''' f '''is initialized to '''1.'''
+
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For each row,  
 +
*'''awk''' first prints the value at the first''' field, '''
 +
*because value of '''variable f '''is initialized to '''1.'''
  
  
Line 303: Line 302:
  
  
Since the value of the '''loop counter i '''is 4, the '''condition '''is '''false'''.
+
Since the value of the '''loop counter i '''is 4, then the '''condition '''is '''false'''.
  
  
Line 320: Line 319:
  
  
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 331: Line 330:
  
  
The&nbsp;'''for&nbsp;statement''' starts by executing&nbsp;'''''initialization'''''.  
+
The '''for statement''' starts by executing '''''initialization'''''.  
  
  
Then, as long as the'''&nbsp;''condition'''''&nbsp;is true, it repeatedly executes&nbsp;the '''''statements''' ''within&nbsp;and then&nbsp;'''''increments''.&nbsp;'''
+
Then, as long as the'''''condition''''' is true, it repeatedly executes the '''''statements''' ''within and then '''''increments'''''
  
  
Line 467: Line 466:
  
  
I have added '''dollar zero tilde slash caret symbol double hash slash within curly brackets 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'''.
  
  
This will search for double '''hash #''' symbol at the beginning of a line.
+
This will search for double '''hash #''' symbol at the beginning of each line.
  
  
Line 486: Line 485:
 
<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;"| Switch to the the '''terminal '''and type the following command.
 
| 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 the '''terminal '''and type the following command.
 
 
'''awk space -F "|" space -f space next.awk space awkdemo_mod.txt space awkdemo2.txt'''
 
 
  
 
Press''' Enter'''
 
Press''' Enter'''
Line 500: Line 495:
 
| 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 13: Summary
 
| 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 13: Summary
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This brings us to the end of this tutorial.
 
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This brings us to the end of this tutorial.
 
  
 
Let us summarize.
 
Let us summarize.
Line 506: Line 500:
  
 
In this tutorial we learnt about-
 
In this tutorial we learnt about-
 
 
* '''while'''
 
* '''while'''
 
* '''do… while'''
 
* '''do… while'''
Line 512: Line 505:
 
* '''next'''
 
* '''next'''
 
* '''nextfile '''in''' awk'''
 
* '''nextfile '''in''' awk'''
 
 
  
 
|-
 
|-
Line 521: Line 512:
 
* print only even '''fields'''(i.e. '''field''' 2, '''field''' 4 etc.  
 
* print only even '''fields'''(i.e. '''field''' 2, '''field''' 4 etc.  
 
* irrespective of how many '''fields''' are there in the '''input file'''.
 
* irrespective of how many '''fields''' are there in the '''input file'''.
 
 
  
 
|-
 
|-
Line 548: Line 537:
 
| 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
+
  
 
|-
 
|-
 
| 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:36, 22 March 2018

Title of script: Loops in Awk

Author: Antara Roy Choudhury

Keywords: for, while, do-while, next, nextfile


Visual Cue
Narration
Slide 1: Introduction Welcome to this spoken tutorial on Loops in awk.
Slide 2: Learning Objective In this tutorial we will learn about-
  • while
  • do-while
  • for
  • and more looping constructs in awk

We will do this through some examples.

Slide 3: System requirement To record this tutorial, I am using
  • Ubuntu Linux 16.04 OS and
  • gedit text editor 3.20.1

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.
  • You should have familiarity with any programming language like C or C++
  • If not, then please go through the corresponding 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.


Please download and extract them.

Slide 6: Loops A loop allows us to perform one or more actions repeatedly.


while, do-while and for are the available loops in awk.

Slide 7: While loop The syntax of while loop can be seen here.


While loop first checks whether the specified condition is true.

If yes, then it executes the code within the body.


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

Show awkdemo.txt file in gedit We will use the same awkdemo.txt file, that we have used earlier.
show while_loop.awk in Gedit I have already written a script named while_loop.awk


The same is available in the Code Files link of this tutorial.

Point to the pipe symbol Here we have set the field separator as Pipe symbol.
Highlight i = 1 Initially, we have to the set the value of the loop variable i as 1.
Highlight f = 1 Here, we have taken one more variable f and initialized it to 1.


Variable f represents the field counter or the position of the fields for each record.

Highlight while (i<=3) Now, in the while condition, we check if i is less than or equal to 3.
Highlight Print statement If yes, then it will print the value in the fth field, for that record in awkdemo.txt file.
Highlight f++ Then we will increment the field counter f by 1.
Highlight i++ After that, we will also increment the value of loop variable i by 1.
Highlight printf("\n") This printf is for printing a newline character at the end of each row.
This loop will be executed for all the records in the awkdemo.txt file.


Which means the first 3 fields will get printed for each record.

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
Type in terminal:

awk -f while_loop.awk awkdemo.txt

[Enter]

Now type:

awk space minus small f while_loop.awk space awkdemo.txt


Press Enter

Show the output Observe that we get the first three fields of all the rows in the output.
Let us do the same with the do-while loop.
Slide 8: Do While Loop The syntax of do-while loop can be seen here.


The do-while loop always executes the code inside the body once.


Then it checks the specified condition.


And repeats the code inside the body, as long as the specified condition is true.

show do_loop.awk in Gedit I have already written a script and named it as do_loop.awk


The same file is available in the Code Files link.

Show the contents and highlight appropriately In this code, these are the statements within the do loop, which will be executed first.


This is the condition that will be checked.


After that,

  • the statements inside the loop will be executed repeatedly
  • as long as the condition is true.
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.

Type:

awk -f do_loop.awk awkdemo.txt

[Enter]

Let’s switch to the terminal. Let me clear the terminal.

Now type:

awk space hyphen small f do underscore loop dot awk awkdemo dot txt

Press Enter

Scroll up and show outputs of while loop and do loop We get the same output.


Then why do we have both while and do-while loops?

Let us understand the difference.

In while_loop.awk, assign i=4

[code uploaded as while_loop_mod.awk]

Switch to the file while underscore loop dot awk


Now, change the value of loop counter i from 1 to 4.

Highlight

(i<=3)

This will make the specified condition false from the beginning.


So this means, we should not get any output.

Press Ctrl+S Save the file and switch to the terminal.
awk -f while_loop.awk awkdemo.txt

[Enter]

Clear the terminal.


Now press Up arrow key until you get the command for executing the while loop.


Now press Enter.

Show the output See, we are not getting any output apart from blank lines.


For each record in the awkdemo.txt file, blank lines are getting printed in the output.

Now, let us make some changes in the do loop file.
In do_loop.awk, assign i=4

[code uploaded as do_loop_mod.awk]

Switch to the file do underscore loop dot awk


Change the value of i from 1 to 4.


Save the file and switch to the terminal.

awk -f do_loop.awk awkdemo.txt

[Enter]

Clear the terminal.

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


Press Enter.

Show the output


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


What is the reason?

Highlight in code file 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.


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.

Highlight in code file This loop will iterate for all the records in the awkdemo.txt file.


Which means the first field for each record will get printed.

Highlight output We are getting the output at least once for each record.


Use the do-while loop, for a job to be executed at least once, irrespective of any other condition.

Retain same screen We can do the same with the for loop also.
Slide 9: for loop The syntax of for loop can be seen here.


The for statement starts by executing initialization.


Then, as long as thecondition is true, it repeatedly executes the statements within and then increments


Assuming your familiarity with a language like C or C++, I am not explaining the syntax in detail.

show for_loop.awk in Gedit This is how the for loop for this condition looks like.


Here, initialization, condition checking and variable incrementation are done in the same line.


Try this out by yourself.

Slide 10: looping constructs There are some more looping constructs
  • break
  • continue
  • exit

We will see some relevant examples on these in further tutorials.

Show awkdemo_mod.txt We may have a single and multiline comments in our file.


Here notice that the single line comments are declared with single hash (#) symbol.


The multiline comments are declared with the help of double hash (##) symbol.

Highlight Now, there is no point of checking and printing these comments in the output.


We have to skip the lines starting with hash (##) symbol.


How can we do this?

Recall the case of giving 50% increment in the stipend for those who are getting more than 8000.


We will use the same example for skipping the comments.

show next.awk


I have created a file named next.awk as shown here for this execution.
Highlight $0~/^#/ {next;} Now, what does this command mean?
Slide 11: next awk will search for the pattern, caret sign hash symbol(^#) at the beginning of each line.


If the pattern is found, the keyword next tells awk to skip the current line immediately.


Then awk will start processing from the next line in the file.


This will save the processing time.

Type

awk -F "|" -f next.awk awkdemo_mod.txt

[Enter]

Switch to the terminal and type the command as shown and

press Enter.

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


Say in awkdemo_mod.txt and awkdemo2.txt

Show awkdemo2.txt See, it is similar to our previous file.


It also has comments preceeded with hash # sysmbol.


And it has large text at the end with double hash ## symbol.

Slide 12: nextfile So our data is in two different files.


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


Once we reach double hash(##) symbol of the first file,

awk should stop processing that file entirely.


Then it has to start the execution from the next file.


This will save the processing time.

Type $0~/^#/ {next;} after the begin statement.


Highlight $0~/^#/ {nextfile;}

Modify the next.awk as shown here.


I have added dollar zero tilde slash caret symbol double hash slash within braces nextfile semicolon below the begin statement.


This will search for double hash # symbol at the beginning of each line.


If found, awk will skip the current file to process the next file.

Press Ctrl+s Save this file.
Type

awk -F "|" -f next.awk awkdemo_mod.txt awkdemo2.txt

[Enter]

Switch to the the terminal and type the following command.

Press Enter

Show the output See, we are getting the output from both the files.
Slide 13: Summary This brings us to the end of this tutorial.

Let us summarize.


In this tutorial we learnt about-

  • while
  • do… while
  • for
  • next
  • nextfile in awk
Slide 14: Assignment 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.
Slide 13:

About Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.


Please download and watch it.

Slide 14:

Spoken Tutorial workshops

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


For more details, please write to us.

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.


And this is Praveen from IIT Bombay signing off.


Thanks for joining.

Contributors and Content Editors

Antarade, Nancyvarkey