Re: awk help: summarize list when column X changes value (685 Views)
Reply
Trusted Contributor
Stuart Abramson
Posts: 278
Registered: ‎07-15-1999
Message 1 of 8 (685 Views)
Accepted Solution

awk help: summarize list when column X changes value

This is the output from the EMC symdisk command:

DA-7B 07B D 1 SEAGATE CUDA180 24 173017 450 173145
DA-7B 07B D 2 SEAGATE CUDA180 20 173017 386 173145
DA-7B 07B D 3 SEAGATE CUDA180 20 173017 386 173145
DA-8B 08B C 5 SEAGATE CUDA180 20 173017 386 173145
DA-8B 08B C 8 SEAGATE CUDA180 20 173017 386 173145
DA-8B 08B C 9 SEAGATE CUDA180 20 173017 386 173145
DA-8B 08B D 0 SEAGATE CUDA180 21 173017 383 173145
DA-8B 08B D 1 SEAGATE CUDA180 25 173017 5946 173145
DA-8B 08B D 2 SEAGATE CUDA180 21 173017 383 173145
DA-8B 08B D 3 SEAGATE CUDA180 20 173017 386 173145
DA-9B 09B C 1 SEAGATE CUDA180 21 173017 383 173145
DA-9B 09B C 2 SEAGATE CUDA180 20 173017 386 173145
DA-9B 09B C 3 SEAGATE CUDA180 20 173017 386 173145
DA-9B 09B C 4 SEAGATE CUDA180 20 173017 386 173145
DA-9B 09B C 5 SEAGATE CUDA180 20 173017 386 173145
DA-9B 09B C 8 SEAGATE CUDA180 20 173017 386 173145
DA-9B 09B C 9 SEAGATE CUDA180 20 173017 386 173145
DA-10B 10B C 4 SEAGATE CUDA180 20 173017 386 173145
DA-10B 10B C 5 SEAGATE CUDA180 20 173017 386 173145
DA-10B 10B C 8 SEAGATE CUDA180 20 173017 386 173145
DA-10B 10B C 9 SEAGATE CUDA180 20 173017 386 173145
DA-10B 10B D 0 SEAGATE CUDA180 21 173017 383 173145
DA-10B 10B D 1 SEAGATE CUDA180 21 173017 383 173145
DA-10B 10B D 2 SEAGATE CUDA180 20 173017 386 173145
DA-10B 10B D 3 SEAGATE CUDA180 20 173017 386 173145
DA-10B 10B D 4 SEAGATE CUDA180 20 173017 386 173145

I want to summarize when the "DA" changes.

I want to say:
DA-07B has 4 x SEAGATE CUDA180 Disks.
DA-08b has 4 x .. etc.

How do i do that in "awk"? Is there some way to say "when $1 changes, count up.."? Or do I have to save the value and check every time, etc.?
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 8 (685 Views)

Re: awk help: summarize list when column X changes value

Hi Stuart:

Try this:

#!/usr/bin/awk -f
{if (FIRST!=1) {FIRST=1;LAST=$1}}
{if ($1!=LAST) {print LAST," count = ",CNT;CNT=0;LAST=$1}}
{print;CNT++}

Regards!

...JRF...
Honored Contributor
Steve Steel
Posts: 2,909
Registered: ‎02-13-2000
Message 3 of 8 (685 Views)

Re: awk help: summarize list when column X changes value

Hi

Keep it simple

cat tmp/in|cut -f1 -d " "|sort -u|
while read line
do
let xx=$(grep $line tmp/in|wc -l)
echo $line has $xx $(grep $line tmp/in|
head -n 1|cut -f5-6 -d" ") disks
done


tmp/in is data file containing list shown


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 4 of 8 (685 Views)

Re: awk help: summarize list when column X changes value

For a simple count where the disk type doesn't matter-
cut -c1 yourfile | sort | uniq -c

Perl can provide a more complete routine-
perl -an -e '$tot{$F[0]}{"$F[4] $F[5]"}++;END {for $DA (sort keys %tot) { for $dsk (sort keys %{$tot{$DA}}) { print "$DA has $tot{$DA}{$dsk} $dsk Disks.\n";}}}' yourfile

HTH

-- Rod Hills
There be dragons...
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 5 of 8 (685 Views)

Re: awk help: summarize list when column X changes value

Hi,

just shorter and without awk:
cut -f1 -d " " /tmp/x | sort | uniq -c

mfG nik
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 8 (685 Views)

Re: awk help: summarize list when column X changes value

Hi Stuart:

BTW, you never evaluated the responses in your thread below and I would like to know if you found them helpful. Thanks.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=976274

Regards!

...JRF...
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 7 of 8 (685 Views)

Re: awk help: summarize list when column X changes value

Stuart,

How about this awk construct:

awk '/^DA-[0-9]*B/{line=$5" "$6;if(NF==1){curr=$1;count[curr]++} else{if(curr==$1)count[curr]++;else{curr=$1;count[curr]++}}} END{for(i in count) print i " has", count[i] " x " line " Disks"}'

cheers!
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 8 of 8 (685 Views)

Re: awk help: summarize list when column X changes value

Matter of fact the code is easier to understand if put into its own script:

=====================myawkscript=======================
/^DA-[0-9]*B/ {
line=$5" "$6
if ((NF==1)||(curr==$1)) {
curr=$1
count[curr]++
}
else {
curr=$1
count[curr]++
}
}
END{for(i in count) printf("%s has %d x %s Disks\n", i, count[i], line)}
=======================================================

# symdisk | awk -f myawkscript

cheers!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.