Bash/Linux使用自定义字段分隔符按第三列排序 [英] Bash/Linux Sort by 3rd column using custom field seperator
问题描述
我似乎无法按照自己的意愿对以下数据进行排序;
I can't seem to sort the following data as I would like;
find output/ -type f -name *.raw | sort
output/rtp.0.0.raw
output/rtp.0.10.raw
output/rtp.0.11.raw
output/rtp.0.12.raw
output/rtp.0.13.raw
output/rtp.0.14.raw
output/rtp.0.15.raw
output/rtp.0.16.raw
output/rtp.0.17.raw
output/rtp.0.18.raw
output/rtp.0.19.raw
output/rtp.0.1.raw
output/rtp.0.20.raw
output/rtp.0.2.raw
output/rtp.0.3.raw
output/rtp.0.4.raw
output/rtp.0.5.raw
output/rtp.0.6.raw
output/rtp.0.7.raw
output/rtp.0.8.raw
output/rtp.0.9.raw
在上面的示例中,我没有将任何参数传递给sort
命令.无论我使用了什么选项,我都无法接近我想要的结果.我想要以下输出;
In the above example I haven't passed any arguments to the sort
command. No matter what options I used I can't get closer to my desired results. I would like the following output;
find output/ -type f -name *.raw | sort
output/rtp.0.0.raw
output/rtp.0.1.raw
output/rtp.0.2.raw
output/rtp.0.3.raw
output/rtp.0.4.raw
output/rtp.0.5.raw
output/rtp.0.6.raw
output/rtp.0.7.raw
output/rtp.0.8.raw
output/rtp.0.9.raw
output/rtp.0.10.raw
output/rtp.0.11.raw
output/rtp.0.12.raw
output/rtp.0.13.raw
output/rtp.0.14.raw
output/rtp.0.15.raw
output/rtp.0.16.raw
output/rtp.0.17.raw
output/rtp.0.18.raw
output/rtp.0.19.raw
output/rtp.0.20.raw
我尝试使用-t .
选项将字段分隔符设置为句号.我还尝试了-k
选项来指定字段,以及-g
,-h
,-n
,但是没有一个选项有帮助.除非我没有正确理解手册页并忽略了我的回答,否则我在手册页中看不到其他任何可以满足我要求的内容.
I have tried with -t .
option to set a field separator to the full stop. Also I have experimented with the -k
option to specify the field, and -g
, -h
, -n
, but none of the options are helping. I can't see anything else in the man pages that would do as I require, unless I haven't understood the man pages correctly and overlooked my answer.
我可以用sort
产生所需的结果吗?如果可以,怎么办?
Can I produce the results I require with sort
, and if so, how?
此外,它很少见,但有时一直显示为"0"的第二列可能会增加.可以将其归类吗?
Additionally, it's vary rare but sometimes the 2nd column which shows as '0' all the way down may increment. Can that be factored into the sort?
推荐答案
这使得它:
$ sort -t'.' -n -k3 a
output/rtp.0.0.raw
output/rtp.0.1.raw
output/rtp.0.2.raw
output/rtp.0.3.raw
output/rtp.0.4.raw
output/rtp.0.5.raw
output/rtp.0.6.raw
output/rtp.0.7.raw
output/rtp.0.8.raw
output/rtp.0.9.raw
output/rtp.0.10.raw
output/rtp.0.11.raw
output/rtp.0.12.raw
output/rtp.0.13.raw
output/rtp.0.14.raw
output/rtp.0.15.raw
output/rtp.0.16.raw
output/rtp.0.17.raw
output/rtp.0.18.raw
output/rtp.0.19.raw
output/rtp.0.20.raw
如您所见,我们需要不同的选择:
As you see we need different options:
-
-t'.'
将点.
设置为字段分隔符. -
-n
进行数字排序. -
-k3
检查第三列.
-t'.'
to set the dot.
as the field separator.-n
to make it numeric sort.-k3
to check the 3rd column.
这也可以做到:
$ sort -t'.' -V -k2 a
output/rtp.0.0.raw
output/rtp.0.1.raw
output/rtp.0.2.raw
output/rtp.0.3.raw
output/rtp.0.4.raw
output/rtp.0.5.raw
output/rtp.0.6.raw
output/rtp.0.7.raw
output/rtp.0.8.raw
output/rtp.0.9.raw
output/rtp.0.10.raw
output/rtp.0.11.raw
output/rtp.0.12.raw
output/rtp.0.13.raw
output/rtp.0.14.raw
output/rtp.0.15.raw
output/rtp.0.16.raw
output/rtp.0.17.raw
output/rtp.0.18.raw
output/rtp.0.19.raw
output/rtp.0.20.raw
如您所见,我们需要不同的选择:
As you see we need different options:
-
-t'.'
将点.
设置为字段分隔符. -
-V
使其根据版本进行排序. -
-k2
检查第二列.
-t'.'
to set the dot.
as the field separator.-V
to make it sort based on version.-k2
to check the 2nd column.
这篇关于Bash/Linux使用自定义字段分隔符按第三列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!