gpt4 book ai didi

bash - 使用 "Sort"根据时间戳对终端中的 CSV 进行排序

转载 作者:行者123 更新时间:2023-12-02 04:13:36 26 4
gpt4 key购买 nike

论坛上有多个关于如何使用终端的排序命令对 csv 进行排序的答案,但似乎没有一个对我有用。这是文件结构:

cowboy,16:36:37,2006-03-01,top/adult/arts/animation/anime/resources
pants,14:29:10,2006-03-01,top/adult/arts/magazines and e-zines
women,01:40:40,2006-03-01,top/adult/arts/literature/publishers
happy,05:02:05,2006-03-02,top/adult/arts/comics/free
hen,21:23:37,2006-03-01,top/adult/arts/comics/free
hurts,16:49:59,2006-03-01,top/adult/arts/magazines and e-zines
tent,00:45:26,2006-03-01,top/adult/arts/magazines and e-zines
toon.com,02:00:44,2006-03-01,top/adult/arts/animation/cartoons/members
sim city,18:35:28,2006-03-01,top/adult/arts/magazines and e-zines
superheros,12:34:15,2006-03-01,top/adult/arts/comics/comic strips and panels/members

我尝试了 sort --key=3,2 -t, 但它不起作用。有人可以帮忙吗?

最佳答案

要先按日期排序,然后按时间排序,您可以使用

$ sort -t ',' -k 3,3 -k 2,2 infile

-t 设置分隔符,-k 设置排序依据的字段。请注意,您必须使用 3,3 仅按日期字段排序;仅 3 会使用第三个字段和该行的其余部分进行排序。

2,2 从技术上讲不是必需的,-k 2 也可以。一般来说,限制排序字段使用该行的其余部分可以防止令人惊讶和意外的行为1

由于日期和时间戳完全用零填充,因此无需按数字排序。

1 引用 manual :

For the large majority of applications, treating keys spanning more than one field as numeric will not do what you expect.

一如既往:打破规则是可以的——如果你了解规则的话。

关于bash - 使用 "Sort"根据时间戳对终端中的 CSV 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35179187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com