Understanding Piping in AWK

smilingbuddha

I found this example in a lecture by Brian Kernighan. to add together the prices of food items in a two column table

#!/usr/bin/awk -f

{amount[$1] += $2}

END {for (name in amount)
           print name, amount[name] | "sort -k1 "
     print "Finished processing everything!"
        }

The last print statement print "Finished...." is my own little addition.

For the input

pizza 200
beer 100
pizza 500
beer 50

it produces the output

Finished processing everything!
beer 150
pizza 700

i.e. it adds up the prices of beer, and pizza entries together. What I don't get is how the last print statement that I added was executed before the for loop.

Also why is the | sort... statement inside the for loop? Shouldn't it be after the for loop after all the printing has been done, sort of like how one would do the same thing at the bash command line via cat file | sort?

user1141630

AWK(1P) has a section about Output Statements

In awk piping creates a stream which remains open until you close it. This allows it to be used in-loop. Using your example:

{amount[$1] += $2}
END {
    for (name in amount)
        print name, amount[name] | "sort -k1"
    close("sort -k1")
    print "Finished processing everything!"
}

This closes sort -k1, allowing it to be printed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related