在 shell 中将对列表转换为表,而不使用 awk

发布于 2024-10-17 14:36:47 字数 595 浏览 4 评论 0原文

我有一个制表符分隔的对列表,如下所示:

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 技术交流群。

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

发布评论

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

评论(1

倚栏听风 2024-10-24 14:36:47

在 awk 中:

awk '{num[$1,$2]++; fruits[$1]=1; colors[$2]=1}END{for(i in fruits) {for(j in colors) printf("%d ", num[i,j]); printf("\n");}}'

PS。作为事后的想法...您可以查看 join 实用程序。与按字段计数配对也许可以解决问题。但我保证它会变得更加毛茸茸的。

聚苯硫醚。我会在这里添加它,因为评论框太狭窄了。
Alexander,您需要一些可以在 POSIX 系统上运行的东西。该任务涉及一些逻辑。无论是将其放入工具的脚本中,还是包含多个命令的长管道中 - 数量都保持大致相同。由于 awk 旨在生成报告,因此在这种情况下它是一个很好的工具。
基本上,您没有太多方法来格式化文本 - 它是 printf utility/builtin 或 awk。在前一种情况下,这意味着脚本中大约有三行,还需要更多行才能产生结果。所以我认为没有更短的方法。但这是我有限但长期的经验所得出的理论。我也想知道一种更简单的方法,如果有的话,我也想学习:)

In awk:

awk '{num[$1,$2]++; fruits[$1]=1; colors[$2]=1}END{for(i in fruits) {for(j in colors) printf("%d ", num[i,j]); printf("\n");}}'

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 or awk. 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:)

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