Commad to get sum of numbers in a text file

To get some of numbers in a text file use following one liner

$ awk '{s+=$0} END {print s}' <inputTextFile>

or

$ cat inputTextFile | awk '{s+=$0} END {print s}'

Example:

Lets say my input.txt contains following lines:

$ cat input.txt

100
24
200

To get sum of the numbers in input.txt file use either of following command;

$ cat input.txt | awk '{s+=$0} END {print s}'

or

$ cat input.txt | awk '{s+=$0} END {print s}'

For both commands output is 324

-Sany

Advertisements

2 comments on “Commad to get sum of numbers in a text file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s