Showing posts with label sed. Show all posts
Showing posts with label sed. Show all posts

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 'http://tinybuddha.com' | 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

Tuesday, 25 August 2009

More playing...And how to change upper- to lower-case

One of the other things I've wanted to play with for a while is sideways text...Just to broaden the look of Conky that much more!

With the help of Johnny B, I used BlockyCounterClockwise to get the following:
From Screenshots

One of the challenges of this was that this particular font reverses the rotation of letters if you type in captials, so when you run the ${execi 60 date} command, the capitals in the output look upside down! I tried setting the "uupercase yes" config variable, but that just made all the text be the opposite rotation from the numbers!

So I had to figure out how to take any single capitals in the date output and change them to lowercase...After a quick Google search, I found:
sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz

Not the prettiest method, but it works! To use it, simply pipe the date output into it, like so:
${execi 60 date | cut -c5 | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz}

(The cut -c5 here just grabs the first letter of the month name.)

Now all we need are some more interesting sideways fonts :) (No offense of course, to Johnny B.)