Counting Column Contents


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



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



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 } '



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


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 *