Counting Column Contents

Problem

You want to count the contents of a column. Accumulatively totaling each number in a given column.



Solution

To total up the contents of a given column, use following awk code:



Example



nawk -v col=# ' { tot+=$col } END { print tot } '

In this example, do a du of the current directory and then total column one (1). Notice awk receives a variable on the column to sum up.


du -ks * | awk -v col=1 ' { tot+=$col } END { print tot } '
11168



Reference

[tags]UNIX, awk, nawk, gawk, Unix Coding School[/tags]



Leave a Reply

Your email address will not be published. Required fields are marked *