Wednesday, August 21, 2013

To add up column values in a file - UNIX

To all up all the values in a particular column of a file in unix use below command

Ex: sample.txt contains below data
10 20 30
40 50 60
70 80 90

Now I want to add all values in column two of above file sample.txt

$ cat sample.txt | awk -F' '  '{print $2}'  | awk '{total = total +$1}END{print total}'

Output: 150

No comments: