Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr2438 :: Gnu Awk - Part 8

More about loops

<< First, < Previous, , Latest >>

Thumbnail of Dave Morriss
Hosted by Dave Morriss on 2017-12-06 is flagged as Explicit and is released under a CC-BY-SA license.
Awk utility, Awk language, gawk, loops. 2.
The show is available on the Internet Archive at: https://archive.org/details/hpr2438

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:20:39

Learning Awk.

Episodes about using Awk, the text manipulation language. It comes in various forms called awk, nawk, mawk and gawk, but the standard version on Linux is GNU Awk (gawk). It's a programming language optimised for the manipulation of delimited text.

Gnu Awk - Part 8

Introduction

This is the eighth episode of the "Learning Awk" series that b-yeezi and I are doing.

Recap of the last episode

  • The while loop: tests a condition and performs commands while the test returns true

  • The do while loop: performs commands after the do, then tests afterwards, repeating the commands while the test is true.

  • The for loop (type 1): initialises a variable, performs a test, and increments the variable all together, performing commands while the test is true.

  • The for loop (type 2): sets a variable to successive indices of an array, preforming a collection of commands for each index.

These types of loops were demonstrated by examples in the last episode.

Note that the example for 'do while' was an infinite loop (perhaps as a test of the alertness of the audience!):

#!/usr/bin/awk -f
BEGIN {

    i=2;
    do {
        print "The square of ", i, " is ", i*i;
        i = i + 1
    }
    while (i != 2)

exit;
}

The condition in the while is always true:

The square of  2  is  4
The square of  3  is  9
The square of  4  is  16
The square of  5  is  25
The square of  6  is  36
The square of  7  is  49
The square of  8  is  64
The square of  9  is  81
The square of  10  is  100
...
The square of  1269630  is  1611960336900
The square of  1269631  is  1611962876161
The square of  1269632  is  1611965415424
The square of  1269633  is  1611967954689
The square of  1269634  is  1611970493956
...

The variable i is set to 2, the print is executed, then i is set to 3. The test "i != 2" is true and will be ad infinitum.

Some more statements

We will come back to loops later in this episode, but first this seems like a good point to describe another statement: the switch statement.

Long notes

The notes for rest of this episode are available here.


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2017-12-23 03:39:52 by Ron Strelecki

AWK part 8

It is strange what people pick up on in a tutorial. For instance, I'd never run a program using: echo nn | ./program.awk ... It's a very handy little construction. I even popped the divisor program into my bin and named it "isprime" so I can just ask "echo 913 | isprime"? and get an answer.

It's often the little off the cuff details that catch attention. Thanks!

Comment #2 posted on 2017-12-24 23:10:52 by Dave Morriss

Thanks Ron

I'm glad you found something of interest in the episode.

This is really a Unix thing. The echo command writes to STDOUT by default, and Awk reads from STDIN unless you tell it otherwise, so joining the two like that in a pipeline (as it's called) achieves a useful result very simply.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
What is the HOST_ID for the host of this show?
What does HPR mean to you?