SonicProtein
SonicProtein

Reputation: 850

Appending an element to associative array awk

I've got an input file (input.txt) with a few fields:

A1  B1  C1  D1  E1
A2  B2  C2  D1  E2
A3  B3  C3  D2  E3
A4  B4  C4  D2  E4

And I want to append elements of an associative array,

awk '{a[$4]=a[$4] $5; print a[$4]} END {for(b in a) {print a[b]}}' input.txt

I think the output should be (ie E2 is concatenated to E1, and E4 is concatenated to E3):

E1 E2
E3 E4

but instead the output is:

E2
E4

I'm not sure what's wrong with my code?

Upvotes: 0

Views: 1781

Answers (1)

mklement0
mklement0

Reputation: 439417

Your output isn't consistent with your command, but I assume that you want the following:

  • build up a list of 5th-column values for each unique 4th-column value
  • print these lists, preceded by the respective 4th-column value

A naïve fix to get what you want would be:

$ awk '{a[$4]=a[$4] " " $5} END {for (b in a) { print b; print a[b]}}' input.txt
D1
 E1 E2
D2
 E3 E4

but there are two things to note:

  • The accumulated 5-th column values will have a leading space - which happens to help with grouped output in this case.
  • Due to enumerating the keys with for (b in a), the 4th-column values will NOT appear in the order they appear in the input, because the order in which awk enumerates keys of its [always associative] arrays is based on internal hash values, which has no guaranteed relationship to the order in which array elements were added (nor does it guarantee any particular order in general).

Upvotes: 2

Related Questions