[ LETTERS ]
What Day Is It?
I’m sending a little feedback to Dave
Taylor’s “parsing the cal” output (see
Dave’s column in the June–September
2011 issues). There’s no need to use
regular expressions in the awk script at
all, because you can compare numbers
directly. Below, you’ll find the script you
can call by the following command line:
cal | awk -f day.awk 25
day.awk BEGIN{ ARGC=1; getline;getline; for(i=1;i<=NF;i++) wd[i]=$i } {for(i=1;i<=NF;i++) if($i==ARGV[1]) print wd[i+($0~/^ /?(7-NF):0)] }
In the BEGIN block, ARGC=1 prevents
it from taking the last argument ( 25 in
this case) as an input file. Then, the
script fetches the first two lines and
stores the weekday names in an array.
The rest of the script compares the
argument number with every field in
every line. On a match, the day name
is output, wd[i]. The month does not
always start on a Sunday, so the script
has to fix the index for lines starting
with a space (condition $0~/^ /). For
those lines, the first item starts with
index 7-NF. Note that this fix also works
fine for the second line of numbers
(which also starts with a space), since
7-7 equals 0.
You can make the script a one-liner if
you like. It was written in multiple lines for
readability reasons. And, last but not least:
great OS, great magazine, keep going.
—Eric Miller
Dave Taylor replies: Thanks for your
note. I realized that there was a way to
break down the input and process it
with a multiline awk script (just as I
could do much of the task more easily
in Perl or, for that matter, a short C
program), but my goal with the Work
the Shell column is to force myself to
stick with standard Linux shell commands and capabilities as much as
possible and see what I can accomplish.
Sometimes the result is a bit, um,
Byzantine and unquestionably inefficient, but the upside is that it’s always
interesting and, I hope, informative
and entertaining reading.
Installfest 2001
Installfest goal: adapt seven older desktop computers for use by fourth-grade
teacher Mike Steins at Shenandoah St.
Elementary School and learn how to
install/configure Linux. (We had two
Linux experts in the group.)
Outcome: we got four computers
working with Linux by scavenging parts
from various other machines.