1822158 Members
3494 Online
109640 Solutions
New Discussion юеВ

parsing output with AWK

 
SOLVED
Go to solution
andi_1
Frequent Advisor

parsing output with AWK

Hi,

Suppose, I have the following information:
total used free avail
/dev/vg00/lvol3 155648 45189 103603 2402
/dev/vg00/lvol1 99669 36453 53249 16081
/dev/vg00/lvol8 819200 710685 10199 19967
/dev/vg00/lvol7 819200 496611 30248 18973

I would like to summarize total size of all the above columns:
e.g.
total used free avail
/dev/vg00/lvol3 155648 45189 103603 2402
/dev/vg00/lvol1 99669 36453 53249 16081
/dev/vg00/lvol8 819200 710685 10199 19967
/dev/vg00/lvol7 819200 496611 30248 18973

Total: 199999 88392 82883 2811

Does anyone know the easiest to do it in awk?

Thank you!
7 REPLIES 7
Sachin Patel
Honored Contributor

Re: parsing output with AWK

Hi Harry
This user has
assign point to 53 out of 123 answer.

LZ i guss you want to total of all used available and used right?

Run following command in one line.
#dbf -l | sort | awk '{t+=$2} {u+=$3} {a+=$4} END {print "\n total = "t " used = u "available = " a}'

Sachin
Is photography a hobby or another way to spend $
James R. Ferguson
Acclaimed Contributor
Solution

Re: parsing output with AWK

Hi:

Crudely and simply:

# awk '{F2=F2+$2;F3=F3+$3;F4=F4+$4;F5=F5+$5};END {print F2,F3,F4,F5}' /tmp/input

Regards!

...JRF...
SHABU KHAN
Trusted Contributor

Re: parsing output with AWK

lz,

Try this:

awk '{t1+=$2} END {print "Total: ", t1}' testfile

Will give you the total for the 2nd Column ...

Do the same thing for the other columns ...

Thanks,
Shabu
Sachin Patel
Honored Contributor

Re: parsing output with AWK

Here is what I do in script to get free space on ts* disks.

#!/bin/sh
bdf -l > file1
SPACE=`cat file1 | grep -v used | grep ts | sort | awk -v s=0 '{s+=$4} {v=s/1000000} END {print v}'`
echo "Free space = ${SPACE}"


Sachin
Is photography a hobby or another way to spend $
SHABU KHAN
Trusted Contributor

Re: parsing output with AWK

Hi lz,

Here is a combination of mine and JRF's ... easy and efficient ...

awk '{t1=t1+$2;t2=t2+$3;t3=t3+$3;t4=t4+$4;} END {print "Total: ", t1,t2,t3,t4}' testfile


Thanks,
Shabu
James R. Ferguson
Acclaimed Contributor

Re: parsing output with AWK

Hi:

For grins-and-giggles, we could also tally the fields into an array (which might be more suitable for situations involving larger numbers of fields):

# awk 'BEGIN{min=2;max=5};{for (i=min;i<=max;i++) a[i]+=$i};END {for (i=min;i<=max;i++) print a[i]}' /tmp/data

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: parsing output with AWK

install di from http://www.gentoo.com/di/ and use the -t option

Filesystem Mount Megs Used Avail %used fs Type
/dev/vg00/lvol3 / 200.0 29.2 160.2 20% vxfs
/dev/vg00/data /data 2000.0 613.4 1300.4 35% vxfs
/dev/vg00/home /home 16.0 1.1 14.0 13% vxfs
/dev/vg00/opt /opt 768.0 549.4 204.9 73% vxfs
/dev/vg00/pro /pro 12000.0 11287.1 690.7 94% vxfs
/dev/vg00/lvol1 /stand 292.1 31.3 231.6 21% hfs
/dev/vg00/tmp /tmp 400.0 233.5 156.7 61% vxfs
/dev/vg00/u /u 1000.0 373.1 587.7 41% vxfs
/dev/vg00/lvol7 /usr 1304.0 616.2 644.9 51% vxfs
/dev/vg00/var /var 504.0 137.5 345.4 31% vxfs
/dev/vg00/wrk /wrk 1000.0 59.8 881.7 12% vxfs
Total 19484.1 13931.6 5218.3 73%
Enjoy, Have FUN! H.Merijn