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

前端 未结 4 1694
旧时难觅i
旧时难觅i 2020-11-30 07:33

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_         


        
4条回答
  •  一生所求
    2020-11-30 07:59

    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 \2
    sed 's/^\([^0-9]*\)\([0-9][0-9]*\)/\2   \1\2/' 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.

提交回复
热议问题