尴尬合并文件问题

发布于 2025-02-13 20:19:04 字数 1463 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森罗 2025-02-20 20:19:04

我将使用gnu awk进行此任务以下方式,让input1.txt content be

113812627 <[email protected]>
113812628 <[email protected]>
113812629 <[email protected]>

and input2.txt2.txt be be

113812627 <[email protected]>
113812628 <[email protected]>
113812627 <[email protected]>
113812628 <[email protected]>
113812629 <[email protected]>
113812629 <[email protected]>

提供

awk '{arr[$1] = arr[$1] " " $2}END{for(i in arr){printf "%s%s\n",i,arr[i]}}' input1.txt input2.txt

输出

113812627 <[email protected]> <[email protected]> <[email protected]>
113812628 <[email protected]> <[email protected]> <[email protected]>
113812629 <[email protected]> <[email protected]> <[email protected]>

说明:我将每行我附加到第1个提交中显示的密钥($ 1)和第二个字段($ 2),如果尚不存在第二场是地方。在对数组中的每个键处理所有键i printf该键下的所有键后,请注意第一个参数中不需要空格,因为它已经是每个值的第一个字符。

(在GAWK 4.2.1中测试)

I would use GNU AWK for this task following way, let input1.txt content be

113812627 <[email protected]>
113812628 <[email protected]>
113812629 <[email protected]>

and input2.txt content be

113812627 <[email protected]>
113812628 <[email protected]>
113812627 <[email protected]>
113812628 <[email protected]>
113812629 <[email protected]>
113812629 <[email protected]>

then

awk '{arr[$1] = arr[$1] " " $2}END{for(i in arr){printf "%s%s\n",i,arr[i]}}' input1.txt input2.txt

gives output

113812627 <[email protected]> <[email protected]> <[email protected]>
113812628 <[email protected]> <[email protected]> <[email protected]>
113812629 <[email protected]> <[email protected]> <[email protected]>

Explanation: for each line I append to value under key shown in 1st filed ($1) space and 2nd field ($2), if it was not yet present, space followed by content of 2nd field is place. After all lines are processed for each key in array I printf that key and value under that key, note that space is not required in 1st argument as it is already 1st character of each value.

(tested in gawk 4.2.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文