Counting Column Contents

[problem]

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

[/problem]

[solution]

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

[/solution]

[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

[/example]

[reference]

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

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

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