There's a perl module called Set::IntSpan which already does this (it was originally written in 1996 to collapse lists of article numbers for .newsrc files, which could be enormous).
There is also a similar module for python called intspan, but I haven't used it.
Anyway, with perl and Set::IntSpan (and tr to get the input data into comma-separated format, and tr again to munge the output), this is trivial.
$ tr $'\n' ',' < input.txt |
perl -MSet::IntSpan -lne 'print Set::IntSpan->new($_)' |
tr ',-' $'\n,'
2,3
9,12
24
28,29
33
Set::IntSpan is packaged for debian and ubuntu as libset-intspan-perl, for fedora as perl-Set-IntSpan, and probably for other distros too. Also available on CPAN, of course.
(1) How I provideI can usedcthe numbers viaSTDIN? I tried to remove-f "$1"and prepend andecho "$numbers"or append<<< "$numbers", but it didn’t work.-f -and thendcreads theSTDIN. (2) How could I replace the newlines with a custom separator? I have already replaced[,]with[-], but I have no idea if I replace the newlines with,(of course, I can do it usingsed, for example). – tukusejssirs Jun 29 '21 at 08:56-f -). The second problem I wish to solve is to replace the newlines with a custom string, so that the output looks like2-3, 9-12, 24, 28-29, 33(I copied the example from the OP). Currenly, I can do it only with an additional command (likesed), but not directly withdc. Note that I have no idea if it is actually possible withdc. ;) – tukusejssirs Jul 01 '21 at 16:18