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
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
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.