0

how to Print the calendar for March and April 2015 and show the calendar entries side-by-side? in UNIX

3

This command:

paste <(cal 3 2015 | awk '{printf "%-20s\n",$0}') <(cal 4 2015)

will create this output:

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

Note: The awk subcommand was used to have the last line of the left-side command padded correctly.

Edit: A more compact variant that makes use of a cal feature (option -3) and requires less processes could be:

cal -3 3 2015 | awk '{print substr($0,23)}'
  • But paste <(cal 3 2015) <(cal 4 2015) works for me... – JJoao Mar 20 '15 at 17:44
  • 1
    If various cal commands behave differently; then the variant with the awk padding will work in a wider context of systems. (I wonder why they "optimized" away the trailling blanks in some versions; I think it would be better to have generally a consistent line width with cal.) – Janis Mar 20 '15 at 18:02
1

If your cal supports -A parameter its easy to display more months - you can display current and one future month like this:

cal -A1

This way you can also specify to display other than current month and year by using -m option, for March 2015 it will be:

cal -A1 -m3 2015

But if you can't use -A you can combine two outputs with pr:

pr -t -m <(cal <your_params>) <(cal <your_params>)

or with paste:

paste <(cal <your_params>) <(cal <your_params>)
  • There is no need for any script magic here. – madneon Mar 20 '15 at 20:46
  • Depending the on the cal version that you have on your system the paste variant may require an additional formatting. (See my original answer for the working solution.) – Janis Mar 21 '15 at 16:08
  • @Janis what OS do you use? cal -A1 works fine on my Debian. – terdon Mar 21 '15 at 22:33
  • terdon; my OS (cygwin) is as irrelevant as yours (Debian) for the answers and comments given. (Often questions like yours are followed by a "switch to the latest release" followup and ignoring the substance of the answer.) I'm of course glad your OS supports the simpler syntax. – Janis Mar 21 '15 at 23:39
0
cal -3| awk '{print substr($0,23)}'
  • Is there a badge for copy/pasting an answer that was already given a day ago? – Janis Mar 21 '15 at 16:03
  • @Jains - Looks like you discovered the Linux commands!!! – vipin kumar Mar 23 '15 at 3:29

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.