I have a few to questions pose in response first:-
Is this homework/assignment? There are specific forums for these.
What have you tried so far?
What output/errors do you get?
What OS and version are you using?
What are your preferred tools? (C, shell, perl, awk, etc.)
What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?
There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.
We're all here to learn and getting the relevant information will help us all.
Not homework/assignment, just a task I want to complete at work to make reporting on some Slurm scheduler output a bit more manageable.
CentOS 7, awk the tool of choice.
I have tried the following, which seems to be getting somewhere, just having a mental block on how to get over the finishing line:
cat test | sed 's/cn//g' | \
awk '{ \
if (length(PRE) == 0) printf "%s", $1 ; \
else if ($1>PRE+1) printf ",%s", $1 ; \
else if ($1=PRE+1) printf "-" ; \
PRE=$1 ; } \
END { print }'
Producing:
001,004,016-----,031-,038,042---,048048
(I put an additional cn048 at the end, just as a test as I got some weird output of another test batch of data using this technique. Otherwise it is the data above).
The almost comes in as if I have a slightly different dataset where the final number isn't the end of a sequence, it becomes a sequence of its own, e.g.