Home > Back-end >  Bash/How to "cut" the current day with cal?
Bash/How to "cut" the current day with cal?

Time:11-03

I have the following script to highlight the current date in a calendar:

$ cal | grep --color -E6 "\b$(date  %e | sed 's/ //g')\b"

The result:

1

How can I cut a value with current date (output should be just the number of the current day: 2 or 02 in the example)? I need to use the "cal" function. Out:

2

This construction:

$ date  %d

does not satisfy the task condition, unfortunately.

CodePudding user response:

If you just want today's date, why do you need cal? Just use date %e.

But for what ever reason, if you want to only print the matched date with grep, use -o.

$ cal | grep --color=never -o "\b$(date  %e | tr -d ' ' )\b"
2

Or maybe you just want to trim the leading whitespace, in that case just use the date & tr command:

$ date  %e | tr -d ' '
2
  •  Tags:  
  • bash
  • Related