-6

Hi friends i have a list of non sequential numbers in a file, need to list sequential range of number.

example:

641
642
643
712
713
714
813
814
815

need to print:

641-643,712-714,813-815

either in awk,unix or perl

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Mahesh Cholleti
  • 19
  • 1
  • 10
  • 2
    what did you try? it is important to show your attempts – fedorqui Apr 24 '15 at 14:29
  • My colleague is working on such file , hence its my duty to help him. – Mahesh Cholleti Apr 24 '15 at 14:32
  • 1
    You should first show us what you have tired, so that we can help you out in the right direction by pointing out any mistakes you made during the aproach and any better aproaches. – nu11p01n73R Apr 24 '15 at 14:36
  • it is in fact an interesting problem. It would be good if you could show what you have tried and where were you stuck. Question like "I have this, I want to have that, do it for me please" is not welcomed here. That might be the reaseon why you got so many downvotes. But again, I think this is an interesting problem. @MaheshCholleti next time try asking in a better way. – Kent Apr 24 '15 at 14:44
  • We are trying to help, only that we appreciate people following the suggestions given in [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – fedorqui Apr 24 '15 at 14:44
  • thanks Kent for suggestions, next time i will have a good approach. – Mahesh Cholleti Apr 24 '15 at 14:46
  • 1
    @TomFenech it is not a dup, is it? I would say, "similar question" – Kent Apr 24 '15 at 14:47
  • 2
    @fedorqui bro sorry i was liitle bit harsh. – Mahesh Cholleti Apr 24 '15 at 14:48

1 Answers1

1

this line works for your example:

awk 'NR==1{printf "%s",$0;p=$0;next}
     $0!=p+1{printf "-%d %d",p,$0}{p=$0}END{print "-"$0}' f

with your data:

kent$  cat f
641
642
643
712
713
714
813
814
815
kent$   awk 'NR==1{printf "%s",$0;p=$0;next}$0!=p+1{printf "-%d %d",p,$0}{p=$0}END{print "-"$0}' f
641-643 712-714 813-815

Note that the last group number may have problem (with unexpected - ) It depends on your input. Anyway, you got the idea, you can do something adjustment on the line above.

Kent
  • 189,393
  • 32
  • 233
  • 301