Damian Walker

Personal Web Pages

Adventures in AWK: a Tiny BASIC Conversion Script

Wednesday, 14th August 2019

I've finally found the time to settle down and re-learn AWK. After getting fed up of manually numbering the lines of my Tiny BASIC programs, I decided it was time to automate the process. So I've written a quick-and-dirty AWK script to read through a file, strip out the comment lines, and add line numbers to the remaining lines that lack them.

The program will start at line 1, and increment the line numbers by 1. If it encounters a line that already has a number, it will adopt that line number and continue counting up from there. It checks that the line number is valid; i.e. that it isn't lower than the number that would be assigned to that line anyway.

The output is formatted nicely for 3-digit line numbers. Larger numbers will work, but the listing looks a bit wonky. This is easily changed; there's a "%3d" in the file that can be changed to "%4d" or, if you want to left-justify all the line numbers, just "%d".

To invoke the script, use the command line:

awk -f number.awk < INPUTFILE > OUTPUTFILE

You can download the file here.