6

I would like to highlight today's date in the output of the cal command. What is the best way?

This is what I have so far:

cal -m | grep -C6 --color "$(date +%e)"

but it doesn't work for all cases e.g, when the date has a single digit. I also want the highlighting to work when I display the calendar for a year.

  • 1
    what specific version of UNIX are you using and what is the output of echo $TERM ? – jamespo Apr 11 '11 at 14:09
  • Red Hat Linux / BSD Cal. – dogbane Apr 11 '11 at 14:20
  • "BSD Cal" is the output of echo $TERM ? – jamespo Apr 11 '11 at 15:21
  • $TERM is linux. – dogbane Apr 11 '11 at 17:10
  • What version of Red Hat and cal, e.g. cat /etc/redhat-release and rpm -q util-linux. This works out of the box on recent versions of Ubuntu and Fedora, so presumably you are using an old version. – Mikel Apr 12 '11 at 2:57
6

I don't know how to highlight the day in the year calendar cal -y with just regular expressions, but the reason your example was not working for single digit dates is because $(date +%e) prepends a space to the output when the date has a single digit.

This will work:

cal | grep --color -EC6 "\b$(date +%e | sed "s/ //g")"
  • 2
    in fish: cal | grep --color -EC6 "\b"(date +%e | sed "s/ //g") – Jorge Bucaran Feb 17 '15 at 1:42
2

On my system (openSUSE 11.4, util-linux-2.19), the current date in output form cal is automatically highlighted (reverse colors) if the output goes to terminal. As per the manpage, this seems to be the default. If it does not work on your system, it might be a bug.

  • There are many variants of cal, for example all three BSDs have different features. This isn't a bug in dogbane's cal, just a different implementation. – Gilles Apr 11 '11 at 19:59
2

The output is already highlighted, so you don't need to highlight it manually. In case you want it to work with grep, you need to disable it:

cal -mh | grep -C6 --color "$(date +%e)"
  • cal | grep -C6 --color "\b$(date +%e)\b" ... need the boundary \b otherwise it will highlight chunks of the year too – slf Oct 14 '14 at 13:21
1

It may be fairly complicated to do something like this;
Why not try something like pal?

0

I think you can use the command date +%-e instead of date +%e to remove the space.

0

For BSD systems without grep --color option:

B=$(tput bold)
U=$(tput sgr0)
DAY=$(date +%e | tr -d " ")
cal | sed -E -e "s|(.*[^0-9])($DAY)([^0-9].*)|\1$B\2$U\3|"

Oneliner:

cal | sed -E -e "s|(.*[^0-9])($(date +%e | tr -d " "))([^0-9].*)|\1$(tput bold)\2$(tput sgr0)\3|"

With week numbers left side:

cal -m -w | sed -E -e "s|(.*) ([[ 0-9]+])$|\2 \1|" -e "1,2s|^|     |" -e "s|(.*[^0-9])($(date +%e | tr -d " "))([^0-9].*)|\1$(tput bold)\2$(tput sgr0)\3|"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.