tags:

views:

4736

answers:

7

I'm sure there is a quick and easy way to calculate the sum of a column of values on Unix systems (using something like awk or xargs perhaps), but writing a shell script to parse the rows line by line is the only thing that comes to mind at the moment.

For example, what's the simplest way to modify the command below to compute and display the total for the SEGSZ column (70300)?

ipcs -mb | head -6
IPC status from /dev/kmem as of Mon Nov 17 08:58:17 2008
T         ID     KEY        MODE        OWNER     GROUP      SEGSZ
Shared Memory:
m          0 0x411c322e --rw-rw-rw-      root      root        348
m          1 0x4e0c0002 --rw-rw-rw-      root      root      61760
m          2 0x412013f5 --rw-rw-rw-      root      root       8192
+2  A: 

You could start by running the data through cut - which would at least trim the columns down.

You should then be able to pipe that into grep, stripping-out non-numerics.

Then ... well, then I'm not sure. It might be possible to pipe that to bc. If not, it could certainly be handed to a shell script to add each item.

If you used tr to change the newlines (\n) to spaces ( ), and piped that through xargs into your script that loops until there are no more inputs, adding each one, you may have an answer.

So, something akin to the following:

cat <whatever> | cut -d'\t` -f7 | grep -v <appropriate-character-class> | tr '\n' ' ' | xargs script-that-adds-arguments

I may have the cut flags slightly wrong - but man is your friend :)

warren
+15  A: 
ipcs -mb | tail +4 | awk '{ sum += $7 } END { print sum }'

Or without tail:

ipcs -mb | awk 'NR > 3 { sum += $7 } END { print sum }'

Using awk with bc to have arbitrary long results (credits to Jouni K.):

ipcs -mb | awk 'NR > 3 { print $7 }' | paste -sd+ | bc
Johannes Schaub - litb
+1 .. removed my duplicate :-)
toolkit
Thanks, very helpful! Running that command, I get this result: 6.59246e+08. Any way to force awk to display the exact value (rather than scientific notation)?
Andrew
Andrew, there is a printf function for awk: http://www.gnu.org/software/gawk/manual/gawk.html#Printf
Johannes Schaub - litb
printf "%d\n", sum should do it. (not %f i guess . dunno why i thought it's a floating point :p)
Johannes Schaub - litb
Also if you know it's always the last field but don't want to count fields (or if the number of fields varies) you can use print $NF.
Jouni K. Seppänen
that is disturbing! but very cool... I [almost] want to yank my proposal :D
warren
awk is teh saver
Johannes Schaub - litb
+1  A: 

Python Solution

#!/usr/bin/env python
text= file("the_file","r")
total= 0
for line in text:
    data = line.split()
    if data[0] in ('T', 'Shared', 'IPC'): continue
    print line
    segsize= int(data[6])
    total += segsize
print total

Most Linux distros have Python.

If you want to process stdin as part of a pipline, use

import sys
total = 0
for line in sys.stdin:
   ...etc...

If you want to assume that there's always 3 header lines:

import sys
total = 0
for line in sys.stdin.readlines()[3:]:
    total += int(line.split()[6])
print total

One-liner:

import sys; print sum( [int(line.split()[6]) for line in sys.stdin.splitlines()[3:]] )
S.Lott
+1  A: 

You could look it up in any online awk reference:

ipcs | awk '
BEGIN { sum = 0 }
/0x000000/ { sum = sum + $2 }
END {print sum}'
florin
+2  A: 

I have a utility script which simply adds up all columns. It's usually easy enough to grab the one you want from the one-line output. As a bonus, some SI-suffixes are recognized.

#!/usr/bin/awk -f
# Sum up numerical values by column (white-space separated)
#
# Usage:  $0 [file ...]
#
# stern, 1999-2005

{
    for(i = 1; i <= NF; ++i) {
        scale = 1
        if ($i ~ /[kK]$/) { scale = 1000 }
        if ($i ~ /[mM]$/) { scale = 1000*1000 }
        if ($i ~ /[gG]$/) { scale = 1000*1000*1000 }
        col[i] += scale * $i;
    }
    if (NF > maxnf) maxnf = NF;
}

END {
    for(i = 1; i <= maxnf; ++i) { printf " %.10g", col[i] }
    print "";
}

Example with custom field separator:

$ head /etc/passwd | addcol -F:
0 0 45 39 0 0 0
# Usage: $0 [file ...] <- There is no "-F"... Can you clarify the usage? What flags are supported?
Andrew
A: 

Thanks for the Python one-liner above!. It helped me to easy check the used space on my drive. Here is a mixed shell / Python one-liner, that do this - counts used space on the device /dev/sda in megabytes. It took me some time, before I found it out, so, maybe someone finds this useful too.

df -h -B 1M | grep dev/sda | tr -s ' '| cut -d' ' -f3 |python -c "import sys; print sum([int(num) for num in sys.stdin.readlines()])"

or more Python / less shell:

 df -h -B 1M | python -c "import sys; print sum([int(l.split()[2]) for l in sys.stdin.readlines() if '/dev/sda' in l])"

Thanks again!

Andrzej
+1  A: 

I would try to construct a calculation string and feed it to bc as follows:

  1. grep the lines that contain the numbers
  2. sed away all characters before (and after) the number on each line
  3. xargs the result (to get a string of numbers separated by blanks)
  4. tr anslate the blanks to '+' characters
  5. good appetite bc!

ipcs -mb | grep -w '^m ' | sed 's/^.*\s//' | xargs | tr ' ' + | bc

Looks like this is slightly longer than the awk solution, but for everyone who can't read (and understand) the odd awk code this may be easier to grasp... :-)

If bc is not installed you can use double parentheses in step 5 above to calculate the result:

  • echo $(( $(ipcs -mb | grep -w '^m ' | sed 's/^.*\s//' | xargs | tr ' ' +) )) or
  • SUM=$(( $(ipcs -mb | grep -w '^m ' | sed 's/^.*\s//' | xargs | tr ' ' +) )) or
  • (( SUM=$(ipcs -mb | grep -w '^m ' | sed 's/^.*\s//' | xargs | tr ' ' +) ))

The spacing after and before the double parentheses is optional.

Peterino