53

My file is delimited by a comma which gives 64 columns. I extracted the field as shown below:

awk '{split($0,a,",");  print a[57]}'

How can I compute the sum of the values in columns 57 with my command?

Johan
  • 74,508
  • 24
  • 191
  • 319
Ajo
  • 643
  • 1
  • 5
  • 4

2 Answers2

137

The split seems unnecessary here, especially considering you're using awk, which is made for field based processing. If your file is truly comma-separated, the following code seems much simpler, IMO:

awk -F',' '{sum+=$57;} END{print sum;}' file.txt

For example, given the following input:

    ~$ cat testawk.txt
    a,a,aa,1
    a,a,aa,2
    d,d,dd,7
    d,d,dd,9
    d,dd,d,0
    d,d,dd,23
    d,d,dd,152
    d,d,dd,7
    d,d,dd,5
    f2,f2,f2,5.5

We can get the sum like:

~$ awk -F',' '{sum+=$4;}END{print sum;}' testawk.txt
   216.5

The -F',' tells awk that the field separator for the input is a comma.

The {sum+=$4;} adds the value of the 4th column to a running total.

The END{print sum;} tells awk to print the contents of sum after all lines are read.

zzevannn
  • 3,414
  • 2
  • 12
  • 20
  • 1
    why do you need a semicolon after sum? – Blaisem Mar 12 '19 at 13:26
  • 4
    @Blaisem - I work with Java a lot and am in the habit of ending statements with them. `awk` uses them as the delimitter between commands, and this specific code would work with or without the semi's as there are no sequential actions. I've editted the answer to be more consistent in it's usage of semi's since it seems to sporadically get upvotes and views. – zzevannn Mar 12 '19 at 19:52
  • @Blaisem if you do one-liner commands you use semicolon a lot. If you split the awk command in lines they are not needed. – Smeterlink Aug 16 '20 at 18:04
22

You could sum the column 57 of your file, and print it using

awk '{split($0,a,","); sum += a[57]} END {print sum}' 
mpromonet
  • 11,326
  • 43
  • 62
  • 91