How to sort a file with string lines by the number of a specific symbol in the line?

Posted by SpacyRicochet on Super User See other posts from Super User or by SpacyRicochet
Published on 2013-10-21T08:52:42Z Indexed on 2013/10/21 10:01 UTC
Read the original article Hit count: 210

Filed under:
|
|
|
|

Say I have a large plaintext file with a string on every line. The string only consists of alphabetical characters, except for underscores _, which divide the strings in syllables.

I want to sort the text file by the amount underscores in the string. Bonus points for putting every group of X underscores in their own file.

Example:

hel_lo
hi
su_per_u_ser
o_ver_flow

would sort into:

hi
hel_lo
o_ver_flow
su_per_u_ser

I've tried doing this with regex, but I've yet to find a proper way of counting the underscores (regex confuses me thoroughly).

Anyone know how I could handle this?

© Super User or respective owner

Related posts about mac

Related posts about command-line