gpt4 book ai didi

Linux排序命令: keys with the same start but different length are not sorted in the same order

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:24 27 4
gpt4 key购买 nike

我正在尝试对两个文件进行排序以便将它们连接起来。我排序的一些键非常相似,这似乎导致了问题。例如,我有两个 key ,分别是 a1kea1k3-b3。我正在使用命令:

sort -nk1 file.txt -o file.txt

在一个文件中,它们按此顺序出现,而在另一个文件中,它们以相反的顺序出现。当我尝试加入文件时,这会导致问题。

如何对这些文件进行排序,使它们保持相同的顺序?

谢谢

最佳答案

不要使用“-n”选项,该选项根据字符串数值进行比较。

-n
Compare according to arithmetic value an initial numeric string consisting of optional white
space, an optional - sign, and zero or more digits, optionally followed by a decimal point and
zero or more digits.

您的 key 是字符串,而不是数字。

相反,你应该这样做:

sort -k1 file.txt -o file.txt

其他信息:

您可以看到,当使用 -n 进行唯一排序时,sort 会认为您的键相同:

sort -un file

您将看到 a1k3-b3a1ke 被视为相等(因此仅发出一个)。如果你这样做:

sort -u file

结果将包含 a1k3-b3a1ke,这正是您想要的。

关于Linux排序命令: keys with the same start but different length are not sorted in the same order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18448062/

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