2011年12月6日 星期二

Using Linux command: awk to sum up a list of number

Linux Command Line
AWK

Example
> cat Text.txt
1
2
3
4
5

If you want to get the sum of this column, you can use the tool awk.
cat Text.txt | awk '{sum += $1} END {print sum}'

Output:
15

If you want to show the information of the file together with the sum, you can enter the command as below
cat Text.txt | awk '{sum += $1} {print} END {print sum}

Output:
1
2
3
4
5
15



AWK
The AWK utility is a data extraction and reporting tool


Reference
vbird:

沒有留言:

張貼留言