Remapping awk array during loop

My problem is this: I had a text file formatted like this:

12 apple 
78 orange 
12 prune
12 prune
78 berries
78 cake

I need to reassign the values ​​in the form:

12 apple, prune
78 orange, berries, cake

I did this with awk. {sbj=$2","; a[$1]=a[$1]sbj}END{for (i in a) print i, a[i]} But the problem is that now I have a file formatted as:

12 apple one
78 orange one

12 prune two
12 prune two
78 berries two
78 cake two

And the output of desire:

12 apple one
78 orange one

12 prune two
78 berries, cake two

I tried to do this by mixing awk and bash, but that didn't help. The only option I can do now is to store each series ("one", "two") in separate files and process them separately using the above code, and then sum all the arrays into a file. But this is a difficult and awkward decision. Is it possible to do this in a single file? Thanks for any advice.

+4
2
$ cat tst.awk
NF {
    if (!seen[$1,$2]++) {
        arr[$1] = ($1 in arr ? arr[$1] "," OFS : "") $2
        sfx[$1] = $3
    }
    next
}
{ prt() }
END { prt() }

function prt(   i) {
    for (i in arr) {
        print i, arr[i], sfx[i]
    }
    print ""
    delete sfx
    delete arr
    delete seen
}

$ awk -f tst.awk file
12 apple one
78 orange one

12 prune two
78 berries, cake two

, in - , . $2 , .

+4

GNU awk :

foo.awk:

!NF{next} # Skip empty lines
{a[$3][$1]=a[$3][$1]" "$2}
END{
    for(i in a){
        for(ii in a[i]){
            print ii" "a[i][ii]" "i
        }
        print ""
    }
}

:

gawk -f foo.awk input.file

, , , gawk: . , for(i in a) . gawk PROCINFO, :

foo.awk:

BEGIN {
    PROCINFO["sorted_in"] = "@ind_str_asc"
}
!NF{next} # Skip empty lines
{a[$3][$1]=a[$3][$1]" "$2}
END{
    for(i in a){
        for(ii in a[i]){
            print ii" "a[i][ii]" "i
        }
        print ""
    }
}
+1

Source: https://habr.com/ru/post/1669387/


All Articles