Appending an element to associative array awk

前端 未结 1 894
名媛妹妹
名媛妹妹 2020-12-22 12:48

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 wa

相关标签:
1条回答
  • 2020-12-22 13:19

    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).
    0 讨论(0)
提交回复
热议问题