Search code examples
bashshellawklinescounting

BASH counting numbers on all lines


I need help with my bash script.

The task is counting total size of files in directory. I already did it ( using ls, awk and grep). My output may look like this for example:

1326
40
598
258
12
$

These numbers means size of files in directory. I need to count them all and I stuck here. So I would be really grateful if someone could tell me how to count them all (and find the total size of files in directory)

Thank you


Solution

  • awk to the rescue!

    awk '$1+0==$1{sum+=$1; count++} END{print sum, count}'
    

    adds up and counts all the numbers ($1+0==$1 for a number, but not for a string) and print them the sum and count when done.