合并来自 uniq -c 的结果
我有很多包含命令结果的文件: uniq -c some_file > some_file.out
例如: 1.out:
1 a 2 b 4 c
2.out
2 b 8 c
我想合并这些结果,所以我得到:
1 a 4 b 12 c
我认为 sort 或 uniq 可以处理它,但我没有看到任何与之相关的选项。 编写一些 ruby/perl 脚本是一种方法,但我想使用核心 *nix 命令(如提到的 sort 和 uniq)轻松完成它。
编辑: 要明确的是。我没有原始文件,我必须合并 *.out 文件。
感谢您的帮助!
I have many files with results of command:
uniq -c some_file > some_file.out
For example:
1.out:
1 a 2 b 4 c
2.out
2 b 8 c
I would like to merge these results, so I get:
1 a 4 b 12 c
I thought that sort or uniq could handle it but I don't see any option related to it.
Writing some ruby/perl script is one of way to go but I'd like to do it easly with core *nix commands (like mentioned sort and uniq).
Edit:
To be clear. I don't have original files and I have to merge *.out files.
Thanks for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
awk
尝试一下:Try it with
awk
:这是一个非常具体的问题,因此不可能有任何工具默认会执行此操作。您可以在足够小的循环中编写脚本(不需要 awk 讨厌的东西),用任何脚本语言(甚至 sh)实现。我不认为还有其他方法。
It's quite a specific problem, so it's unlikely any tool will do this by default. You can script it in a small enough loop (no need for awk nastyness), implemented in any scripting language (even sh). I don't think there's another way.
这并不是很严重(但它有效)。我喜欢飞利浦解决方案。
This is not quite serious (but it works). I like Philipps solution.
接受的答案适用于问题中提供的特定值。但是,如果
uniq -c
的输出包含的空格多于计数和值之间的空格,则以下awk
脚本不会截断第二个字段之后的输出:The accepted answer works for the specific values provided in the question. If the output of
uniq -c
contains more spaces than just the one between the count and the value however, the followingawk
script does not truncate output after the second field: