【问题标题】:On command-line how to start sorting from nth index in ascending order在命令行上如何从第 n 个索引开始按升序排序
【发布时间】:2015-02-17 08:44:30
【问题描述】:

=> "ls" 命令总是按升序输出:

common_results /1_L_0.010293_O=3.4077_B=1_SR=1.1349_500/test.txt

common_results /1_L_0.010588_O=0.24208_B=0_SR=0.70094_500/test.txt

common_results /1_L_0.011283_O=1.6461_B=0_SR=0.86875_500/test.txt

common_results /1_L_0.011446_O=2.9968_B=0_SR=0.74779_500/test.txt

common_results /1_L_0.011487_O=8.5498_B=0_SR=0.84261_500/test.txt

=> 是否可以从第 n 个索引点开始执行“ls”。例如,从第 29 个索引点开始,它将从 "_O="

旁边的字符开始升序

ls”(更新结果。)

common_results /1_L_0.010588_O=0.24208_B=0_SR=0.70094_500/test.txt

common_results /1_L_0.010293_O=3.4077_B=1_SR=1.1349_500/test.txt

common_results /1_L_0.011446_O=2.9968_B=0_SR=0.74779_500/test.txt

common_results /1_L_0.011487_O=8.5498_B=0_SR=0.84261_500/test.txt

common_results /1_L_0.011283_O=1.6461_B=0_SR=0.86875_500/test.txt

【问题讨论】:

    标签: sorting command-line ls


    【解决方案1】:

    解析ls 的输出并不总是一个好主意,但是,如果您了解后果(它可能充满各种奇妙的字符,如空格或换行符,所有这些都会导致简单的假设失败)并且可以缓解问题,您可以通过sort传递文件名以获得您想要的效果:

    pax> #   1         2         3         4
    pax> #7890123456789012345678901234567890
    pax> #                        V
    pax> echo '
    common_results/1_L_0.010293_O=3.4077_B=1_SR=1.1349_500/test.txt
    common_results/1_L_0.010588_O=0.24208_B=0_SR=0.70094_500/test.txt
    common_results/1_L_0.011283_O=1.6461_B=0_SR=0.86875_500/test.txt
    common_results/1_L_0.011446_O=2.9968_B=0_SR=0.74779_500/test.txt
    common_results/1_L_0.011487_O=8.5498_B=0_SR=0.84261_500/test.txt' | sort  -k1.31
    
    common_results/1_L_0.010588_O=0.24208_B=0_SR=0.70094_500/test.txt
    common_results/1_L_0.011283_O=1.6461_B=0_SR=0.86875_500/test.txt
    common_results/1_L_0.011446_O=2.9968_B=0_SR=0.74779_500/test.txt
    common_results/1_L_0.010293_O=3.4077_B=1_SR=1.1349_500/test.txt
    common_results/1_L_0.011487_O=8.5498_B=0_SR=0.84261_500/test.txt
    

    根据第一个字段对输出进行排序,第三十一个字符(均基于一个),即O= 之后字符的位置。

    如果数字可能大于或等于 10,您可能还想为 sort 使用 -n 数字标志。否则,27.1828 将被视为小于 3.14159

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 2022-01-28
      • 1970-01-01
      相关资源
      最近更新 更多