在 shell 中将对列表转换为表,而不使用 awk
我有一个制表符分隔的对列表,如下所示:
apple yellow
orange green
apple red
pear blue
apple yellow
apple yellow
我想使用 Linux 命令行工具将其转换为表:
yellow green red blue
apple 3 0 1 0
orange 0 1 0 0
pear 0 0 0 1
我可以用最少的手动脚本来完成此操作吗?
注意:我知道如何编码,谢谢,问题是关于预先存在的工具,可能使用最小脚本粘合。而 awk
程序,除非它们非常短,否则算作“脚本”。
注2:这是一个学习问题。我不太关心解决方案是短还是长(不过较短的更好)。我想学习解决这个问题的其他方法。
如果我想以最快的方式解决这个问题,我不会在这里问这个问题,我会花 30 秒用我最了解的语言写三行。
I have a tab-delimited list of pairs like this:
apple yellow
orange green
apple red
pear blue
apple yellow
apple yellow
I want to convert it, using Linux command-line tools, to table:
yellow green red blue
apple 3 0 1 0
orange 0 1 0 0
pear 0 0 0 1
Can I do this with minimum scripting by hand?
Note: I know how to code this, thank you, the question is about pre-existing tools, possibly with minimal script glue. And awk
programs, unless they are very short, count as "scripting" for that matter.
Note 2: This is a learning question. I do not care much if solution is short or long (shorter are preferable though). I want to learn other ways of solving this problem.
If I wanted to solve this problem in the fastest way, I would not ask this question here, I would go and spend 30 seconds on writing three lines in the language I know best.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 awk 中:
PS。作为事后的想法...您可以查看
join
实用程序。与按字段计数配对也许可以解决问题。但我保证它会变得更加毛茸茸的。聚苯硫醚。我会在这里添加它,因为评论框太狭窄了。
Alexander,您需要一些可以在 POSIX 系统上运行的东西。该任务涉及一些逻辑。无论是将其放入工具的脚本中,还是包含多个命令的长管道中 - 数量都保持大致相同。由于
awk
旨在生成报告,因此在这种情况下它是一个很好的工具。基本上,您没有太多方法来格式化文本 - 它是
printf
utility/builtin 或awk
。在前一种情况下,这意味着脚本中大约有三行,还需要更多行才能产生结果。所以我认为没有更短的方法。但这是我有限但长期的经验所得出的理论。我也想知道一种更简单的方法,如果有的话,我也想学习:)In awk:
PS. As an afterthought... You can look into
join
utility. Paired with counts by fields maybe this will do the trick. But i promise it will be hairier.PPS. I'll add it here, as comment box is too cramped.
Alexander, you need something to run on POSIX system. There's some amount of logic involved in the task. Be it put into a script of a tool, or a long pipe with several commands - the amount stays roughly the same. As
awk
was designed to produce reports, it's the good tool in this case.Basically you don't have many means to format the text - it's
printf
utility/builtin orawk
. In the former case it means about three lines in the script and some more to produce the result. So i think thare's no shorter way. But's kind of theoretical from my limited albeit prolonged experience. I would also like to know an easier way if there's one, i like to learn too:)