$ cat tst.sh
#!/usr/bin/env bash
awk -F'[ -]' -v OFS='\t' '{print $2$3, NR, $0}' "${@:--}" |
sort -k1,1n -k2,2n |
cut -f3- |
awk -F'[ -]' '
{ curr = $2$3 }
curr != prev {
close(out)
out = "file_" (++cnt) ".csv"
prev = curr
}
{ print > out }
'
./tst.sh file
$ head file_*
==> file_1.csv <==
id1, 2017-04-28T19:59:00, 80
id2, 2017-04-28T03:14:35, e4
id5, 2017-04-28T00:31:54, 65
id7, 2017-04-28T21:04:30, 7f
==> file_2.csv <==
id0, 2020-12-12T07:18:26, 7f
id3, 2020-12-12T23:45:09, ff
id4, 2020-12-12T09:12:34, a1
id6, 2020-12-12T20:13:47, 45
The above will work robustly, efficiently, and portably with any POSIX awk, sort, and cut and will preserve the input order in the output files.
Here's how the first 3 steps are rearranging the input file contents:
$ cat file
id0, 2020-12-12T07:18:26, 7f
id1, 2017-04-28T19:59:00, 80
id2, 2017-04-28T03:14:35, e4
id3, 2020-12-12T23:45:09, ff
id4, 2020-12-12T09:12:34, a1
id5, 2017-04-28T00:31:54, 65
id6, 2020-12-12T20:13:47, 45
id7, 2017-04-28T21:04:30, 7f
so that by the time the final awk script runs it has lines ordered by the year+month from $2 and preserving the input order for all lines with the same date+time:
$ awk -F'[ -]' -v OFS='\t' '{print $2$3, NR, $0}' file
202012 1 id0, 2020-12-12T07:18:26, 7f
201704 2 id1, 2017-04-28T19:59:00, 80
201704 3 id2, 2017-04-28T03:14:35, e4
202012 4 id3, 2020-12-12T23:45:09, ff
202012 5 id4, 2020-12-12T09:12:34, a1
201704 6 id5, 2017-04-28T00:31:54, 65
202012 7 id6, 2020-12-12T20:13:47, 45
201704 8 id7, 2017-04-28T21:04:30, 7f
$ awk -F'[ -]' -v OFS='\t' '{print $2$3, NR, $0}' file | sort -k1,1n -k2,2n
201704 2 id1, 2017-04-28T19:59:00, 80
201704 3 id2, 2017-04-28T03:14:35, e4
201704 6 id5, 2017-04-28T00:31:54, 65
201704 8 id7, 2017-04-28T21:04:30, 7f
202012 1 id0, 2020-12-12T07:18:26, 7f
202012 4 id3, 2020-12-12T23:45:09, ff
202012 5 id4, 2020-12-12T09:12:34, a1
202012 7 id6, 2020-12-12T20:13:47, 45
$ awk -F'[ -]' -v OFS='\t' '{print $2$3, NR, $0}' file | sort -k1,1n -k2,2n | cut -f3-
id1, 2017-04-28T19:59:00, 80
id2, 2017-04-28T03:14:35, e4
id5, 2017-04-28T00:31:54, 65
id7, 2017-04-28T21:04:30, 7f
id0, 2020-12-12T07:18:26, 7f
id3, 2020-12-12T23:45:09, ff
id4, 2020-12-12T09:12:34, a1
id6, 2020-12-12T20:13:47, 45