blog advertising is good for you


blog advertising is good for you
User login

a neat bash one-liner

Just a tiny little one-liner I came up with that I thought I’d share. It works great with GeekTool.

cal | sed "s/^/ /;s/$/ /;s/ $(date +%d) / $(date +%d | sed 's/./#/g') /"

It displays the regular unix calendar, but with the current date replaced by #‘s. Like this:

     December 2005
 Su Mo Tu We Th Fr Sa
              1  2  3
  4  5  6  7  8  9 10
 11 12 13 14 15 16 17
 18 ## 20 21 22 23 24
 25 26 27 28 29 30 31

Nothing big, and I’m sure you already knew how to do this, but you never know Smiling

UPDATE:
Thanks to Eridius, his comment showed me how to work with escape codes! The [[:<:]] thing only seems to work on Mac, and I like bolding rather than reversing, so this one is the new winner:

cal | sed "s/^/ /;s/$/ /;s/ $(date +%d) /$(printf '\e[1m&\e[m')/"

Average rating
(0 votes)
About 31d1

Author Biography

He starts his first programming job next week.

Adam Knight's picture

Very cool. Smiling

And the best hacks use the most common tools in ways that may seem obvious, but weren’t to anyone else before, so keep going. Smiling

cp

Here’s a version that’s a tad simpler:

cal | sed "s/[[:<:]]$(date +%d)[[:>:]]/$(date +%d | sed 's/./#/g')/"

And here’s one that, instead of replacing with ##, replaces with inverted colors. It can be easily adjusted to use some other highlight instead:

cal | sed "s/[[:<:]]$(date +%d)[[:>:]]/$(printf '\e[7m&\e[m')/"</pre>

Note: The purpose of the printf is to embed the escape character.

The [[:<:]] thing only seems to work on my Mac – it throws an error on my Debian box and Cygwin. I’m trying to find it in the documentation, I mean, I can see what it does but haven’t a clue why!

But man, thanks for the escape code knowledge. I was trying to figure this out yesterday and couldn’t for the life of me. \e[ is power!

Odd. the [[:<:]] thing is documented in re_format(7). But I guess I’m not terribly surprised it’s not universal – I had never even heard of this particular character class until I read re_format(7) in search of it (I tried \< and \b first and neither worked, so I read docs).

As for escape codes. You could also use V[ to embed a raw escape code into the line. I just used the printf version so you could copy&paste directly from the web page (since raw escape codes can’t be embedded into webpages).

I had a problem where dates with single digits didn’t work.
For me, the fix was to change the $(date +%d) call to:

$(date +%e)

-HI-

Post new comment
The content of this field is kept private and will not be shown publicly.
1 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.