Formatting Dates With awk

I’m a huge sci-fi fan (big surprise there) but can’t seem to be able to keep up with TV schedules that well.  Particularly because I tend to save up TV over a week (or more) and then catch up later.  Which means I either need to set the DVR or remember to source content from elsewhere.  I used to literally write episode airings on a paper calendar, but that got annoying to keep up with and then I’d spill coffee on it or lose it.  Yes, yes, I know the DVR can do recurring recordings, but I tend to lose track after a few weeks and then wonder what’s new, what’s a rerun, or what aired during a huge thunderstorm and we either lost signal or got preempted by weather coverage.

I thought about email reminders, but that gets spammy and doesn’t really fit my requirements.  Local calendars wouldn’t really work, as I often move between computers and don’t want to fool with exporting/importing.  Not to mention others would like to access the schedule.  But the Google based calendar would be cumbersome and entirely too time consuming for this purpose.  A text file hosted on my XS would be perfect.

Eventually what I figured out I wanted was to create a personalized TV schedule in a flat text file in a data format that awk could parse.

OK, easy enough.  I looked up schedules on Wikipedia and did up something that looks like this (obviously this is a truncated version):

2011/06/13:Sanctuary:S03E19:”Out of the Blue”
2011/06/20:Sanctuary:S03E20:”Into the Black”
2011/06/30:Futurama:S06E16:”Ghost in the Machines”
2011/07/07:Futurama:S06E17:”Law and Oracle”
2011/07/08:Torchwood:S04E01:”The New World”
2011/07/11:Warehouse.13:S03E01:”The New Guy”
2011/07/14:Futurama:S06E18:”The Silence of the Clamps”
2011/07/15:Haven:S02E01:”A Tale of Two Audreys”
2011/07/17:Breaking.Bad:S04E01:”Box Cutter”

Yeah, I manually entered all that, but sorting stuff before editing, depending on what I’m doing, makes data entry a breeze.

To sort by date:

cp tv-listings.txt tv-listings.bak
sort tv-listings.bak > tv-listings.txt

To sort by show name:

cp tv-listings.txt tv-listings.bak
sort -t: -k2 tv-listings.bak > tv-listings.txt

Now, sorted by date would probably be good enough.  But I wanted something a little more human readable.  Enter awk.

Since the XS has Apache, it would be nice for my friends and me to be able to access my TV schedule from anywhere.  I’m not gonna mess around with html, though.  A simple .txt is fine.

The difficult part was figuring out how to make awk parse the date.  To generate a text file that looks like this and upload it to my XS…

Jun 13 Mon Sanctuary S03E19 “Out of the Blue”
Jun 20 Mon Sanctuary S03E20 “Into the Black” 
Jun 23 Thu Futurama S06E14 “Neutopia”
Jun 23 Thu Futurama S06E15 “Benderama”
Jun 30 Thu Futurama S06E16 “Ghost in the Machines”
Jul 07 Thu Futurama S06E17 “Law and Oracle”
Jul 08 Fri Torchwood S04E01 “The New World”
Jul 11 Mon Alphas S01E01 “Pilot”
Jul 11 Mon Eureka S04E11 “Liftoff”
Jul 11 Mon Warehouse.13 S03E01 “The New Guy”
Jul 14 Thu Futurama S06E18 “The Silence of the Clamps”
Jul 15 Fri Haven S02E01 “A Tale of Two Audreys”
Jul 15 Fri Torchwood S04E02 “Renditions”
Jul 17 Sun Breaking.Bad S04E01 “Box Cutter”

…here’s the simple bash script I used:



# Backup master tv listings file just in case

cp $FILE $FILE.bak
sort $FILE.bak > $FILE

awk ‘BEGIN{FS=”:”}
 cmd=”date +”%b %d %a” –date=47″$1″47″
 cmd | getline day
 print day, $2, $3, $4
}’ $FILE > current-listings.txt

scp current-listings.txt myserver:/var/www/html/anna

Now, the fun part about this line

cmd=”date +”%b %d %a” –date=47″$1″47″

Is that you can edit the date format any way you like.  For example, if I format that line to look like so:

cmd=”date +”%B %d %A” –date=47″$1″47″

It generates this file:

June 13 Monday Sanctuary S03E19 “Out of the Blue”
June 20 Monday Sanctuary S03E20 “Into the Black” 
June 23 Thursday Futurama S06E14 “Neutopia”
June 23 Thursday Futurama S06E15 “Benderama” 
June 30 Thursday Futurama S06E16 “Ghost in the Machines” 
July 07 Thursday Futurama S06E17 “Law and Oracle”
July 08 Friday Torchwood S04E01 “The New World”
July 11 Monday Alphas S01E01 “Pilot”
July 11 Monday Eureka S04E11 “Liftoff”
July 11 Monday Warehouse.13 S03E01 “The New Guy”
July 14 Thursday Futurama S06E18 “The Silence of the Clamps”
July 15 Friday Haven S02E01 “A Tale of Two Audreys”
July 15 Friday Torchwood S04E02 “Renditions”
July 17 Sunday Breaking.Bad S04E01 “Box Cutter”

Or I could move the date fields around.  This:

cmd=”date +”%A %d %B” –date=47″$1″47″

Gives me this:

Monday 13 June Sanctuary S03E19 “Out of the Blue”
Monday 20 June Sanctuary S03E20 “Into the Black” 
Thursday 23 June Futurama S06E14 “Neutopia”
Thursday 23 June Futurama S06E15 “Benderama”
Thursday 30 June Futurama S06E16 “Ghost in the Machines” 
Thursday 07 July Futurama S06E17 “Law and Oracle”
Friday 08 July Torchwood S04E01 “The New World”
Monday 11 July Alphas S01E01 “Pilot”
Monday 11 July Eureka S04E11 “Liftoff”
Monday 11 July Warehouse.13 S03E01 “The New Guy”
Thursday 14 July Futurama S06E18 “The Silence of the Clamps”
Friday 15 July Haven S02E01 “A Tale of Two Audreys”
Friday 15 July Torchwood S04E02 “Renditions”
Sunday 17 July Breaking.Bad S04E01 “Box Cutter”

Obviously this could be a lot more sophisticated and/or automated, but it only takes me a few minutes a week to maintain.  And it should make keeping up with shows a little easier.

And even more obviously, there are better things to do with something like this.  Like if you have “real life” things, such as children’s soccer games, recitals, dentist appointments, etc.

Anyway, for recurring stuff that you don’t necessarily want or need a fancy calendar application or notification system for, this might fit the bill.  And awk lets you format the date in all kinds of pretty ways!