If your last supports the --since option, you can limit to the current month with:
last --since "$(date +'%Y-%m-01')"
The command date +'%Y-%m-01' prints the date of the first day of this month. For example, if I run it today, April 26th, 2020, I get:
$ date +'%Y-%m-01'
2020-04-01
So the last command becomes last --since 2020-04-01.
This means that all we need to do in awk is exclude the lines starting with reboot, the one starting with wmtp, and any empty ones and then count:
last --since "$(date +'%Y-%m-01')" | awk '!/^reboot/&&!/^wtmp /&&/./{date=$5" "$6; a[date]++}END{for(date in a){printf "%s: %d\n",date,a[date]}}' | sort -nk2
Or, a little easier to read:
last --since "$(date +'%Y-%m-01')" |
awk '!/^reboot/ && !/^wtmp / && /./{
date=$5" "$6;
a[date]++
}
END{
for(date in a){
printf "%s: %d\n",date,a[date]
}
}' | sort -nk2
On my laptop, this returns:
$ last --since "$(date +'%Y-%m-01')" | awk '!/^reboot/&&!/^wtmp / && /./{date=$5" "$6; a[date]++}END{for(date in a){printf "%s: %d\n",date,a[date]}}' | sort -nk2
Apr 3: 1
Apr 5: 1
Apr 20: 1
If this is something you will be using often, you can make it into a shell function by adding these lines to your ~/.bashrc (or equivalent if you are not using bash):
monthLogins(){
last --since "$(date +'%Y-%m-01')" |
awk '!/^reboot/ && !/^wtmp / && /./{
date=$5" "$6;
a[date]++
}
END{
for(date in a){
printf "%s: %d\n",date,a[date]
}
}' | sort -nk2
}
You can then run monthLogins and get your desired output.
Alternatively, instead of filtering out reboot and wtmp, you could filter for only existing user names with (assuming your shell supports the <() construct):
monthLogins(){
last --since "$(date +'%Y-%m-01')" |
grep -f <(cut -d: -f1 /etc/passwd)
awk '/./{
date=$5" "$6;
a[date]++
}
END{
for(date in a){
printf "%s: %d\n",date,a[date]
}
}' | sort -nk2
}