Operating System - Linux
1753444 Members
4752 Online
108794 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
Stuart Abramson
Trusted Contributor

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.?
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

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...
Steve Steel
Honored Contributor

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)
Rodney Hills
Honored Contributor

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...
Peter Nikitka
Honored Contributor
Solution

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"
James R. Ferguson
Acclaimed Contributor

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...
Sandman!
Honored Contributor

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!
Sandman!
Honored Contributor

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!