gpt4 book ai didi

linux - 搜索文件中的重复行,计算它和哪个位置(行号)而不排序?

转载 作者:太空狗 更新时间:2023-10-29 11:49:04 25 4
gpt4 key购买 nike

我一直在阅读类似的问题 here ,但是提供的答案没有行号。

[root@test ~]# cat -n file 
1 123
2 123
3 234
4 234
5 123
6 345
[root@test ~]#

[root@test ~]# sort file | uniq -c
3 123
2 234
1 345
[root@test ~]#

我要找的是类似 this 的东西,但在 Linux shell 脚本(首选)或任何其他脚本解决方案中。

textmechanic.com 提供的输出

( 2 dupe of 1 ): 123 
( 4 dupe of 3 ): 234
( 5 dupe of 1 ): 123

最佳答案

你可以使用awk:

awk '{if ($1 in a) printf "( %d dupe of %d ): %s\n", NR, a[$1], $1; else a[$1] = NR}' file

( 2 dupe of 1 ): 123
( 4 dupe of 3 ): 234
( 5 dupe of 1 ): 123

关于linux - 搜索文件中的重复行,计算它和哪个位置(行号)而不排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48984027/

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