Skip to content

Instantly share code, notes, and snippets.

@beardhatcode
Last active December 24, 2015 19:08
Show Gist options
  • Save beardhatcode/6847670 to your computer and use it in GitHub Desktop.
Save beardhatcode/6847670 to your computer and use it in GitHub Desktop.
Gets information about the current day from http://www.daysoftheyear.com
lynx -dump http://www.daysoftheyear.com/days/$(date +%Y)/$(date +%m)/ | grep -v "^\s" | grep -v "^\s*$"|sed "s/\[[0-9]*\]/ -/;s/And also.../Fun 33th Mon, 9999/"|sed -n "/\S* $(date +%-d)\S* \S*, \d*/,/\S* \d*\S* \S*, \d*/p"|head -n -1;echo "See http://www.daysoftheyear.com for more"

Output

Sun 6th Oct, 2013
 -Change A Light Day
 -Mad Hatter Day
See http://www.daysoftheyear.com for more

How it works

  1. get data: Get the page of the current month of http://www.daysoftheyear.com. I use the month because my time zone is not the same as the one of the website, so to be able to get the right day, I found that the month page is far more useful to do that. Note that the -dump option prints the data to standard output. Lynx strips out HTML tags ans leaves with data that is easier to handle. lynx -dump http://www.daysoftheyear.com/days/$(date +%Y)/$(date +%m)/

  2. clean: remove lines that start with spaces (^\s, these lines contain extra information that we don't need) and remove blank lines (^\s*$). What remains is mostly dates and the names of the special days. grep -v "^\s" | grep -v "^\s*$"

  3. Add ticks before day names: in the output from lynx the links to the days are shown like [135]... Day. We repace the number beween brackets by a tick. And change the "And also" text to "Fun 33th Mon, 9999" so the last record has the same kind of end as each record sed "s/\[[0-9]*\]/ -/;s/And also.../Fun 33th Mon, 9999/"

  4. Pick the correct data: the data we need is beteen text like "Thu 3rd Oct, 2013" and "Fri 4th Oct, 2013". We use sed to get the data between these words. sed -n /START/,/STOP/p gets the data between start and stop. I've plunged in a bunch of regexps to get it to work for all days and months. sed -n "/\S* $(date +%-d)\S* \S*, \d*/,/\S* \d*\S* \S*, \d*/p"

  5. Remove next date: sed also returns the end word (in this case the date of the next day). We remove it by removing the last line: head -n -1


###Note: If you want to use this as startup thing, you might want to cache the data per month first do you don't cause needless traffic.

The data this returnes is property of daysoftheyear.com

This "script" was written with the sole purpose of learning about pipes and sed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment