gpt4 book ai didi

sorting - 使用一行的前六个字符在unix中对文件进行排序

转载 作者:行者123 更新时间:2023-12-04 11:22:07 25 4
gpt4 key购买 nike

我想使用一行的前六个字符对文件进行排序。
它应该忽略第六个字符之后的默认排序顺序。
我尝试使用以下命令,但系统在第六个字符之后采用默认排序顺序。

sort -k 1,6 filename.txt

输入文件:“文件名.txt”
09289720150531N201505220820D20150514
09289720150531N201505220820A20150516
08806020150531N201505290810D20150526
08806020150531N201505290810A20150528

输出应该是:
08806020150531N201505290810D20150526
08806020150531N201505290810A20150528
09289720150531N201505220820D20150514
09289720150531N201505220820A20150516

但我的命令输出是:
08806020150531N201505290810A20150528
08806020150531N201505290810D20150526
09289720150531N201505220820A20150516
09289720150531N201505220820D20150514

最佳答案

如图所示的选项使用字段位置。如果您将其更改为类似 -k1.1,1.6 的内容它将使用第一个字段中的字符位置。这是一个 extended POSIX功能,可能在大多数平台上提供。

但是,在您的示例中,字符位置 1-6 中只有两个不同的值:088060092897 .标准排序命令没有忽略列的功能,但仅适用于使用列。虽然 GNU sort 提供了一个扩展( -s 用于“禁用最后的比较”),但 Solaris sort 没有这样的扩展。在考虑了排序键之后,它会按行的其余部分进行排序。

它的手册中有一些模糊的措辞暗示-u会做你想做的:

When there are multiple key fields, later keys are compared only after all earlier keys compare equal. Except when the -u option is specified, lines that otherwise compare equal are ordered as if none of the options -d, -f, -i, -n or -k were present (but with -r still in effect, if it was specified) and with all bytes in the lines significant to the comparison.



然而——重新审视这一点——自 -u 以来的措辞具有误导性。用于过滤重复项。

评论表明 -k1.1,1.6可以缩短为 -k1.6 ,并在 Solaris 10 上进行测试确认可以正常工作。那就是 /usr/bin/sort , 当然。在我的 Solaris 10 副本中,还有一个 sort 副本,在 /opt/sfw/bin/sort 中。 :
$ /opt/sfw/bin/sort --version
sort (GNU coreutils) 5.97

并且该程序支持 -s上面提到的选项。使用该选项,程序会生成请求的输出。

关于sorting - 使用一行的前六个字符在unix中对文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30569957/

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