Sort strings with numbers naturally with GNU sort

Say you have a list of filenames or identifiers with numbers, e.g.

unknowns.txt:

unknown_E1_2
unknown_E1_1
unknown_E2_45
unknown_E10_34
unknown_E1_10
unknown_E1_23

and you want to sort them “naturally”, e.g. unknown_E1_2 should come before unknown_E1_10. If you would just sort them as strings (character by character), 10 would come before 2, because the character ‘1’ comes before ‘2’, and the trailing ‘0’ just doesn’t matter for the sort order anymore.

sort from the GNU coreutils let’s you do this easily with the -V switch. Note that this not only works for numbers at the end of a string, but also for the numbers in the middle:

# sort -V unknowns.txt
unknown_E1_1
unknown_E1_2
unknown_E1_10
unknown_E1_23
unknown_E2_45
unknown_E10_34

See Ned Batchfelder’s blog post on how to do this in Python. Especially check out Toothy’s comment for a brief solution.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: