简体   繁体   English

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

In the above example I haven't passed any arguments to the sort command. 在上面的示例中,我没有将任何参数传递给sort命令。 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

I have tried with -t . 我用-t .尝试过-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. 我还尝试了-k选项来指定字段,以及-g-h-n ,但是没有一个选项有帮助。 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. 除非我没有正确理解手册页并忽略了答案,否则我在手册页中看不到其他任何所需的功能。

Can I produce the results I require with sort , and if so, how? 我可以使用sort产生所需的结果吗?如果可以,怎么办?

Additionally, it's vary rare but sometimes the 2nd column which shows as '0' all the way down may increment. 此外,这种情况很少见,但有时一直显示为“ 0”的第二列可能会增加。 Can that be factored into the sort? 可以将其归类吗?

This makes it: 这使得:

$ 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'.' to set the dot . 设置点. as the field separator. 作为字段分隔符。
  • -n to make it numeric sort. -n使其成为数字排序。
  • -k3 to check the 3rd column. -k3检查第三列。

Update 更新资料

This also makes it: 这也使得:

$ 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'.' to set the dot . 设置点. as the field separator. 作为字段分隔符。
  • -V to make it sort based on version. -V以使其根据版本进行排序。
  • -k2 to check the 2nd column. -k2检查第二列。

Fedorqui's solution is good (+1), but not all versions of sort support -V . Fedorqui的解决方案很好(+1),但并非所有版本的sort支持-V For those versions that do not, you need to do a little more work than fedorqui's original solution. 对于那些没有的版本,您需要做的工作比fedorqui的原始解决方案还要多。 This should suffice: 这样就足够了:

sort -t. -k2,2 -k3,3 -n

You get a slightly different sort (eg, '05' sorts before '1' instead of after) if you use: 如果使用以下命令,您得到的排序会稍有不同(例如,“ 05”在“ 1”之前而不是之后):

sort -t. -k2g

(Note that -g is also non-standard, and not available in all versions of sort ). (请注意-g也是非标准的,并非在sort所有版本中都可用)。

sort -t'.' -n -k3

to sort the 3th column from smallest to biggest 将第3列从最小到最大排序

And if you want to sort from biggest to smallest, you can use '-r' option : 如果要从最大到最小排序,可以使用'-r'选项:

sort -t'.' -n -r -k3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM