Ordering a string by the count of substrings

sorttext processing

I have long list of numbers like this:

1234-212-22-11153782-0114232192380
8807698823332-6756-234-14-09867378
45323-14-221-238372635363-43676256
62736373-9983-23-234-8863345637388

. . . . 
. . . . 

I would like to do two things:

1) order this list by the count of digits within each segment, the output should be like this:

22-212-1234-11153782-0114232192380
14-234-6756-09867378-8807698823332
14-221-45323-43676256-238372635363
23-234-9983-62736373-8863345637388

2) find the count of sub strings in each line, the output should be:

2-3-4-8-13
2-3-4-8-13
2-3-5-8-12
2-3-4-8-13

In this example the first, second and third segments of each number has same numbers, but they could be different.

Best Answer

How about

$ perl -F'-' -lpe '$_ = join "-", sort { length $a <=> length $b } @F' file
22-212-1234-11153782-0114232192380
14-234-6756-09867378-8807698823332
14-221-45323-43676256-238372635363
23-234-9983-62736373-8863345637388

and

$ perl -F'-' -lpe '$_ = join "-", sort { $a <=> $b } map length, @F' file
2-3-4-8-13
2-3-4-8-13
2-3-5-8-12
2-3-4-8-13

Thanks to Stéphane Chazelas for suggested improvements

Related Question