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

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

问题描述

我有一个包含30万〜行的文本文件。每一行都有不同数量的逗号分隔的字段,其中最后保证数字的。我想通过这最后的数字领域的文件进行排序。我不能做的:

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]* //'

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

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