1752577 Members
4227 Online
108788 Solutions
New Discussion юеВ

awk parsing

 
SOLVED
Go to solution
andi_1
Frequent Advisor

awk parsing

Hi guys,

I posted a question how to calculate sum of all columns. It really helped me. Thank you.
I have a little different problem though: here an possible file I could have:

/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
/dev/vg01a/lvol3 155648 45189 103603 2402
/dev/vg01a/lvol1 99669 36453 53249 16081
/dev/vg01a/lvol8 819200 710685 10199 19967
/dev/vg01a/lvol7 819200 496611 30248 18973

and I need the following output:
/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(vg00): xxxxx xxxxx xxxx xxxx

/dev/vg01a/lvol3 155648 45189 103603 2402
/dev/vg01a/lvol1 99669 36453 53249 16081
/dev/vg01a/lvol8 819200 710685 10199 19967
/dev/vg01a/lvol7 819200 496611 30248 18973

Total(vg01a): xxxxx xxxxx xxxx xxxx

Any ideas how to use awk in this case?

Thanks a lot!
7 REPLIES 7
harry d brown jr
Honored Contributor

Re: awk parsing

To display individual LV's, just add a print to the area that doesn't have a print (wow - I scare myself).

Also change tle bdf to cat your file.


bdf|grep -v Filesystem|tr -s " " " "|cut -d" " -f 1-4|sort|
awk 'BEGIN {
prev="";
totals[0]=0;
totals[1]=0;
totals[2]=0;
gtotals[0]=0;
gtotals[1]=0;
gtotals[2]=0;
printf("%4s %14s %14s %14s\n","VG","Size","In use","Available");
}
{split($1,curr,"/");
if (prev!="" && prev!=curr[3])
{printf("%4s %14d %14d %14d\n",prev,totals[0],totals[1],totals[2]
);
gtotals[0]+=totals[0];
gtotals[1]+=totals[1];
gtotals[2]+=totals[2];
totals[0]=0;
totals[1]=0;
totals[2]=0;
}
prev=curr[3];
totals[0]+=$2;
totals[1]+=$3;
totals[2]+=$4;
}
END {
gtotals[0]+=totals[0];
gtotals[1]+=totals[1];
gtotals[2]+=totals[2];
printf("%4s %14d %14d %14d\n",prev,totals[0],totals[1],totals[2]);
printf("%4s %14s %14s %14s\n","----","--------------","-------------
-","--------------");
printf("%4s %14d %14d %14d\n","Totl",gtotals[0],gtotals[1],gtotals[2
]);
}'


live free or die
harry
Live Free or Die
andi_1
Frequent Advisor

Re: awk parsing

Hi Harry,

Wow, thanks a lot a such big script!
Unfortunately, I cannot use bdf, since the file I get is from another system.

So, I just have file.txt....

I assume your script should from simple file as well?
James R. Ferguson
Acclaimed Contributor

Re: awk parsing

Hi:

Along our earlier lines, for example:

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

Regards!

...JRF...
harry d brown jr
Honored Contributor
Solution

Re: awk parsing

Slight more changes than I thought, this should pretty much do, except maybe for formatting.

tr -s " " " "|cut -d" " -f 1-4|sort|
awk 'BEGIN {
prev="";
totals[0]=0;
totals[1]=0;
totals[2]=0;
gtotals[0]=0;
gtotals[1]=0;
gtotals[2]=0;
printf("%24s %14s %14s %14s\n","VG","Size","In use","Available");
}
{
split($1,curr,"/");
if (prev!="" && prev!=curr[3])
{printf("%24s %14d %14d %14d\n",prev,totals[0],totals[1],totals[2
]);
printf "\n";
gtotals[0]+=totals[0];
gtotals[1]+=totals[1];
gtotals[2]+=totals[2];
totals[0]=0;
totals[1]=0;
totals[2]=0;
}
printf("%24s %14d %14d %14d\n",$1,$2,$3,$4);
prev=curr[3];
totals[0]+=$2;
totals[1]+=$3;
totals[2]+=$4;
}
END {
gtotals[0]+=totals[0];
gtotals[1]+=totals[1];
gtotals[2]+=totals[2];
printf("%24s %14d %14d %14d\n",prev,totals[0],totals[1],totals[2]);
printf("%24s %14s %14s %14s\n","----","--------------","------------
--","--------------");
printf("%24s %14d %14d %14d\n","Totl",gtotals[0],gtotals[1],gtotals[
2]);
}'


Live Free or Die
SHABU KHAN
Trusted Contributor

Re: awk parsing

Lz,

Same like before ...

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

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

Thanks,
Shabu
H.Merijn Brand (procura
Honored Contributor

Re: awk parsing

# perl -nae 'm:^/dev/([^/]+):;for(1..4){$d{$1}[0][$_]+=$F[$_]};push@{$d{$1}},$_;END{for(keys%d){@d=@{$d{$_}};$d=shift@d;print@d,"Total($_):@{$d}\n"}}' logfile
Enjoy, Have FUN! H.Merijn
SHABU KHAN
Trusted Contributor

Re: awk parsing

Lz,

If you want this in the format that you specified ... execute this script:

prompt>cat myscript

-------------

#!/bin/ksh

rm -f /tmp/testfile.out

grep "vg00" testfile >> /tmp/testfile.out
echo " " >> /tmp/testfile.out
awk '/vg00/{t1=t1+$2;t2=t2+$3;t3=t3+$3;t4=t4+$4;} END {print "Total(vg00): ", t1,t2,t3,t4}' testfile >> /tmp/testfile.out
echo " " >> /tmp/testfile.out
grep "vg01a" testfile >> /tmp/testfile.out
echo " " >> /tmp/testfile.out
awk '/vg01a/{t1=t1+$2;t2=t2+$3;t3=t3+$3;t4=t4+$4;} END {print "Total(vg01a): ", t1,t2,t3,t4}' testfile >> /tmp/testfile.out

-------------
prompt>chmod 755 myscript
prompt>./myscript
prompt>cat /tmp/testfile.out

Thanks,
Shabu