Monday, 21 September 2009

How to add a little Zen to your life, and why sed is my new favourite command

I've wanted to add quotes to my Conky for a while, and after coming across some discussions on Ubuntu Forums, I had a bit of a brain-child...There had to be a simple way (e.g. without an external script) to grab part of a website and parse it to display on my screen. I did a little research, and it turns out that a combination of curl and sed was what I needed. With a little help from my friends, I ended up with the following command (after TEXT in my Conky):
${alignr}${execpi 3600 curl --silent '' | sed '{:q;N;s/\n/ /g;t q}'|grep -o '
[^<]*'|sed 's/
[[:blank:]]*//;s/[[:blank:]]*$//'|fold -sw 32 | sed -e 's/^/\${alignr}/' -e 's/ $//g'}

Basically, it uses curl to look at Tiny Buddha, an inspirational website featuring daily quotes. Then, for an explanation of all the sed commands, thanks to mobilediesel:
The first sed changes all newlines into spaces so the div tag will be on one line. Then grep looks for the div class="entry" up to the first < character. Then sed again to take out the div tag along with the spaces following it, then the spaces at the end. Then, obviously, through fold so it fits nicely on the desktop.

Since it's looking specifically for the div class="entry" tag, grep should find it even if the site layout changes.

The --silent suggestion is thanks to Crinos512.

Anyway, here's the result, a lovely minimal desktop, with just a battery bar (because I don't use any other indicators, not even a system tray these days!) and the time/date.
From Screenshots

1 comment:

  1. it doesn't work for me. I just did a copy/paste to my conky.conf and there is no output at all:(