-2

I have a text file containing paths of various wav files. But when they are created they are sorted based on alphabetical order. Hence the paths are like this

/home/wav_path/converted_Interpreter_TC_100.wav
/home/wav_path/converted_Interpreter_TC_101.wav
/home/wav_path/converted_Interpreter_TC_102.wav
/home/wav_path/converted_Interpreter_TC_103.wav
/home/wav_path/converted_Interpreter_TC_104.wav
/home/wav_path/converted_Interpreter_TC_105.wav
/home/wav_path/converted_Interpreter_TC_106.wav
/home/wav_path/converted_Interpreter_TC_107.wav
/home/wav_path/converted_Interpreter_TC_108.wav
/home/wav_path/converted_Interpreter_TC_109.wav
/home/wav_path/converted_Interpreter_TC_10.wav
/home/wav_path/converted_Interpreter_TC_110.wav
/home/wav_path/converted_Interpreter_TC_111.wav
/home/wav_path/converted_Interpreter_TC_11.wav
/home/wav_path/converted_Interpreter_TC_12.wav
/home/wav_path/converted_Interpreter_TC_13.wav
/home/wav_path/converted_Interpreter_TC_14.wav
/home/wav_path/converted_Interpreter_TC_15.wav
/home/wav_path/converted_Interpreter_TC_16.wav
/home/wav_path/converted_Interpreter_TC_17.wav
/home/wav_path/converted_Interpreter_TC_18.wav
/home/wav_path/converted_Interpreter_TC_19.wav
/home/wav_path/converted_Interpreter_TC_1.wav
/home/wav_path/converted_Interpreter_TC_20.wav
/home/wav_path/converted_Interpreter_TC_21.wav
/home/wav_path/converted_Interpreter_TC_22.wav
/home/wav_path/converted_Interpreter_TC_23.wav
/home/wav_path/converted_Interpreter_TC_24.wav
/home/wav_path/converted_Interpreter_TC_25.wav
/home/wav_path/converted_Interpreter_TC_26.wav
/home/wav_path/converted_Interpreter_TC_27.wav
/home/wav_path/converted_Interpreter_TC_28.wav
/home/wav_path/converted_Interpreter_TC_29.wav
/home/wav_path/converted_Interpreter_TC_2.wav
/home/wav_path/converted_Interpreter_TC_30.wav
/home/wav_path/converted_Interpreter_TC_31.wav
/home/wav_path/converted_Interpreter_TC_32.wav
/home/wav_path/converted_Interpreter_TC_33.wav
/home/wav_path/converted_Interpreter_TC_34.wav
/home/wav_path/converted_Interpreter_TC_35.wav
/home/wav_path/converted_Interpreter_TC_36.wav
/home/wav_path/converted_Interpreter_TC_37.wav
/home/wav_path/converted_Interpreter_TC_38.wav
/home/wav_path/converted_Interpreter_TC_39.wav
/home/wav_path/converted_Interpreter_TC_3.wav
/home/wav_path/converted_Interpreter_TC_40.wav
/home/wav_path/converted_Interpreter_TC_41.wav
/home/wav_path/converted_Interpreter_TC_42.wav
/home/wav_path/converted_Interpreter_TC_43.wav
/home/wav_path/converted_Interpreter_TC_44.wav
/home/wav_path/converted_Interpreter_TC_45.wav
/home/wav_path/converted_Interpreter_TC_46.wav
/home/wav_path/converted_Interpreter_TC_47.wav
/home/wav_path/converted_Interpreter_TC_48.wav
/home/wav_path/converted_Interpreter_TC_49.wav
/home/wav_path/converted_Interpreter_TC_4.wav
/home/wav_path/converted_Interpreter_TC_50.wav
/home/wav_path/converted_Interpreter_TC_51.wav
/home/wav_path/converted_Interpreter_TC_52.wav
/home/wav_path/converted_Interpreter_TC_53.wav
/home/wav_path/converted_Interpreter_TC_54.wav
/home/wav_path/converted_Interpreter_TC_55.wav
/home/wav_path/converted_Interpreter_TC_56.wav
/home/wav_path/converted_Interpreter_TC_57.wav
/home/wav_path/converted_Interpreter_TC_58.wav
/home/wav_path/converted_Interpreter_TC_59.wav
/home/wav_path/converted_Interpreter_TC_5.wav
/home/wav_path/converted_Interpreter_TC_60.wav
/home/wav_path/converted_Interpreter_TC_61.wav
/home/wav_path/converted_Interpreter_TC_62.wav
/home/wav_path/converted_Interpreter_TC_63.wav
/home/wav_path/converted_Interpreter_TC_64.wav
/home/wav_path/converted_Interpreter_TC_65.wav
/home/wav_path/converted_Interpreter_TC_66.wav
/home/wav_path/converted_Interpreter_TC_67.wav
/home/wav_path/converted_Interpreter_TC_68.wav
/home/wav_path/converted_Interpreter_TC_69.wav
/home/wav_path/converted_Interpreter_TC_6.wav
/home/wav_path/converted_Interpreter_TC_70.wav
/home/wav_path/converted_Interpreter_TC_71.wav
/home/wav_path/converted_Interpreter_TC_72.wav
/home/wav_path/converted_Interpreter_TC_73.wav
/home/wav_path/converted_Interpreter_TC_74.wav
/home/wav_path/converted_Interpreter_TC_75.wav
/home/wav_path/converted_Interpreter_TC_76.wav
/home/wav_path/converted_Interpreter_TC_77.wav
/home/wav_path/converted_Interpreter_TC_78.wav
/home/wav_path/converted_Interpreter_TC_79.wav
/home/wav_path/converted_Interpreter_TC_7.wav
/home/wav_path/converted_Interpreter_TC_80.wav
/home/wav_path/converted_Interpreter_TC_81.wav
/home/wav_path/converted_Interpreter_TC_82.wav
/home/wav_path/converted_Interpreter_TC_83.wav
/home/wav_path/converted_Interpreter_TC_84.wav
/home/wav_path/converted_Interpreter_TC_85.wav
/home/wav_path/converted_Interpreter_TC_86.wav
/home/wav_path/converted_Interpreter_TC_87.wav
/home/wav_path/converted_Interpreter_TC_88.wav
/home/wav_path/converted_Interpreter_TC_89.wav
/home/wav_path/converted_Interpreter_TC_8.wav
/home/wav_path/converted_Interpreter_TC_90.wav
/home/wav_path/converted_Interpreter_TC_91.wav
/home/wav_path/converted_Interpreter_TC_92.wav
/home/wav_path/converted_Interpreter_TC_93.wav
/home/wav_path/converted_Interpreter_TC_94.wav
/home/wav_path/converted_Interpreter_TC_95.wav
/home/wav_path/converted_Interpreter_TC_98.wav
/home/wav_path/converted_Interpreter_TC_99.wav
/home/wav_path/converted_Interpreter_TC_9.wav

I want them to be sorted based on the numerics after TC, so that they can be like this

/home/wav_path/converted_Interpreter_TC_1.wav
/home/wav_path/converted_Interpreter_TC_2.wav
/home/wav_path/converted_Interpreter_TC_3.wav
/home/wav_path/converted_Interpreter_TC_4.wav

I have tried using sort and sort -n but none are working. Can anyone please help me with how to do this?

5
  • please update the question with your sort attempts Commented Nov 2, 2023 at 14:30
  • will all files have the same number of (sub)directories or could the number (of (sub)dirs) vary? if they could vary, please update to show an example; will all files have the same directory/filename prefix (eg, /home/wav_path/converted_Interpreter_TC_) - if 'no' then please update to show some other samples Commented Nov 2, 2023 at 14:31
  • 3
    See sort -V with the GNU implementation of sort. Commented Nov 2, 2023 at 14:31
  • Cross-posted: askubuntu.com/q/1491272/158442 Commented Nov 2, 2023 at 15:05
  • Turing: you're not that new. You know that posting the same question within 5 minutes on multiple sites is rude, because it wastes answerer's time. I'm a bit disappointed in you. Commented Nov 2, 2023 at 15:08

2 Answers 2

2

If your file names are exactly as you show, you want to sort numericaly on the 5th field defined by _:

/home/wav_path/converted_Interpreter_TC_1.wav
_________ ______________ ___________ __ _____
1st field    2nd field    3d field   4th 5th

So, you can do:

$ sort -t _ -nk5,5 names 
/home/wav_path/converted_Interpreter_TC_1.wav
/home/wav_path/converted_Interpreter_TC_2.wav
/home/wav_path/converted_Interpreter_TC_3.wav
/home/wav_path/converted_Interpreter_TC_4.wav
/home/wav_path/converted_Interpreter_TC_5.wav
/home/wav_path/converted_Interpreter_TC_6.wav
/home/wav_path/converted_Interpreter_TC_7.wav
/home/wav_path/converted_Interpreter_TC_8.wav
/home/wav_path/converted_Interpreter_TC_9.wav
/home/wav_path/converted_Interpreter_TC_10.wav
/home/wav_path/converted_Interpreter_TC_11.wav
/home/wav_path/converted_Interpreter_TC_12.wav
/home/wav_path/converted_Interpreter_TC_13.wav
/home/wav_path/converted_Interpreter_TC_14.wav
/home/wav_path/converted_Interpreter_TC_15.wav
/home/wav_path/converted_Interpreter_TC_16.wav
/home/wav_path/converted_Interpreter_TC_17.wav
/home/wav_path/converted_Interpreter_TC_18.wav
/home/wav_path/converted_Interpreter_TC_19.wav
/home/wav_path/converted_Interpreter_TC_20.wav
/home/wav_path/converted_Interpreter_TC_21.wav
/home/wav_path/converted_Interpreter_TC_22.wav
/home/wav_path/converted_Interpreter_TC_23.wav
/home/wav_path/converted_Interpreter_TC_24.wav
/home/wav_path/converted_Interpreter_TC_25.wav
/home/wav_path/converted_Interpreter_TC_26.wav
/home/wav_path/converted_Interpreter_TC_27.wav
/home/wav_path/converted_Interpreter_TC_28.wav
/home/wav_path/converted_Interpreter_TC_29.wav
/home/wav_path/converted_Interpreter_TC_30.wav
/home/wav_path/converted_Interpreter_TC_31.wav
/home/wav_path/converted_Interpreter_TC_32.wav
/home/wav_path/converted_Interpreter_TC_33.wav
/home/wav_path/converted_Interpreter_TC_34.wav
/home/wav_path/converted_Interpreter_TC_35.wav
/home/wav_path/converted_Interpreter_TC_36.wav
/home/wav_path/converted_Interpreter_TC_37.wav
/home/wav_path/converted_Interpreter_TC_38.wav
/home/wav_path/converted_Interpreter_TC_39.wav
/home/wav_path/converted_Interpreter_TC_40.wav
/home/wav_path/converted_Interpreter_TC_41.wav
/home/wav_path/converted_Interpreter_TC_42.wav
/home/wav_path/converted_Interpreter_TC_43.wav
/home/wav_path/converted_Interpreter_TC_44.wav
/home/wav_path/converted_Interpreter_TC_45.wav
/home/wav_path/converted_Interpreter_TC_46.wav
/home/wav_path/converted_Interpreter_TC_47.wav
/home/wav_path/converted_Interpreter_TC_48.wav
/home/wav_path/converted_Interpreter_TC_49.wav
/home/wav_path/converted_Interpreter_TC_50.wav
/home/wav_path/converted_Interpreter_TC_51.wav
/home/wav_path/converted_Interpreter_TC_52.wav
/home/wav_path/converted_Interpreter_TC_53.wav
/home/wav_path/converted_Interpreter_TC_54.wav
/home/wav_path/converted_Interpreter_TC_55.wav
/home/wav_path/converted_Interpreter_TC_56.wav
/home/wav_path/converted_Interpreter_TC_57.wav
/home/wav_path/converted_Interpreter_TC_58.wav
/home/wav_path/converted_Interpreter_TC_59.wav
/home/wav_path/converted_Interpreter_TC_60.wav
/home/wav_path/converted_Interpreter_TC_61.wav
/home/wav_path/converted_Interpreter_TC_62.wav
/home/wav_path/converted_Interpreter_TC_63.wav
/home/wav_path/converted_Interpreter_TC_64.wav
/home/wav_path/converted_Interpreter_TC_65.wav
/home/wav_path/converted_Interpreter_TC_66.wav
/home/wav_path/converted_Interpreter_TC_67.wav
/home/wav_path/converted_Interpreter_TC_68.wav
/home/wav_path/converted_Interpreter_TC_69.wav
/home/wav_path/converted_Interpreter_TC_70.wav
/home/wav_path/converted_Interpreter_TC_71.wav
/home/wav_path/converted_Interpreter_TC_72.wav
/home/wav_path/converted_Interpreter_TC_73.wav
/home/wav_path/converted_Interpreter_TC_74.wav
/home/wav_path/converted_Interpreter_TC_75.wav
/home/wav_path/converted_Interpreter_TC_76.wav
/home/wav_path/converted_Interpreter_TC_77.wav
/home/wav_path/converted_Interpreter_TC_78.wav
/home/wav_path/converted_Interpreter_TC_79.wav
/home/wav_path/converted_Interpreter_TC_80.wav
/home/wav_path/converted_Interpreter_TC_81.wav
/home/wav_path/converted_Interpreter_TC_82.wav
/home/wav_path/converted_Interpreter_TC_83.wav
/home/wav_path/converted_Interpreter_TC_84.wav
/home/wav_path/converted_Interpreter_TC_85.wav
/home/wav_path/converted_Interpreter_TC_86.wav
/home/wav_path/converted_Interpreter_TC_87.wav
/home/wav_path/converted_Interpreter_TC_88.wav
/home/wav_path/converted_Interpreter_TC_89.wav
/home/wav_path/converted_Interpreter_TC_90.wav
/home/wav_path/converted_Interpreter_TC_91.wav
/home/wav_path/converted_Interpreter_TC_92.wav
/home/wav_path/converted_Interpreter_TC_93.wav
/home/wav_path/converted_Interpreter_TC_94.wav
/home/wav_path/converted_Interpreter_TC_95.wav
/home/wav_path/converted_Interpreter_TC_98.wav
/home/wav_path/converted_Interpreter_TC_99.wav
/home/wav_path/converted_Interpreter_TC_100.wav
/home/wav_path/converted_Interpreter_TC_101.wav
/home/wav_path/converted_Interpreter_TC_102.wav
/home/wav_path/converted_Interpreter_TC_103.wav
/home/wav_path/converted_Interpreter_TC_104.wav
/home/wav_path/converted_Interpreter_TC_105.wav
/home/wav_path/converted_Interpreter_TC_106.wav
/home/wav_path/converted_Interpreter_TC_107.wav
/home/wav_path/converted_Interpreter_TC_108.wav
/home/wav_path/converted_Interpreter_TC_109.wav
/home/wav_path/converted_Interpreter_TC_110.wav
/home/wav_path/converted_Interpreter_TC_111.wav

That tells sort to use _ as the field delimiter and to sort numerically (-n) on the 5th field only (-k5,5).

Or, if you have GNU sort, use -V:

       -V, --version-sort
              natural sort of (version) numbers within text

So you can do

sort -V names 

and get the same result.

0

The procedure for "I want to sort by a property of the file name" is:

  1. decorate (i.e., put the key you want to use for sorting up front)
  2. sort
  3. de-decorate

If, and only if you know that these specific file names cannot contain new line characters: you can just decorate by matching the number with a regex, repeating it at the beginning of the line and then sorting numerically.

In this case, the chain looks like

<your_text_file \
    sed -n 's;^your_beginning_pattern\([[:digit:]]*\)your_end_pattern$;\1 &;p' | \
    sort -n | \
    sed 's;^[[:digit:]]* \(.*\)$;\1;'

You'll need to replace all that starts with your_ with what is applicable to your use case. (That should be utterly doable in your trivial example. Give it a try!)

(The sed -n … ;p' ensures that only lines matching your pattern are printed. So, if you accidentally included files that did not match your pattern, they will be excluded. If you don't want that, don't use the -n sed option and the p flag.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.