Summing a List of Numbers
Problem
You need to sum a list of numbers, including numbers that don’t appear on lines by themselves.
Solution
Use awk both to isolate the field to be
summed and to do the summing. Here we’ll sum up the numbers that are the
file sizes from the output of an ls
-l
command:
$ ls -l | awk '{sum += $5} END {print sum}'
Discussion
We are summing up the fifth field of the ls -l
output. The output of ls-l
looks like this:
-rw-r--r-- 1 albing users 267 2005-09-26 21:26 lilmax
and the fields are: permissions, links, owner, group, size (in
bytes), date, time, and filename. We’re only interested in the size, so
we use $5
in our
awk program to reference that field.
We enclose the two bodies of our awk program
in braces ({}); note that there can be more than one body (or block) of
code in an awk program. A block of code preceded by
the literal keyword END
is only run
once, when the rest of the program has finished. Similarly, you can
prefix a block of code with BEGIN
and supply
some code that will be run before any input is read. The BEGIN
block is useful for initializing
variables, and we could have used one here to initialize sum, but
awk guarantees that variables will start out
empty.
If you look at the output of an ls
-l
command, you will notice that the first line is a total,
and doesn’t fit our expected format for the other lines.
We have two choices for dealing with that. We can pretend it’s not there, which is the approach taken above. Since that undesired line doesn’t ...
Get bash Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.