Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Sum of every lines' value in a file

Answer:

Assume you have a file num.txt

1
2
3
4
5

How do you calculate the sum of every lines' value in a file?

Use Perl!

# perl -lne '$x += $_; END { print $x; }' < num.txt

15
  1. Remove empty lines in a file
  2. Write to the middle of a file in Perl
  3. Remove specified lines from a file using sed
  4. How to count the number of duplicated lines in a file
  5. Delete all trailing blank lines at end of file using sed

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>