How to find all Sun Sat on command line

This works on Ubuntu but not on Mac.

$ seq -f %1.0f 20210101 20211231 | date -f - 2>/dev/null | grep 'Sun\|Sat'
...
Sat Dec 18 00:00:00 JST 2021
Sun Dec 19 00:00:00 JST 2021
Sat Dec 25 00:00:00 JST 2021
Sun Dec 26 00:00:00 JST 2021

How to find all Sat and Sun from command line?


Solution 1:

The macOS/BSD version of date can't read from standard input so you need to loop manually.

for d in $(seq -f %1.0f 20210101 20211231); do 
    date -jf "%Y%m%d" $d 2>/dev/null | grep 'Sat\|Sun'
done

You can avoid most of the impossible dates created by seq (and get the result faster) by running

for m in {01..12}; do
    for d in {01..31}; do
        date -jf "%Y%m%d" "2021${m}${d}" 2>/dev/null
    done
done | grep 'Sat\|Sun'