根据每行中存在的数值对文本文件进行排序

根据每行中存在的数值对文本文件进行排序

我有一个文本文件,其中包含各种 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_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

我希望它们根据TC之后的数字进行排序,这样它们就可以像这样

/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

我尝试过使用sortsort -n但没有一个有效。谁能帮我解决这个问题吗?

答案1

如果您的文件名与您显示的完全一样,您希望在由以下定义的第五个字段上进行数字排序_

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

所以,你可以这样做:

$ 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

这表明sort用作_字段分隔符并-n仅对第 5 个字段 ( ) 进行数字排序 ( -k5,5)。

或者,如果您有 GNU sort,请使用-V

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

所以你可以做

sort -V names 

并得到相同的结果。

答案2

“我想按文件名的属性排序”的过程是:

  1. 装饰(即将用于排序的键放在前面)
  2. 种类
  3. 装饰

如果,并且仅有的如果您知道这些特定文件名不能包含换行符:您可以通过将数字与正则表达式匹配来进行装饰,在行的开头重复它,然后按数字排序。

在这种情况下,链条看起来像

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

您需要将所有以your_适用于您的用例的内容替换。 (在你的小例子中这应该是完全可行的。尝试一下!)

(这sed -n … ;p'确保只打印与您的模式匹配的行。因此,如果您不小心包含了与您的模式不匹配的文件,它们将被排除。如果您不希望这样,请不要使用-nsed 选项和p标志。

相关内容