Home » Linux » How to sort strings that contain a common prefix and suffix numerically from Bash?

How to sort strings that contain a common prefix and suffix numerically from Bash?

Posted by: admin November 29, 2017 Leave a comment

Questions:

Here is a list of files:

some.string_100_with_numbers.in-it.txt
some.string_101_with_numbers.in-it.txt
some.string_102_with_numbers.in-it.txt
some.string_23_with_numbers.in-it.txt
some.string_24_with_numbers.in-it.txt
some.string_25_with_numbers.in-it.txt

Now I would like to sort it numerically. Starting with *23* and ending with *102*.

I have tried -n and -g. -t does not help in these messy strings.

Can I ignore leading strings to the number with an option or do I have to be clever and script?

Answers:

Use ls -lv

From the man page:

-v     natural sort of (version) numbers within text

Questions:
Answers:

Try the following:

sort -t '_' -k 2n
  • -t '_' (sets the delimiter to the underscore character)
  • -k 2n (sorts by the second column using numeric ordering)

DEMO.

Questions:
Answers:

In the general case, try the Schwartzian transform.

Briefly, break out the number into its own field, sort on that, and discard the added field.

# In many shells, use ctrl-v tab to insert a literal tab after the first 
sed 's/^\([^0-9]*\)\([0-9][0-9]*\)/   /' file |
sort -n |
cut -f2-

This works nicely if the input doesn’t have an obvious separator, like for the following input.

abc1
abc10
abc2

where you would like the sort to move the last line up right after the first.