Displaying week's number in certain format using ncal or cal The 2019 Stack Overflow Developer...
Time travel alters history but people keep saying nothing's changed
Where does the "burst of radiance" from Holy Weapon originate?
How to deal with fear of taking dependencies
CiviEvent: Public link for events of a specific type
Inflated grade on resume at previous job, might former employer tell new employer?
Springs with some finite mass
Output the Arecibo Message
Should I write numbers in words or as numerals when there are multiple next to each other?
What does "sndry explns" mean in one of the Hitchhiker's guide books?
Is flight data recorder erased after every flight?
What do hard-Brexiteers want with respect to the Irish border?
Unbreakable Formation vs. Cry of the Carnarium
Spanish for "widget"
Pristine Bit Checking
What is this 4-propeller plane?
What is the meaning of Triage in Cybersec world?
How come people say “Would of”?
Is domain driven design an anti-SQL pattern?
In microwave frequencies, do you use a circulator when you need a (near) perfect diode?
Falsification in Math vs Science
What effect does the “loading” weapon property have in practical terms?
Are there any other methods to apply to solving simultaneous equations?
How was Skylab's orbit inclination chosen?
Where to refill my bottle in India?
Displaying week's number in certain format using ncal or cal
The 2019 Stack Overflow Developer Survey Results Are InA Calendar/Scheduling applicationls -l --group-directories-first (act on symlinks as well)gzip files with cat and from pipe gives different resultsDisplay Set Number of Months with calCalendar to Cal displaying scheduleGet previous week's number in bashHow can I calculate and format a date duration using GNU tools, not a result date?Does `hash -p` change the pathname of a external command name?Terminal output not displayed for certain commandsHow to automatically scroll to requested command when the “General Commands Manual” is shown?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Don't you just love it when two commands each do one thing you want but neither do both?
This is what cal
does. Nice formatting. Lacks week numbers though:
$ cal
January 2012
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 19 20 21
22 23 24 25 26 27 28
29 30 31
This is what ncal
does. Weird formatting, but with week numbers:
$ ncal -w
January 2012
Su 1 8 15 22 29
Mo 2 9 16 23 30
Tu 3 10 17 24 31
We 4 11 18 25
Th 5 12 19 26
Fr 6 13 20 27
Sa 7 14 21 28
1 2 3 4 5
The kind of output I want, actually a crossbreed between cal
and ncal -w
:
$ cal --magik-calendar-week-option
January 2012
Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 6 7
2 8 9 10 11 12 13 14
3 15 16 17 18 19 20 21
4 22 23 24 25 26 27 28
5 29 30 31
bash command-line terminal calendar
add a comment |
Don't you just love it when two commands each do one thing you want but neither do both?
This is what cal
does. Nice formatting. Lacks week numbers though:
$ cal
January 2012
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 19 20 21
22 23 24 25 26 27 28
29 30 31
This is what ncal
does. Weird formatting, but with week numbers:
$ ncal -w
January 2012
Su 1 8 15 22 29
Mo 2 9 16 23 30
Tu 3 10 17 24 31
We 4 11 18 25
Th 5 12 19 26
Fr 6 13 20 27
Sa 7 14 21 28
1 2 3 4 5
The kind of output I want, actually a crossbreed between cal
and ncal -w
:
$ cal --magik-calendar-week-option
January 2012
Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 6 7
2 8 9 10 11 12 13 14
3 15 16 17 18 19 20 21
4 22 23 24 25 26 27 28
5 29 30 31
bash command-line terminal calendar
add a comment |
Don't you just love it when two commands each do one thing you want but neither do both?
This is what cal
does. Nice formatting. Lacks week numbers though:
$ cal
January 2012
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 19 20 21
22 23 24 25 26 27 28
29 30 31
This is what ncal
does. Weird formatting, but with week numbers:
$ ncal -w
January 2012
Su 1 8 15 22 29
Mo 2 9 16 23 30
Tu 3 10 17 24 31
We 4 11 18 25
Th 5 12 19 26
Fr 6 13 20 27
Sa 7 14 21 28
1 2 3 4 5
The kind of output I want, actually a crossbreed between cal
and ncal -w
:
$ cal --magik-calendar-week-option
January 2012
Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 6 7
2 8 9 10 11 12 13 14
3 15 16 17 18 19 20 21
4 22 23 24 25 26 27 28
5 29 30 31
bash command-line terminal calendar
Don't you just love it when two commands each do one thing you want but neither do both?
This is what cal
does. Nice formatting. Lacks week numbers though:
$ cal
January 2012
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 19 20 21
22 23 24 25 26 27 28
29 30 31
This is what ncal
does. Weird formatting, but with week numbers:
$ ncal -w
January 2012
Su 1 8 15 22 29
Mo 2 9 16 23 30
Tu 3 10 17 24 31
We 4 11 18 25
Th 5 12 19 26
Fr 6 13 20 27
Sa 7 14 21 28
1 2 3 4 5
The kind of output I want, actually a crossbreed between cal
and ncal -w
:
$ cal --magik-calendar-week-option
January 2012
Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 6 7
2 8 9 10 11 12 13 14
3 15 16 17 18 19 20 21
4 22 23 24 25 26 27 28
5 29 30 31
bash command-line terminal calendar
bash command-line terminal calendar
edited Nov 30 '16 at 15:04


bishop
2,1362923
2,1362923
asked Jan 19 '12 at 9:53
k0pernikusk0pernikus
4,467154567
4,467154567
add a comment |
add a comment |
9 Answers
9
active
oldest
votes
If neither of these commands suit your needs you can use gcal
to do what you want instead.
Example
$ gcal -K
April 2014
Su Mo Tu We Th Fr Sa CW
1 2 3 4 5 13
6 7 8 9 10 11 12 14
13 14 15 16 17 18 19 15
20 21 22 23 24 25 26 16
27 28 29 30 17
Prints the week number in the last column to the right.
References
- Gcal the ultra-powerful command line GNU calendar
- Gcal user's manual
- The many uses of gcal
gcal --starting-day=Monday --with-week-number
fits my needs more yet this tool is great.
– k0pernikus
Dec 21 '16 at 10:48
add a comment |
This highlights today's date, and can display any month via $1
in the form: YYYY-mm-dd
... It defaults to today's date
It is set up to show ISO week numbers, and the first weekday being Monday.
#!/bin/bash
# Input reference date is expected in 'YYYY-mm-dd' format
#
today=($(date '+%Y %m %d')); Y=0; m=1; d=2 # establish today's date
[[ -z $1 ]] && ref=(${today[@]}) || ref=(${1//-/ }) # get input date
dNbA=$(date --date="$(date +%Y-%m-01)" +'%u') # day-number of 1st day of reference month
today[m]=$((10#${today[m]})); ref[m]=$((10#${ref[m]})) # remove leading zero (octal clash)
today[d]=$((10#${today[d]})); ref[d]=$((10#${ref[d]})) # remove leading zero (octal clash)
nxtm=$(( ref[m]==12 ?1 :ref[m]+1 )) # month-number of next month
nxtY=$(( ref[m]==12 ?ref[Y]+1:ref[Y] )) # year-number of next month
nxtA="$nxtY-$nxtm-1" # date of 1st day of next month
refZ=$(date --date "$(date +$nxtA) yesterday" +%Y-%m-%d) # date of last day of reference month
days=$(date --date="$refZ" '+%d') # days in reference month
h1="$(date --date="${ref[Y]}-${ref[m]}-${ref[d]}" '+%B %Y')" # header 1
h2="Mo Tu We Th Fr Sa Su" # header 2
printf " %$(((${#h2}-${#h1}-1)/2))s%sn" " " "$h1"
printf " %sn" "$h2"
# print week rows
printf "%2d " "$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-01)" +'%V')))" # week-number (of year) with suppressed leading 0
printf "%$(((dNbA-1)*3))s" # lead spaces (before start of month)
dNbW=$dNbA # day-number of week
dNbM=1 # day-number of month
while ((dNbM <= days)) ;do
if (( today[Y]==ref[Y] &&
today[m]==ref[m] &&
today[d]==dNbM )) ;then
printf "x1b[7m%2dx1b[0m " "$dNbM" # highlight today's date
else
printf "%2d " "$dNbM"
fi
((dNbM++))
if ((dNbW >=7)) ;then
cdate=$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-$dNbM)" +'%V'))) # remove leading zero (octal clash)
printf "n%2d " "$cdate" # week-number of year
dNbW=0
fi
((dNbW++))
done
printf "%$(((8-dNbW)*3))sn" # trailing spaces (after end of month)
Here is this month's display (with 20
highlighted)
January 2012
Mo Tu We Th Fr Sa Su
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
I didn't even notice the iso-issue. That will do :)
– k0pernikus
Jan 20 '12 at 11:18
1
@k0pernikus: I've just modified the script.. (about 35 minutes after you posted the above comment).. It was getting caught up on08
and09
being interpreted as octal instead of decimal .. It should be fine now...
– Peter.O
Jan 20 '12 at 12:05
add a comment |
You can use nl
to number the lines (that's the program purpose :). But you need to extract the first week in the month from somewhere. It can be done from ncal
itself:
$ ncal -w 2 2012 | tail -1 | awk '{print $1}'
5
We insert this as a parameter to nl
's option -v
(starting line number), and tell it to only number lines with numbers or spaces.
$ cal 2 2012 | nl -bp'^[0-9 ]+$' -w2 -s' ' -v$(ncal -w 2 2012 | tail -1 | awk '{print $1}')
February 2012
Su Mo Tu We Th Fr Sa
5 1 2 3 4
6 5 6 7 8 9 10 11
7 12 13 14 15 16 17 18
8 19 20 21 22 23 24 25
9 26 27 28 29
This is all awfully fragile though. Anyway, if you aren't going to need cal
's more advanced options, it will work. You can put it in a file and replace "$@"
where I put 2 2012
.
EDIT: But this is WRONG! I just noticed that the first week in January can have number 52 or 53! So we just either have to make an exception for January, or just extract all the week numbers from ncal
and apply them to the output of cal
.
This is the solution I thought originally, but I thought (erroneously) I would simplify it using nl
. It uses paste
, which merges files side-by-side. Since there isn't any file, we have to use the bashism <(...)
; that's what I was trying to avoid.
Our first "file" will be a list of the week numbers, with two empty lines at the beginning:
$ printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)
52
1
2
3
4
5
The second one, just the output of cal
. All together, as parameters to paste
:
$ paste -d' ' <(printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)) <(cal 1 2011)
January 2011
Su Mo Tu We Th Fr Sa
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
Much messier and incompatible that the other one. En fin...
Not so great :-/. See my edit.
– angus
Jan 19 '12 at 12:01
add a comment |
Generate the weeks sequence with ncal
and use paste
for having both outputs side by side.
$ paste <(echo; echo; ncal -w | tail -1 | xargs -n1 printf '%2dn') <(cal)
If you don't like having tabs as delimiters just add something like sed 's/t/ /'
Edit: way simpler, no need to care about tabs:
$ paste -d' ' <((echo -n ' '; ncal -w | tail -1 )| fold -w 3) <(cal)
Elegant and embodies many of ESR's 17 Unix Rules. Should be the accepted answer as it truly combines the output ofcal
andncal
(other approaches replicate behavior of eithercal
orncal
).
– bishop
Nov 30 '16 at 14:53
add a comment |
One way using Perl (my output language of the cal
command is Spanish, but I hope result doesn't vary from English):
$ cal | perl -pe 'if ( m/As*d/ ) { s/A/++$i . qq[ ] x 2/e } else { s/A/qq[ ] x 3/e }'
Output:
enero de 2012
lu ma mi ju vi sá do
1 1
2 2 3 4 5 6 7 8
3 9 10 11 12 13 14 15
4 16 17 18 19 20 21 22
5 23 24 25 26 27 28 29
6 30 31
Explanation:
-pe # For every input line from previous pipe, execute next
# instructions and print to output.
if ( m/As*d/ ) # If line begins with a digit omitting spaces...
s/A/++$i . qq[ ] x 2/e # Insert at the beginning of the line a counter plus two spaces.
else # else...
s/A/qq[ ] x 3/e # Insert three spaces at the beginning of the line.
how will this work for subsequent months. February weeks do not start from again 1, they start from either 5/6 similarly March will be from 9/10.
– Nikhil Mulley
Jan 19 '12 at 10:41
cal 02 2012, will it have this failed??
– Nikhil Mulley
Jan 19 '12 at 10:42
@Nikhil: I don't understand what you mean. Can you try to explain it more in depth? Does the script fail withcal 02 2012
? It seemed to work in my test.
– Birei
Jan 19 '12 at 10:52
@Nikhil: Ah, ok. I missunderstood the question. It means absolute week numbers and not relative to each month. I will delete my wrong answer in a while.
– Birei
Jan 19 '12 at 10:55
1
cool..do not delete the answer, please keep it for reference.
– Nikhil Mulley
Jan 19 '12 at 11:09
add a comment |
Without enough rep to comment on the answer by slm, gcal can be installed on Mac OS using brew install gcal
.
(I found the answer here first, then this answer on ask different, but the unix answer lacked all the information I needed to install on the mac.)
add a comment |
Here is my solution which is shorter in code and works fine for other dates than the current month.
Sorry for the US people, this uses ISO format, i.e. 1st day of week is monday.
This is done with the -m option for cal and the %V option for date
#!/bin/bash
if [ "$1" = "" ]
then
when="now"
else
when="$1"
fi
y=$(date --date "$when" +%Y )
if [ $? -ne 0 ]
then
exit
fi
m=$(date --date "$when" +%m )
w=$(date --date $(printf %4d%02d01 $y $m) +%V)
cal -m $m $y |
awk -v w=$w '
NR>2{ww=w++}
$0!=""{printf "%2s %sn", ww , $0}
'
add a comment |
I did this script to generate on-duty calendar for teams. It generates calendar with week numbers and assigning names to them (simply round-robin). After output is printed you just need to copy it to excel and do "text to column" by ; and once again perform "text to column" on first column by using "fixed" option. The benefit of this calendar (comparing with horizontal orientation) is that you can use auto-filter (Alt+d f f) and find only your weeks.
#!/usr/bin/ksh
y=2017
set -A team1 Petars Justas Richard Jukka Jesper
set -A team2 Modestas Timo Mats Andreas
i1=0
i2=0
wn=1
own=1
echo "WN Month Mo Tu We Th Fr Sa Su ;Team1 ;Team2"
ycal=`for m in 1 2 3 4 5 6 7 8 9 10 11 12
do
IFS=
cal -m $m $y|egrep -v "Mo|^$"|while read line
do
echo $line|grep -q $y && mname=$line || print $mname $line|sed 's/'$y'//'
done
done`
IFS=
echo "$ycal"|while read line2
do
IFS=
echo "$line2"|grep -v "1 2 3 4 5 6 7"|egrep -q "* 1 |* 1$" || let wn++
[ $own -ne $wn ] && {
[ $i1 -eq ${#team1[@]}-1 ] && i1=0 || let i1++;
[ $i2 -eq ${#team2[@]}-1 ] && i2=0 || let i2++; }
printf '%2s %s ;%s ;%sn' $wn $line2 ${team1[$i1]} ${team2[$i2]}
own=$wn
done
Sorry, code a bit nasty... and no comments... but it works well ;)
add a comment |
I don't really want to install gcal
, and I could not get it to work properly on my system. So without gcal, funollet's answer is indeed the best solution. I just changed it a tiny bit so that it also preserves formatting such as day highlight, colors etc. using the script
command. I also removed redundant characters, making it suitable as an alias, as such:
alias today="paste -d' ' <((echo ' '{,};ncal -w|tail -1)|fold -w3) <(script /dev/null -qc cal)"
Or replace cal
with a new function, like so:
function cal {
paste -d' ' <((echo ' '{,};ncal -w $@|tail -1)|fold -w3) <(script /dev/null -qc "cal $@")
}
However, with this you cannot do things like cal -3
. You get the correct weeks but they are not aligned properly.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f29434%2fdisplaying-weeks-number-in-certain-format-using-ncal-or-cal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
If neither of these commands suit your needs you can use gcal
to do what you want instead.
Example
$ gcal -K
April 2014
Su Mo Tu We Th Fr Sa CW
1 2 3 4 5 13
6 7 8 9 10 11 12 14
13 14 15 16 17 18 19 15
20 21 22 23 24 25 26 16
27 28 29 30 17
Prints the week number in the last column to the right.
References
- Gcal the ultra-powerful command line GNU calendar
- Gcal user's manual
- The many uses of gcal
gcal --starting-day=Monday --with-week-number
fits my needs more yet this tool is great.
– k0pernikus
Dec 21 '16 at 10:48
add a comment |
If neither of these commands suit your needs you can use gcal
to do what you want instead.
Example
$ gcal -K
April 2014
Su Mo Tu We Th Fr Sa CW
1 2 3 4 5 13
6 7 8 9 10 11 12 14
13 14 15 16 17 18 19 15
20 21 22 23 24 25 26 16
27 28 29 30 17
Prints the week number in the last column to the right.
References
- Gcal the ultra-powerful command line GNU calendar
- Gcal user's manual
- The many uses of gcal
gcal --starting-day=Monday --with-week-number
fits my needs more yet this tool is great.
– k0pernikus
Dec 21 '16 at 10:48
add a comment |
If neither of these commands suit your needs you can use gcal
to do what you want instead.
Example
$ gcal -K
April 2014
Su Mo Tu We Th Fr Sa CW
1 2 3 4 5 13
6 7 8 9 10 11 12 14
13 14 15 16 17 18 19 15
20 21 22 23 24 25 26 16
27 28 29 30 17
Prints the week number in the last column to the right.
References
- Gcal the ultra-powerful command line GNU calendar
- Gcal user's manual
- The many uses of gcal
If neither of these commands suit your needs you can use gcal
to do what you want instead.
Example
$ gcal -K
April 2014
Su Mo Tu We Th Fr Sa CW
1 2 3 4 5 13
6 7 8 9 10 11 12 14
13 14 15 16 17 18 19 15
20 21 22 23 24 25 26 16
27 28 29 30 17
Prints the week number in the last column to the right.
References
- Gcal the ultra-powerful command line GNU calendar
- Gcal user's manual
- The many uses of gcal
answered Apr 28 '14 at 16:31
slm♦slm
256k71541687
256k71541687
gcal --starting-day=Monday --with-week-number
fits my needs more yet this tool is great.
– k0pernikus
Dec 21 '16 at 10:48
add a comment |
gcal --starting-day=Monday --with-week-number
fits my needs more yet this tool is great.
– k0pernikus
Dec 21 '16 at 10:48
gcal --starting-day=Monday --with-week-number
fits my needs more yet this tool is great.– k0pernikus
Dec 21 '16 at 10:48
gcal --starting-day=Monday --with-week-number
fits my needs more yet this tool is great.– k0pernikus
Dec 21 '16 at 10:48
add a comment |
This highlights today's date, and can display any month via $1
in the form: YYYY-mm-dd
... It defaults to today's date
It is set up to show ISO week numbers, and the first weekday being Monday.
#!/bin/bash
# Input reference date is expected in 'YYYY-mm-dd' format
#
today=($(date '+%Y %m %d')); Y=0; m=1; d=2 # establish today's date
[[ -z $1 ]] && ref=(${today[@]}) || ref=(${1//-/ }) # get input date
dNbA=$(date --date="$(date +%Y-%m-01)" +'%u') # day-number of 1st day of reference month
today[m]=$((10#${today[m]})); ref[m]=$((10#${ref[m]})) # remove leading zero (octal clash)
today[d]=$((10#${today[d]})); ref[d]=$((10#${ref[d]})) # remove leading zero (octal clash)
nxtm=$(( ref[m]==12 ?1 :ref[m]+1 )) # month-number of next month
nxtY=$(( ref[m]==12 ?ref[Y]+1:ref[Y] )) # year-number of next month
nxtA="$nxtY-$nxtm-1" # date of 1st day of next month
refZ=$(date --date "$(date +$nxtA) yesterday" +%Y-%m-%d) # date of last day of reference month
days=$(date --date="$refZ" '+%d') # days in reference month
h1="$(date --date="${ref[Y]}-${ref[m]}-${ref[d]}" '+%B %Y')" # header 1
h2="Mo Tu We Th Fr Sa Su" # header 2
printf " %$(((${#h2}-${#h1}-1)/2))s%sn" " " "$h1"
printf " %sn" "$h2"
# print week rows
printf "%2d " "$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-01)" +'%V')))" # week-number (of year) with suppressed leading 0
printf "%$(((dNbA-1)*3))s" # lead spaces (before start of month)
dNbW=$dNbA # day-number of week
dNbM=1 # day-number of month
while ((dNbM <= days)) ;do
if (( today[Y]==ref[Y] &&
today[m]==ref[m] &&
today[d]==dNbM )) ;then
printf "x1b[7m%2dx1b[0m " "$dNbM" # highlight today's date
else
printf "%2d " "$dNbM"
fi
((dNbM++))
if ((dNbW >=7)) ;then
cdate=$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-$dNbM)" +'%V'))) # remove leading zero (octal clash)
printf "n%2d " "$cdate" # week-number of year
dNbW=0
fi
((dNbW++))
done
printf "%$(((8-dNbW)*3))sn" # trailing spaces (after end of month)
Here is this month's display (with 20
highlighted)
January 2012
Mo Tu We Th Fr Sa Su
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
I didn't even notice the iso-issue. That will do :)
– k0pernikus
Jan 20 '12 at 11:18
1
@k0pernikus: I've just modified the script.. (about 35 minutes after you posted the above comment).. It was getting caught up on08
and09
being interpreted as octal instead of decimal .. It should be fine now...
– Peter.O
Jan 20 '12 at 12:05
add a comment |
This highlights today's date, and can display any month via $1
in the form: YYYY-mm-dd
... It defaults to today's date
It is set up to show ISO week numbers, and the first weekday being Monday.
#!/bin/bash
# Input reference date is expected in 'YYYY-mm-dd' format
#
today=($(date '+%Y %m %d')); Y=0; m=1; d=2 # establish today's date
[[ -z $1 ]] && ref=(${today[@]}) || ref=(${1//-/ }) # get input date
dNbA=$(date --date="$(date +%Y-%m-01)" +'%u') # day-number of 1st day of reference month
today[m]=$((10#${today[m]})); ref[m]=$((10#${ref[m]})) # remove leading zero (octal clash)
today[d]=$((10#${today[d]})); ref[d]=$((10#${ref[d]})) # remove leading zero (octal clash)
nxtm=$(( ref[m]==12 ?1 :ref[m]+1 )) # month-number of next month
nxtY=$(( ref[m]==12 ?ref[Y]+1:ref[Y] )) # year-number of next month
nxtA="$nxtY-$nxtm-1" # date of 1st day of next month
refZ=$(date --date "$(date +$nxtA) yesterday" +%Y-%m-%d) # date of last day of reference month
days=$(date --date="$refZ" '+%d') # days in reference month
h1="$(date --date="${ref[Y]}-${ref[m]}-${ref[d]}" '+%B %Y')" # header 1
h2="Mo Tu We Th Fr Sa Su" # header 2
printf " %$(((${#h2}-${#h1}-1)/2))s%sn" " " "$h1"
printf " %sn" "$h2"
# print week rows
printf "%2d " "$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-01)" +'%V')))" # week-number (of year) with suppressed leading 0
printf "%$(((dNbA-1)*3))s" # lead spaces (before start of month)
dNbW=$dNbA # day-number of week
dNbM=1 # day-number of month
while ((dNbM <= days)) ;do
if (( today[Y]==ref[Y] &&
today[m]==ref[m] &&
today[d]==dNbM )) ;then
printf "x1b[7m%2dx1b[0m " "$dNbM" # highlight today's date
else
printf "%2d " "$dNbM"
fi
((dNbM++))
if ((dNbW >=7)) ;then
cdate=$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-$dNbM)" +'%V'))) # remove leading zero (octal clash)
printf "n%2d " "$cdate" # week-number of year
dNbW=0
fi
((dNbW++))
done
printf "%$(((8-dNbW)*3))sn" # trailing spaces (after end of month)
Here is this month's display (with 20
highlighted)
January 2012
Mo Tu We Th Fr Sa Su
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
I didn't even notice the iso-issue. That will do :)
– k0pernikus
Jan 20 '12 at 11:18
1
@k0pernikus: I've just modified the script.. (about 35 minutes after you posted the above comment).. It was getting caught up on08
and09
being interpreted as octal instead of decimal .. It should be fine now...
– Peter.O
Jan 20 '12 at 12:05
add a comment |
This highlights today's date, and can display any month via $1
in the form: YYYY-mm-dd
... It defaults to today's date
It is set up to show ISO week numbers, and the first weekday being Monday.
#!/bin/bash
# Input reference date is expected in 'YYYY-mm-dd' format
#
today=($(date '+%Y %m %d')); Y=0; m=1; d=2 # establish today's date
[[ -z $1 ]] && ref=(${today[@]}) || ref=(${1//-/ }) # get input date
dNbA=$(date --date="$(date +%Y-%m-01)" +'%u') # day-number of 1st day of reference month
today[m]=$((10#${today[m]})); ref[m]=$((10#${ref[m]})) # remove leading zero (octal clash)
today[d]=$((10#${today[d]})); ref[d]=$((10#${ref[d]})) # remove leading zero (octal clash)
nxtm=$(( ref[m]==12 ?1 :ref[m]+1 )) # month-number of next month
nxtY=$(( ref[m]==12 ?ref[Y]+1:ref[Y] )) # year-number of next month
nxtA="$nxtY-$nxtm-1" # date of 1st day of next month
refZ=$(date --date "$(date +$nxtA) yesterday" +%Y-%m-%d) # date of last day of reference month
days=$(date --date="$refZ" '+%d') # days in reference month
h1="$(date --date="${ref[Y]}-${ref[m]}-${ref[d]}" '+%B %Y')" # header 1
h2="Mo Tu We Th Fr Sa Su" # header 2
printf " %$(((${#h2}-${#h1}-1)/2))s%sn" " " "$h1"
printf " %sn" "$h2"
# print week rows
printf "%2d " "$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-01)" +'%V')))" # week-number (of year) with suppressed leading 0
printf "%$(((dNbA-1)*3))s" # lead spaces (before start of month)
dNbW=$dNbA # day-number of week
dNbM=1 # day-number of month
while ((dNbM <= days)) ;do
if (( today[Y]==ref[Y] &&
today[m]==ref[m] &&
today[d]==dNbM )) ;then
printf "x1b[7m%2dx1b[0m " "$dNbM" # highlight today's date
else
printf "%2d " "$dNbM"
fi
((dNbM++))
if ((dNbW >=7)) ;then
cdate=$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-$dNbM)" +'%V'))) # remove leading zero (octal clash)
printf "n%2d " "$cdate" # week-number of year
dNbW=0
fi
((dNbW++))
done
printf "%$(((8-dNbW)*3))sn" # trailing spaces (after end of month)
Here is this month's display (with 20
highlighted)
January 2012
Mo Tu We Th Fr Sa Su
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
This highlights today's date, and can display any month via $1
in the form: YYYY-mm-dd
... It defaults to today's date
It is set up to show ISO week numbers, and the first weekday being Monday.
#!/bin/bash
# Input reference date is expected in 'YYYY-mm-dd' format
#
today=($(date '+%Y %m %d')); Y=0; m=1; d=2 # establish today's date
[[ -z $1 ]] && ref=(${today[@]}) || ref=(${1//-/ }) # get input date
dNbA=$(date --date="$(date +%Y-%m-01)" +'%u') # day-number of 1st day of reference month
today[m]=$((10#${today[m]})); ref[m]=$((10#${ref[m]})) # remove leading zero (octal clash)
today[d]=$((10#${today[d]})); ref[d]=$((10#${ref[d]})) # remove leading zero (octal clash)
nxtm=$(( ref[m]==12 ?1 :ref[m]+1 )) # month-number of next month
nxtY=$(( ref[m]==12 ?ref[Y]+1:ref[Y] )) # year-number of next month
nxtA="$nxtY-$nxtm-1" # date of 1st day of next month
refZ=$(date --date "$(date +$nxtA) yesterday" +%Y-%m-%d) # date of last day of reference month
days=$(date --date="$refZ" '+%d') # days in reference month
h1="$(date --date="${ref[Y]}-${ref[m]}-${ref[d]}" '+%B %Y')" # header 1
h2="Mo Tu We Th Fr Sa Su" # header 2
printf " %$(((${#h2}-${#h1}-1)/2))s%sn" " " "$h1"
printf " %sn" "$h2"
# print week rows
printf "%2d " "$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-01)" +'%V')))" # week-number (of year) with suppressed leading 0
printf "%$(((dNbA-1)*3))s" # lead spaces (before start of month)
dNbW=$dNbA # day-number of week
dNbM=1 # day-number of month
while ((dNbM <= days)) ;do
if (( today[Y]==ref[Y] &&
today[m]==ref[m] &&
today[d]==dNbM )) ;then
printf "x1b[7m%2dx1b[0m " "$dNbM" # highlight today's date
else
printf "%2d " "$dNbM"
fi
((dNbM++))
if ((dNbW >=7)) ;then
cdate=$((10#$(date -d "$(date +${ref[Y]}-${ref[m]}-$dNbM)" +'%V'))) # remove leading zero (octal clash)
printf "n%2d " "$cdate" # week-number of year
dNbW=0
fi
((dNbW++))
done
printf "%$(((8-dNbW)*3))sn" # trailing spaces (after end of month)
Here is this month's display (with 20
highlighted)
January 2012
Mo Tu We Th Fr Sa Su
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
edited Mar 12 '15 at 9:36


Gunstick
32
32
answered Jan 19 '12 at 11:30
Peter.OPeter.O
19.2k1891146
19.2k1891146
I didn't even notice the iso-issue. That will do :)
– k0pernikus
Jan 20 '12 at 11:18
1
@k0pernikus: I've just modified the script.. (about 35 minutes after you posted the above comment).. It was getting caught up on08
and09
being interpreted as octal instead of decimal .. It should be fine now...
– Peter.O
Jan 20 '12 at 12:05
add a comment |
I didn't even notice the iso-issue. That will do :)
– k0pernikus
Jan 20 '12 at 11:18
1
@k0pernikus: I've just modified the script.. (about 35 minutes after you posted the above comment).. It was getting caught up on08
and09
being interpreted as octal instead of decimal .. It should be fine now...
– Peter.O
Jan 20 '12 at 12:05
I didn't even notice the iso-issue. That will do :)
– k0pernikus
Jan 20 '12 at 11:18
I didn't even notice the iso-issue. That will do :)
– k0pernikus
Jan 20 '12 at 11:18
1
1
@k0pernikus: I've just modified the script.. (about 35 minutes after you posted the above comment).. It was getting caught up on
08
and 09
being interpreted as octal instead of decimal .. It should be fine now...– Peter.O
Jan 20 '12 at 12:05
@k0pernikus: I've just modified the script.. (about 35 minutes after you posted the above comment).. It was getting caught up on
08
and 09
being interpreted as octal instead of decimal .. It should be fine now...– Peter.O
Jan 20 '12 at 12:05
add a comment |
You can use nl
to number the lines (that's the program purpose :). But you need to extract the first week in the month from somewhere. It can be done from ncal
itself:
$ ncal -w 2 2012 | tail -1 | awk '{print $1}'
5
We insert this as a parameter to nl
's option -v
(starting line number), and tell it to only number lines with numbers or spaces.
$ cal 2 2012 | nl -bp'^[0-9 ]+$' -w2 -s' ' -v$(ncal -w 2 2012 | tail -1 | awk '{print $1}')
February 2012
Su Mo Tu We Th Fr Sa
5 1 2 3 4
6 5 6 7 8 9 10 11
7 12 13 14 15 16 17 18
8 19 20 21 22 23 24 25
9 26 27 28 29
This is all awfully fragile though. Anyway, if you aren't going to need cal
's more advanced options, it will work. You can put it in a file and replace "$@"
where I put 2 2012
.
EDIT: But this is WRONG! I just noticed that the first week in January can have number 52 or 53! So we just either have to make an exception for January, or just extract all the week numbers from ncal
and apply them to the output of cal
.
This is the solution I thought originally, but I thought (erroneously) I would simplify it using nl
. It uses paste
, which merges files side-by-side. Since there isn't any file, we have to use the bashism <(...)
; that's what I was trying to avoid.
Our first "file" will be a list of the week numbers, with two empty lines at the beginning:
$ printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)
52
1
2
3
4
5
The second one, just the output of cal
. All together, as parameters to paste
:
$ paste -d' ' <(printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)) <(cal 1 2011)
January 2011
Su Mo Tu We Th Fr Sa
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
Much messier and incompatible that the other one. En fin...
Not so great :-/. See my edit.
– angus
Jan 19 '12 at 12:01
add a comment |
You can use nl
to number the lines (that's the program purpose :). But you need to extract the first week in the month from somewhere. It can be done from ncal
itself:
$ ncal -w 2 2012 | tail -1 | awk '{print $1}'
5
We insert this as a parameter to nl
's option -v
(starting line number), and tell it to only number lines with numbers or spaces.
$ cal 2 2012 | nl -bp'^[0-9 ]+$' -w2 -s' ' -v$(ncal -w 2 2012 | tail -1 | awk '{print $1}')
February 2012
Su Mo Tu We Th Fr Sa
5 1 2 3 4
6 5 6 7 8 9 10 11
7 12 13 14 15 16 17 18
8 19 20 21 22 23 24 25
9 26 27 28 29
This is all awfully fragile though. Anyway, if you aren't going to need cal
's more advanced options, it will work. You can put it in a file and replace "$@"
where I put 2 2012
.
EDIT: But this is WRONG! I just noticed that the first week in January can have number 52 or 53! So we just either have to make an exception for January, or just extract all the week numbers from ncal
and apply them to the output of cal
.
This is the solution I thought originally, but I thought (erroneously) I would simplify it using nl
. It uses paste
, which merges files side-by-side. Since there isn't any file, we have to use the bashism <(...)
; that's what I was trying to avoid.
Our first "file" will be a list of the week numbers, with two empty lines at the beginning:
$ printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)
52
1
2
3
4
5
The second one, just the output of cal
. All together, as parameters to paste
:
$ paste -d' ' <(printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)) <(cal 1 2011)
January 2011
Su Mo Tu We Th Fr Sa
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
Much messier and incompatible that the other one. En fin...
Not so great :-/. See my edit.
– angus
Jan 19 '12 at 12:01
add a comment |
You can use nl
to number the lines (that's the program purpose :). But you need to extract the first week in the month from somewhere. It can be done from ncal
itself:
$ ncal -w 2 2012 | tail -1 | awk '{print $1}'
5
We insert this as a parameter to nl
's option -v
(starting line number), and tell it to only number lines with numbers or spaces.
$ cal 2 2012 | nl -bp'^[0-9 ]+$' -w2 -s' ' -v$(ncal -w 2 2012 | tail -1 | awk '{print $1}')
February 2012
Su Mo Tu We Th Fr Sa
5 1 2 3 4
6 5 6 7 8 9 10 11
7 12 13 14 15 16 17 18
8 19 20 21 22 23 24 25
9 26 27 28 29
This is all awfully fragile though. Anyway, if you aren't going to need cal
's more advanced options, it will work. You can put it in a file and replace "$@"
where I put 2 2012
.
EDIT: But this is WRONG! I just noticed that the first week in January can have number 52 or 53! So we just either have to make an exception for January, or just extract all the week numbers from ncal
and apply them to the output of cal
.
This is the solution I thought originally, but I thought (erroneously) I would simplify it using nl
. It uses paste
, which merges files side-by-side. Since there isn't any file, we have to use the bashism <(...)
; that's what I was trying to avoid.
Our first "file" will be a list of the week numbers, with two empty lines at the beginning:
$ printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)
52
1
2
3
4
5
The second one, just the output of cal
. All together, as parameters to paste
:
$ paste -d' ' <(printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)) <(cal 1 2011)
January 2011
Su Mo Tu We Th Fr Sa
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
Much messier and incompatible that the other one. En fin...
You can use nl
to number the lines (that's the program purpose :). But you need to extract the first week in the month from somewhere. It can be done from ncal
itself:
$ ncal -w 2 2012 | tail -1 | awk '{print $1}'
5
We insert this as a parameter to nl
's option -v
(starting line number), and tell it to only number lines with numbers or spaces.
$ cal 2 2012 | nl -bp'^[0-9 ]+$' -w2 -s' ' -v$(ncal -w 2 2012 | tail -1 | awk '{print $1}')
February 2012
Su Mo Tu We Th Fr Sa
5 1 2 3 4
6 5 6 7 8 9 10 11
7 12 13 14 15 16 17 18
8 19 20 21 22 23 24 25
9 26 27 28 29
This is all awfully fragile though. Anyway, if you aren't going to need cal
's more advanced options, it will work. You can put it in a file and replace "$@"
where I put 2 2012
.
EDIT: But this is WRONG! I just noticed that the first week in January can have number 52 or 53! So we just either have to make an exception for January, or just extract all the week numbers from ncal
and apply them to the output of cal
.
This is the solution I thought originally, but I thought (erroneously) I would simplify it using nl
. It uses paste
, which merges files side-by-side. Since there isn't any file, we have to use the bashism <(...)
; that's what I was trying to avoid.
Our first "file" will be a list of the week numbers, with two empty lines at the beginning:
$ printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)
52
1
2
3
4
5
The second one, just the output of cal
. All together, as parameters to paste
:
$ paste -d' ' <(printf ' n n' && printf '%2d n' $(ncal -w 1 2011 | tail -1)) <(cal 1 2011)
January 2011
Su Mo Tu We Th Fr Sa
52 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29
5 30 31
Much messier and incompatible that the other one. En fin...
edited Jan 19 '12 at 12:00
answered Jan 19 '12 at 10:52
angusangus
9,37123432
9,37123432
Not so great :-/. See my edit.
– angus
Jan 19 '12 at 12:01
add a comment |
Not so great :-/. See my edit.
– angus
Jan 19 '12 at 12:01
Not so great :-/. See my edit.
– angus
Jan 19 '12 at 12:01
Not so great :-/. See my edit.
– angus
Jan 19 '12 at 12:01
add a comment |
Generate the weeks sequence with ncal
and use paste
for having both outputs side by side.
$ paste <(echo; echo; ncal -w | tail -1 | xargs -n1 printf '%2dn') <(cal)
If you don't like having tabs as delimiters just add something like sed 's/t/ /'
Edit: way simpler, no need to care about tabs:
$ paste -d' ' <((echo -n ' '; ncal -w | tail -1 )| fold -w 3) <(cal)
Elegant and embodies many of ESR's 17 Unix Rules. Should be the accepted answer as it truly combines the output ofcal
andncal
(other approaches replicate behavior of eithercal
orncal
).
– bishop
Nov 30 '16 at 14:53
add a comment |
Generate the weeks sequence with ncal
and use paste
for having both outputs side by side.
$ paste <(echo; echo; ncal -w | tail -1 | xargs -n1 printf '%2dn') <(cal)
If you don't like having tabs as delimiters just add something like sed 's/t/ /'
Edit: way simpler, no need to care about tabs:
$ paste -d' ' <((echo -n ' '; ncal -w | tail -1 )| fold -w 3) <(cal)
Elegant and embodies many of ESR's 17 Unix Rules. Should be the accepted answer as it truly combines the output ofcal
andncal
(other approaches replicate behavior of eithercal
orncal
).
– bishop
Nov 30 '16 at 14:53
add a comment |
Generate the weeks sequence with ncal
and use paste
for having both outputs side by side.
$ paste <(echo; echo; ncal -w | tail -1 | xargs -n1 printf '%2dn') <(cal)
If you don't like having tabs as delimiters just add something like sed 's/t/ /'
Edit: way simpler, no need to care about tabs:
$ paste -d' ' <((echo -n ' '; ncal -w | tail -1 )| fold -w 3) <(cal)
Generate the weeks sequence with ncal
and use paste
for having both outputs side by side.
$ paste <(echo; echo; ncal -w | tail -1 | xargs -n1 printf '%2dn') <(cal)
If you don't like having tabs as delimiters just add something like sed 's/t/ /'
Edit: way simpler, no need to care about tabs:
$ paste -d' ' <((echo -n ' '; ncal -w | tail -1 )| fold -w 3) <(cal)
edited Jan 19 '12 at 17:49
answered Jan 19 '12 at 16:39
funolletfunollet
22112
22112
Elegant and embodies many of ESR's 17 Unix Rules. Should be the accepted answer as it truly combines the output ofcal
andncal
(other approaches replicate behavior of eithercal
orncal
).
– bishop
Nov 30 '16 at 14:53
add a comment |
Elegant and embodies many of ESR's 17 Unix Rules. Should be the accepted answer as it truly combines the output ofcal
andncal
(other approaches replicate behavior of eithercal
orncal
).
– bishop
Nov 30 '16 at 14:53
Elegant and embodies many of ESR's 17 Unix Rules. Should be the accepted answer as it truly combines the output of
cal
and ncal
(other approaches replicate behavior of either cal
or ncal
).– bishop
Nov 30 '16 at 14:53
Elegant and embodies many of ESR's 17 Unix Rules. Should be the accepted answer as it truly combines the output of
cal
and ncal
(other approaches replicate behavior of either cal
or ncal
).– bishop
Nov 30 '16 at 14:53
add a comment |
One way using Perl (my output language of the cal
command is Spanish, but I hope result doesn't vary from English):
$ cal | perl -pe 'if ( m/As*d/ ) { s/A/++$i . qq[ ] x 2/e } else { s/A/qq[ ] x 3/e }'
Output:
enero de 2012
lu ma mi ju vi sá do
1 1
2 2 3 4 5 6 7 8
3 9 10 11 12 13 14 15
4 16 17 18 19 20 21 22
5 23 24 25 26 27 28 29
6 30 31
Explanation:
-pe # For every input line from previous pipe, execute next
# instructions and print to output.
if ( m/As*d/ ) # If line begins with a digit omitting spaces...
s/A/++$i . qq[ ] x 2/e # Insert at the beginning of the line a counter plus two spaces.
else # else...
s/A/qq[ ] x 3/e # Insert three spaces at the beginning of the line.
how will this work for subsequent months. February weeks do not start from again 1, they start from either 5/6 similarly March will be from 9/10.
– Nikhil Mulley
Jan 19 '12 at 10:41
cal 02 2012, will it have this failed??
– Nikhil Mulley
Jan 19 '12 at 10:42
@Nikhil: I don't understand what you mean. Can you try to explain it more in depth? Does the script fail withcal 02 2012
? It seemed to work in my test.
– Birei
Jan 19 '12 at 10:52
@Nikhil: Ah, ok. I missunderstood the question. It means absolute week numbers and not relative to each month. I will delete my wrong answer in a while.
– Birei
Jan 19 '12 at 10:55
1
cool..do not delete the answer, please keep it for reference.
– Nikhil Mulley
Jan 19 '12 at 11:09
add a comment |
One way using Perl (my output language of the cal
command is Spanish, but I hope result doesn't vary from English):
$ cal | perl -pe 'if ( m/As*d/ ) { s/A/++$i . qq[ ] x 2/e } else { s/A/qq[ ] x 3/e }'
Output:
enero de 2012
lu ma mi ju vi sá do
1 1
2 2 3 4 5 6 7 8
3 9 10 11 12 13 14 15
4 16 17 18 19 20 21 22
5 23 24 25 26 27 28 29
6 30 31
Explanation:
-pe # For every input line from previous pipe, execute next
# instructions and print to output.
if ( m/As*d/ ) # If line begins with a digit omitting spaces...
s/A/++$i . qq[ ] x 2/e # Insert at the beginning of the line a counter plus two spaces.
else # else...
s/A/qq[ ] x 3/e # Insert three spaces at the beginning of the line.
how will this work for subsequent months. February weeks do not start from again 1, they start from either 5/6 similarly March will be from 9/10.
– Nikhil Mulley
Jan 19 '12 at 10:41
cal 02 2012, will it have this failed??
– Nikhil Mulley
Jan 19 '12 at 10:42
@Nikhil: I don't understand what you mean. Can you try to explain it more in depth? Does the script fail withcal 02 2012
? It seemed to work in my test.
– Birei
Jan 19 '12 at 10:52
@Nikhil: Ah, ok. I missunderstood the question. It means absolute week numbers and not relative to each month. I will delete my wrong answer in a while.
– Birei
Jan 19 '12 at 10:55
1
cool..do not delete the answer, please keep it for reference.
– Nikhil Mulley
Jan 19 '12 at 11:09
add a comment |
One way using Perl (my output language of the cal
command is Spanish, but I hope result doesn't vary from English):
$ cal | perl -pe 'if ( m/As*d/ ) { s/A/++$i . qq[ ] x 2/e } else { s/A/qq[ ] x 3/e }'
Output:
enero de 2012
lu ma mi ju vi sá do
1 1
2 2 3 4 5 6 7 8
3 9 10 11 12 13 14 15
4 16 17 18 19 20 21 22
5 23 24 25 26 27 28 29
6 30 31
Explanation:
-pe # For every input line from previous pipe, execute next
# instructions and print to output.
if ( m/As*d/ ) # If line begins with a digit omitting spaces...
s/A/++$i . qq[ ] x 2/e # Insert at the beginning of the line a counter plus two spaces.
else # else...
s/A/qq[ ] x 3/e # Insert three spaces at the beginning of the line.
One way using Perl (my output language of the cal
command is Spanish, but I hope result doesn't vary from English):
$ cal | perl -pe 'if ( m/As*d/ ) { s/A/++$i . qq[ ] x 2/e } else { s/A/qq[ ] x 3/e }'
Output:
enero de 2012
lu ma mi ju vi sá do
1 1
2 2 3 4 5 6 7 8
3 9 10 11 12 13 14 15
4 16 17 18 19 20 21 22
5 23 24 25 26 27 28 29
6 30 31
Explanation:
-pe # For every input line from previous pipe, execute next
# instructions and print to output.
if ( m/As*d/ ) # If line begins with a digit omitting spaces...
s/A/++$i . qq[ ] x 2/e # Insert at the beginning of the line a counter plus two spaces.
else # else...
s/A/qq[ ] x 3/e # Insert three spaces at the beginning of the line.
edited Apr 14 '14 at 17:31
slm♦
256k71541687
256k71541687
answered Jan 19 '12 at 10:33
BireiBirei
5,10221514
5,10221514
how will this work for subsequent months. February weeks do not start from again 1, they start from either 5/6 similarly March will be from 9/10.
– Nikhil Mulley
Jan 19 '12 at 10:41
cal 02 2012, will it have this failed??
– Nikhil Mulley
Jan 19 '12 at 10:42
@Nikhil: I don't understand what you mean. Can you try to explain it more in depth? Does the script fail withcal 02 2012
? It seemed to work in my test.
– Birei
Jan 19 '12 at 10:52
@Nikhil: Ah, ok. I missunderstood the question. It means absolute week numbers and not relative to each month. I will delete my wrong answer in a while.
– Birei
Jan 19 '12 at 10:55
1
cool..do not delete the answer, please keep it for reference.
– Nikhil Mulley
Jan 19 '12 at 11:09
add a comment |
how will this work for subsequent months. February weeks do not start from again 1, they start from either 5/6 similarly March will be from 9/10.
– Nikhil Mulley
Jan 19 '12 at 10:41
cal 02 2012, will it have this failed??
– Nikhil Mulley
Jan 19 '12 at 10:42
@Nikhil: I don't understand what you mean. Can you try to explain it more in depth? Does the script fail withcal 02 2012
? It seemed to work in my test.
– Birei
Jan 19 '12 at 10:52
@Nikhil: Ah, ok. I missunderstood the question. It means absolute week numbers and not relative to each month. I will delete my wrong answer in a while.
– Birei
Jan 19 '12 at 10:55
1
cool..do not delete the answer, please keep it for reference.
– Nikhil Mulley
Jan 19 '12 at 11:09
how will this work for subsequent months. February weeks do not start from again 1, they start from either 5/6 similarly March will be from 9/10.
– Nikhil Mulley
Jan 19 '12 at 10:41
how will this work for subsequent months. February weeks do not start from again 1, they start from either 5/6 similarly March will be from 9/10.
– Nikhil Mulley
Jan 19 '12 at 10:41
cal 02 2012, will it have this failed??
– Nikhil Mulley
Jan 19 '12 at 10:42
cal 02 2012, will it have this failed??
– Nikhil Mulley
Jan 19 '12 at 10:42
@Nikhil: I don't understand what you mean. Can you try to explain it more in depth? Does the script fail with
cal 02 2012
? It seemed to work in my test.– Birei
Jan 19 '12 at 10:52
@Nikhil: I don't understand what you mean. Can you try to explain it more in depth? Does the script fail with
cal 02 2012
? It seemed to work in my test.– Birei
Jan 19 '12 at 10:52
@Nikhil: Ah, ok. I missunderstood the question. It means absolute week numbers and not relative to each month. I will delete my wrong answer in a while.
– Birei
Jan 19 '12 at 10:55
@Nikhil: Ah, ok. I missunderstood the question. It means absolute week numbers and not relative to each month. I will delete my wrong answer in a while.
– Birei
Jan 19 '12 at 10:55
1
1
cool..do not delete the answer, please keep it for reference.
– Nikhil Mulley
Jan 19 '12 at 11:09
cool..do not delete the answer, please keep it for reference.
– Nikhil Mulley
Jan 19 '12 at 11:09
add a comment |
Without enough rep to comment on the answer by slm, gcal can be installed on Mac OS using brew install gcal
.
(I found the answer here first, then this answer on ask different, but the unix answer lacked all the information I needed to install on the mac.)
add a comment |
Without enough rep to comment on the answer by slm, gcal can be installed on Mac OS using brew install gcal
.
(I found the answer here first, then this answer on ask different, but the unix answer lacked all the information I needed to install on the mac.)
add a comment |
Without enough rep to comment on the answer by slm, gcal can be installed on Mac OS using brew install gcal
.
(I found the answer here first, then this answer on ask different, but the unix answer lacked all the information I needed to install on the mac.)
Without enough rep to comment on the answer by slm, gcal can be installed on Mac OS using brew install gcal
.
(I found the answer here first, then this answer on ask different, but the unix answer lacked all the information I needed to install on the mac.)
answered Mar 13 '18 at 19:03
user3.1415927user3.1415927
53117
53117
add a comment |
add a comment |
Here is my solution which is shorter in code and works fine for other dates than the current month.
Sorry for the US people, this uses ISO format, i.e. 1st day of week is monday.
This is done with the -m option for cal and the %V option for date
#!/bin/bash
if [ "$1" = "" ]
then
when="now"
else
when="$1"
fi
y=$(date --date "$when" +%Y )
if [ $? -ne 0 ]
then
exit
fi
m=$(date --date "$when" +%m )
w=$(date --date $(printf %4d%02d01 $y $m) +%V)
cal -m $m $y |
awk -v w=$w '
NR>2{ww=w++}
$0!=""{printf "%2s %sn", ww , $0}
'
add a comment |
Here is my solution which is shorter in code and works fine for other dates than the current month.
Sorry for the US people, this uses ISO format, i.e. 1st day of week is monday.
This is done with the -m option for cal and the %V option for date
#!/bin/bash
if [ "$1" = "" ]
then
when="now"
else
when="$1"
fi
y=$(date --date "$when" +%Y )
if [ $? -ne 0 ]
then
exit
fi
m=$(date --date "$when" +%m )
w=$(date --date $(printf %4d%02d01 $y $m) +%V)
cal -m $m $y |
awk -v w=$w '
NR>2{ww=w++}
$0!=""{printf "%2s %sn", ww , $0}
'
add a comment |
Here is my solution which is shorter in code and works fine for other dates than the current month.
Sorry for the US people, this uses ISO format, i.e. 1st day of week is monday.
This is done with the -m option for cal and the %V option for date
#!/bin/bash
if [ "$1" = "" ]
then
when="now"
else
when="$1"
fi
y=$(date --date "$when" +%Y )
if [ $? -ne 0 ]
then
exit
fi
m=$(date --date "$when" +%m )
w=$(date --date $(printf %4d%02d01 $y $m) +%V)
cal -m $m $y |
awk -v w=$w '
NR>2{ww=w++}
$0!=""{printf "%2s %sn", ww , $0}
'
Here is my solution which is shorter in code and works fine for other dates than the current month.
Sorry for the US people, this uses ISO format, i.e. 1st day of week is monday.
This is done with the -m option for cal and the %V option for date
#!/bin/bash
if [ "$1" = "" ]
then
when="now"
else
when="$1"
fi
y=$(date --date "$when" +%Y )
if [ $? -ne 0 ]
then
exit
fi
m=$(date --date "$when" +%m )
w=$(date --date $(printf %4d%02d01 $y $m) +%V)
cal -m $m $y |
awk -v w=$w '
NR>2{ww=w++}
$0!=""{printf "%2s %sn", ww , $0}
'
edited Mar 23 '15 at 9:44
answered Mar 23 '15 at 9:38


GunstickGunstick
32
32
add a comment |
add a comment |
I did this script to generate on-duty calendar for teams. It generates calendar with week numbers and assigning names to them (simply round-robin). After output is printed you just need to copy it to excel and do "text to column" by ; and once again perform "text to column" on first column by using "fixed" option. The benefit of this calendar (comparing with horizontal orientation) is that you can use auto-filter (Alt+d f f) and find only your weeks.
#!/usr/bin/ksh
y=2017
set -A team1 Petars Justas Richard Jukka Jesper
set -A team2 Modestas Timo Mats Andreas
i1=0
i2=0
wn=1
own=1
echo "WN Month Mo Tu We Th Fr Sa Su ;Team1 ;Team2"
ycal=`for m in 1 2 3 4 5 6 7 8 9 10 11 12
do
IFS=
cal -m $m $y|egrep -v "Mo|^$"|while read line
do
echo $line|grep -q $y && mname=$line || print $mname $line|sed 's/'$y'//'
done
done`
IFS=
echo "$ycal"|while read line2
do
IFS=
echo "$line2"|grep -v "1 2 3 4 5 6 7"|egrep -q "* 1 |* 1$" || let wn++
[ $own -ne $wn ] && {
[ $i1 -eq ${#team1[@]}-1 ] && i1=0 || let i1++;
[ $i2 -eq ${#team2[@]}-1 ] && i2=0 || let i2++; }
printf '%2s %s ;%s ;%sn' $wn $line2 ${team1[$i1]} ${team2[$i2]}
own=$wn
done
Sorry, code a bit nasty... and no comments... but it works well ;)
add a comment |
I did this script to generate on-duty calendar for teams. It generates calendar with week numbers and assigning names to them (simply round-robin). After output is printed you just need to copy it to excel and do "text to column" by ; and once again perform "text to column" on first column by using "fixed" option. The benefit of this calendar (comparing with horizontal orientation) is that you can use auto-filter (Alt+d f f) and find only your weeks.
#!/usr/bin/ksh
y=2017
set -A team1 Petars Justas Richard Jukka Jesper
set -A team2 Modestas Timo Mats Andreas
i1=0
i2=0
wn=1
own=1
echo "WN Month Mo Tu We Th Fr Sa Su ;Team1 ;Team2"
ycal=`for m in 1 2 3 4 5 6 7 8 9 10 11 12
do
IFS=
cal -m $m $y|egrep -v "Mo|^$"|while read line
do
echo $line|grep -q $y && mname=$line || print $mname $line|sed 's/'$y'//'
done
done`
IFS=
echo "$ycal"|while read line2
do
IFS=
echo "$line2"|grep -v "1 2 3 4 5 6 7"|egrep -q "* 1 |* 1$" || let wn++
[ $own -ne $wn ] && {
[ $i1 -eq ${#team1[@]}-1 ] && i1=0 || let i1++;
[ $i2 -eq ${#team2[@]}-1 ] && i2=0 || let i2++; }
printf '%2s %s ;%s ;%sn' $wn $line2 ${team1[$i1]} ${team2[$i2]}
own=$wn
done
Sorry, code a bit nasty... and no comments... but it works well ;)
add a comment |
I did this script to generate on-duty calendar for teams. It generates calendar with week numbers and assigning names to them (simply round-robin). After output is printed you just need to copy it to excel and do "text to column" by ; and once again perform "text to column" on first column by using "fixed" option. The benefit of this calendar (comparing with horizontal orientation) is that you can use auto-filter (Alt+d f f) and find only your weeks.
#!/usr/bin/ksh
y=2017
set -A team1 Petars Justas Richard Jukka Jesper
set -A team2 Modestas Timo Mats Andreas
i1=0
i2=0
wn=1
own=1
echo "WN Month Mo Tu We Th Fr Sa Su ;Team1 ;Team2"
ycal=`for m in 1 2 3 4 5 6 7 8 9 10 11 12
do
IFS=
cal -m $m $y|egrep -v "Mo|^$"|while read line
do
echo $line|grep -q $y && mname=$line || print $mname $line|sed 's/'$y'//'
done
done`
IFS=
echo "$ycal"|while read line2
do
IFS=
echo "$line2"|grep -v "1 2 3 4 5 6 7"|egrep -q "* 1 |* 1$" || let wn++
[ $own -ne $wn ] && {
[ $i1 -eq ${#team1[@]}-1 ] && i1=0 || let i1++;
[ $i2 -eq ${#team2[@]}-1 ] && i2=0 || let i2++; }
printf '%2s %s ;%s ;%sn' $wn $line2 ${team1[$i1]} ${team2[$i2]}
own=$wn
done
Sorry, code a bit nasty... and no comments... but it works well ;)
I did this script to generate on-duty calendar for teams. It generates calendar with week numbers and assigning names to them (simply round-robin). After output is printed you just need to copy it to excel and do "text to column" by ; and once again perform "text to column" on first column by using "fixed" option. The benefit of this calendar (comparing with horizontal orientation) is that you can use auto-filter (Alt+d f f) and find only your weeks.
#!/usr/bin/ksh
y=2017
set -A team1 Petars Justas Richard Jukka Jesper
set -A team2 Modestas Timo Mats Andreas
i1=0
i2=0
wn=1
own=1
echo "WN Month Mo Tu We Th Fr Sa Su ;Team1 ;Team2"
ycal=`for m in 1 2 3 4 5 6 7 8 9 10 11 12
do
IFS=
cal -m $m $y|egrep -v "Mo|^$"|while read line
do
echo $line|grep -q $y && mname=$line || print $mname $line|sed 's/'$y'//'
done
done`
IFS=
echo "$ycal"|while read line2
do
IFS=
echo "$line2"|grep -v "1 2 3 4 5 6 7"|egrep -q "* 1 |* 1$" || let wn++
[ $own -ne $wn ] && {
[ $i1 -eq ${#team1[@]}-1 ] && i1=0 || let i1++;
[ $i2 -eq ${#team2[@]}-1 ] && i2=0 || let i2++; }
printf '%2s %s ;%s ;%sn' $wn $line2 ${team1[$i1]} ${team2[$i2]}
own=$wn
done
Sorry, code a bit nasty... and no comments... but it works well ;)
edited Dec 1 '16 at 10:25
answered Nov 30 '16 at 12:30


Petras LPetras L
10116
10116
add a comment |
add a comment |
I don't really want to install gcal
, and I could not get it to work properly on my system. So without gcal, funollet's answer is indeed the best solution. I just changed it a tiny bit so that it also preserves formatting such as day highlight, colors etc. using the script
command. I also removed redundant characters, making it suitable as an alias, as such:
alias today="paste -d' ' <((echo ' '{,};ncal -w|tail -1)|fold -w3) <(script /dev/null -qc cal)"
Or replace cal
with a new function, like so:
function cal {
paste -d' ' <((echo ' '{,};ncal -w $@|tail -1)|fold -w3) <(script /dev/null -qc "cal $@")
}
However, with this you cannot do things like cal -3
. You get the correct weeks but they are not aligned properly.
add a comment |
I don't really want to install gcal
, and I could not get it to work properly on my system. So without gcal, funollet's answer is indeed the best solution. I just changed it a tiny bit so that it also preserves formatting such as day highlight, colors etc. using the script
command. I also removed redundant characters, making it suitable as an alias, as such:
alias today="paste -d' ' <((echo ' '{,};ncal -w|tail -1)|fold -w3) <(script /dev/null -qc cal)"
Or replace cal
with a new function, like so:
function cal {
paste -d' ' <((echo ' '{,};ncal -w $@|tail -1)|fold -w3) <(script /dev/null -qc "cal $@")
}
However, with this you cannot do things like cal -3
. You get the correct weeks but they are not aligned properly.
add a comment |
I don't really want to install gcal
, and I could not get it to work properly on my system. So without gcal, funollet's answer is indeed the best solution. I just changed it a tiny bit so that it also preserves formatting such as day highlight, colors etc. using the script
command. I also removed redundant characters, making it suitable as an alias, as such:
alias today="paste -d' ' <((echo ' '{,};ncal -w|tail -1)|fold -w3) <(script /dev/null -qc cal)"
Or replace cal
with a new function, like so:
function cal {
paste -d' ' <((echo ' '{,};ncal -w $@|tail -1)|fold -w3) <(script /dev/null -qc "cal $@")
}
However, with this you cannot do things like cal -3
. You get the correct weeks but they are not aligned properly.
I don't really want to install gcal
, and I could not get it to work properly on my system. So without gcal, funollet's answer is indeed the best solution. I just changed it a tiny bit so that it also preserves formatting such as day highlight, colors etc. using the script
command. I also removed redundant characters, making it suitable as an alias, as such:
alias today="paste -d' ' <((echo ' '{,};ncal -w|tail -1)|fold -w3) <(script /dev/null -qc cal)"
Or replace cal
with a new function, like so:
function cal {
paste -d' ' <((echo ' '{,};ncal -w $@|tail -1)|fold -w3) <(script /dev/null -qc "cal $@")
}
However, with this you cannot do things like cal -3
. You get the correct weeks but they are not aligned properly.
edited yesterday
answered yesterday
fivethousfivethous
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f29434%2fdisplaying-weeks-number-in-certain-format-using-ncal-or-cal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown