Bash:按最后一个字段值对文本文件进行排序 [英] Bash: sort text file by last field value

查看:19
本文介绍了Bash:按最后一个字段值对文本文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 ~300k 行的文本文件.每行都有不同数量的逗号分隔字段,其中最后一个保证是数字字段.我想按最后一个数字字段对文件进行排序.我做不到:

I have a text file containing ~300k rows. Each row has a varying number of comma-delimited fields, the last of which is guaranteed numerical. I want to sort the file by this last numerical field. I can't do:

sort -t, -n -k 2 file.in > file.out

因为每行中的字段数不是恒定的.我认为 sed, awk 也许是答案,但不确定如何.例如:

as the number of fields in each row is not constant. I think sed, awk maybe the answer, but not sure how. E.g:

awk -F, '{print $NF}' file.in

给我最后一列的值,但如何使用它对文件进行排序?

gives me the last column value, but how to use this to sort the file?

推荐答案

使用 awk 将数字键放在前面.$NF 是当前记录的最后一个字段.种类.使用 sed 删除重复键.

Use awk to put the numeric key up front. $NF is the last field of the current record. Sort. Use sed to remove the duplicate key.

awk -F, '{ print $NF, $0 }' yourfile | sort -n -k1 | sed 's/^[0-9][0-9]* //'

这篇关于Bash:按最后一个字段值对文本文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆