gpt4 book ai didi

sed - 基于文件中第一个字段的唯一行数

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

我正在尝试根据第一个文件计算输出到文件的唯一行数字段,其中输入行如下所示:

Forms.js     /forms/Forms.js     http://www.gumby.com/test.htm   404
Forms.js /forms/Forms1.js http://www.gumby.com/test.htm 404
Forms.js /forms/Forms2.js http://www.gumby.com/test.htm 404
Interpret.js /forms/Interpret1.js http://www.gumby.com/test.htm 404
Interpret.js /forms/Interpret2.js http://www.gumby.com/test.htm 404
Interpret.js /forms/Interpret3.js http://www.gumby.com/test.htm 404

像这样:

3    Forms.js    /forms/Forms.js     http://www.gumby.com.mx/test.htm 404
3 Interpret.js /forms/Interpret.js http://www.gumby.com.mx/test.htm 404

我一直在尝试 sort 和 uniq 的各种组合,但还没有成功。我可以使用整行来获得不同的行,但我只想要第一个字段。我目前正在使用 cygwin。我不识字,但我怀疑那是要走的路。谁有方便的解决方案?

最佳答案

这个:

<infile awk '{ h[$1]++ } END { for(k in h) print h[k], k }'

会让你:

3 Forms.js
3 Interpret.js

如果您还想保留第一次命中,请使用:

awk '!h[$1] { g[$1]=$0 } { h[$1]++ } END { for(k in g) print h[k], g[k] }'

输出:

3 Forms.js /forms/Forms.js http://www.gumby.com/test.htm 404
3 Interpret.js /forms/Interpret1.js http://www.gumby.com/test.htm 404

使用 GNU awk 测试。

请注意,这不需要对输入进行排序。另请注意,结果是无序的。

关于sed - 基于文件中第一个字段的唯一行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13822397/

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