Linux-AWK/C2/Loops-in-awk/English
Title of script: Loops in Awk
Author: Antara Roy Choudhury
Keywords: for, while, do-while, next, nextfile
|
|
Slide 1: Introduction | Welcome to this spoken tutorial on Loops 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: Loops | A loop allows us to perform one or more actions repeatedly.
|
Slide 7: While loop | The syntax of while loop can be seen here.
If yes, then it executes the code within the body.
|
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
|
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.
|
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.
| |
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
|
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.
|
show do_loop.awk in Gedit | I have already written a script and named it as do_loop.awk
|
Show the contents and highlight appropriately | In this code, these are the statements within the do loop, which will be executed first.
|
This loop will iterate for all the records in the awkdemo.txt file
| |
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.
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
|
Highlight
(i<=3) |
This will make the specified condition false from the beginning.
|
Press Ctrl+S | Save the file and switch to the terminal. |
awk -f while_loop.awk awkdemo.txt
[Enter] |
Clear the terminal.
|
Show the output | See, we are not getting any output apart from blank lines.
|
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
|
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.
|
Show the output
|
In the output, only the first field for each row is printed.
|
Highlight in code file | For each row,
|
Highlight in code file | This loop will iterate for all the records in the awkdemo.txt file.
|
Highlight output | We are getting the output at least once for each record.
|
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.
|
show for_loop.awk in Gedit | This is how the for loop for this condition looks like.
|
Slide 10: looping constructs | There are some more looping constructs
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.
|
Highlight | Now, there is no point of checking and printing these comments in the output.
|
Recall the case of giving 50% increment in the stipend for those who are getting more than 8000.
| |
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.
|
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.
| |
Show awkdemo2.txt | See, it is similar to our previous file.
|
Slide 12: nextfile | So our data is in two different files.
awk should stop processing that file entirely.
|
Type $0~/^#/ {next;} after the begin statement.
|
Modify the next.awk as shown here.
|
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.
|
Slide 14: Assignment | As an assignment for the student records of awkdemo2.txt,
|
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 and gives certificates on passing online tests.
|
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.
|