在命令行上如何从第n个索引开始按升序排序

时间:2015-02-17 08:44:31

标签: sorting command-line ls

=> “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

1 个答案:

答案 0 :(得分: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的可能性,您可能还想使用-n的{​​{1}}数字标记。如果不这样做,sort将被视为小于27.1828