Domanda

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.

È stato utile?

Soluzione

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:)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top