AWK one liner – Getting Sum of a Column

The following AWK one liner that can be used to get sum of a column in a text file:

Here is the sample of file content: cat file_size.txt

-rw——- 1 oracle oinstall 4096 Jun 8 18:24 file0000018500000001 -rwxr-xr-x 1 oracle oinstall 19 Jun 14 13:33 file0000034c00000001 -rw——- 1 oracle oinstall 18 Jun 14 13:34 file0000035400000002 -rwxr-xr-x 1 oracle oinstall 0 Jun 14 13:34 file0000035700000003 -rwxr-xr-x 1 oracle oinstall 0 Jun 14 13:35 file0000036400000004 -rw-r–r– 1 oracle oinstall 0 Jun 26 15:30 file000051b700000001 -rw-r–r– 1 oracle oinstall 0 Jun 26 16:24 file000051c300000002 -rw——- 1 oracle oinstall 4096 Jun 28 13:22 file000051ca00000003 -rw-r–r– 1 oracle oinstall 0 Jun 28 13:37 file000051cf00000004 -rw——- 1 oracle oinstall 4096 Jun 28 13:44 file000051d600000005 AWK one liner to get the sum of column 5, i.e size of the file:

cat file_size.txt | awk '{ tot +=$5 } END {print tot }' 12325

Leave a Reply

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