Bash:按最后一个字段值对文本文件进行排序

发布于 2024-09-25 13:35:09 字数 286 浏览 6 评论 0原文

我有一个包含约 300k 行的文本文件。每行都有不同数量的逗号分隔字段,最后一个字段保证为数字。我想按最后一个数字字段对文件进行排序。我不能这样做:

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

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一桥轻雨一伞开 2024-10-02 13:35:09

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

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

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]* //'
辞慾 2024-10-02 13:35:09
vim file.in -c '%sort n /.*,\zs/' -c 'saveas file.out' -c 'q'
vim file.in -c '%sort n /.*,\zs/' -c 'saveas file.out' -c 'q'
骷髅 2024-10-02 13:35:09

也许在排序之前反转文件中每一行的字段? 类似的东西

perl -ne 'chomp; print(join(",",reverse(split(","))),"\n")' |
  sort -t, -n -k1 |
  perl -ne 'chomp; print(join(",",reverse(split(","))),"\n")'

只要不以任何方式引用逗号, 就应该这样做。如果这是一个完整的 CSV 文件(其中逗号可以用反斜杠或空格引用),那么您需要一个真正的 CSV 解析器。

Maybe reverse the fields of each line in the file before sorting? Something like

perl -ne 'chomp; print(join(",",reverse(split(","))),"\n")' |
  sort -t, -n -k1 |
  perl -ne 'chomp; print(join(",",reverse(split(","))),"\n")'

should do it, as long as commas are never quoted in any way. If this is a full-fledged CSV file (in which commas can be quoted with backslash or space) then you need a real CSV parser.

这个俗人 2024-10-02 13:35:09

Perl 一行:

@lines=<STDIN>;foreach(sort{($a=~/.*,(\d+)/)[0]<=>($b=~/.*,(\d+)/)[0]}@lines){print;}

Perl one-liner:

@lines=<STDIN>;foreach(sort{($a=~/.*,(\d+)/)[0]<=>($b=~/.*,(\d+)/)[0]}@lines){print;}
擦肩而过的背影 2024-10-02 13:35:09

我将把我的作为替代方案(我无法让 awk 工作):)

示例文件:

Call of Doody                           1322
Seam the Ripper                         1329
Mafia Bots 1                            1109
Chicken Fingers                         1243
Batup Light                             1221
Hunter F Tomcat                         1140
Tober                                   0833

代码:

for i in `sed -e 's/.* \(\d\)*/\1/' file.txt | sort`; do grep $i file.txt; done > file_sort.txt

I'm going to throw mine in here as an alternative (and I couldn't get awk to work) :)

sample file:

Call of Doody                           1322
Seam the Ripper                         1329
Mafia Bots 1                            1109
Chicken Fingers                         1243
Batup Light                             1221
Hunter F Tomcat                         1140
Tober                                   0833

code:

for i in `sed -e 's/.* \(\d\)*/\1/' file.txt | sort`; do grep $i file.txt; done > file_sort.txt
芸娘子的小脾气 2024-10-02 13:35:09

Python 一行:

python -c "print ''.join(sorted(open('filename'), key=lambda l: int(l.split(',')[-1])))"

Python one-liner:

python -c "print ''.join(sorted(open('filename'), key=lambda l: int(l.split(',')[-1])))"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文