handling arrays in "awk" (311 Views)
Reply
Highlighted
Trusted Contributor
Posts: 278
Registered: ‎07-15-1999
Message 1 of 6 (311 Views)
Accepted Solution

handling arrays in "awk"

I wrote this little script to "flatten out" an lvdisplay of all of the LVs on my system.

I am collecting all of the PVs in an LV in an array PV[], but I don't know how to print them all out on one line.

Can one of you "awk wizards" help me, please?

for VGROUP in $VOLGRPS
do
LVOLS=$( vgdisplay -v $VGROUP | grep "LV Name" | sort | awk '{print $3}' )
set $LVOLS
NLVS=$#
print $VGROUP $NLVS
for LV in $LVOLS
do
LVNAME=$( basename $LV )
lvdisplay -v $LV | awk -v LVName=$LVNAME '
/Mirror/ {MIRS = $3}
/LV Size/ {SIZE = $4}
/Current LE/ {LES = $3}
/Allocated PE/ {PES = $3}
/Stripes/ {STRIPES = $2}
/ \/dev\/dsk/ {PV[CNT] = substr($1,10);
CNT = CNT + 1}
END {print LVName, MIRS, SIZE, LES, \
PES, STRIPES, CNT, PV[$@]} ........<===
'
done
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 6 (311 Views)

Re: handling arrays in "awk"

Hi Stuart:

You need to walk through each array element:

...
for (i=1;i<=CNT;i++) {printf "%s ",PV[i]};print "\n"}
...

Use the 'printf' command in lieu of 'print' to print without injecting a newline.

Regards!

...JRF...
Honored Contributor
Posts: 764
Registered: ‎08-23-2002
Message 3 of 6 (311 Views)

Re: handling arrays in "awk"

something like this should do:

END {
printf("%s %s %s %s %s %s %s", LVName, MIRS, SIZE, LES, PES, STRIPES, CNT);
#use printf so there is no newline
for ( i in PV ) printf(" %s", PV[i]);
#although this isn't garanteed to be in order
# to be sure they are in order:
#for (i=0;i++;i#and finally a new line
printf("\n");
}
Honored Contributor
Posts: 2,220
Registered: ‎01-13-2005
Message 4 of 6 (311 Views)

Re: handling arrays in "awk"

Replace the instance of PV[$@] with the following:

END {print ; for (s in PV) printf("%s ", PV[s]); print "\n"}

regards!
Honored Contributor
Posts: 4,030
Registered: ‎06-09-2004
Message 5 of 6 (311 Views)

Re: handling arrays in "awk"

Use this script as,

for VGROUP in $VOLGRPS
do
LVOLS=$( vgdisplay -v $VGROUP | grep "LV Name" | sort | awk '{print $3}' )
set $LVOLS
NLVS=$#
print "VGROUP\t No. LVs"
print "$VGROUP $NLVS\n"
printf LVName" "MIRS" "SIZE" "LES" "PES" "STRIPES" "PVNAME"\n"

for LV in $LVOLS
do
LVNAME=$(basename $LV )
lvdisplay -v $LV | awk -v LVName=$LVNAME '
/Mirror/ {MIRS = $3}
/LV Size/ {SIZE = $4}
/Current LE/ {LES = $3}
/Allocated PE/ {PES = $3}
/Stripes/ {STRIPES = $2}
/^ +\/dev\/dsk/ {PV[CNT] = substr($1,10);CNT = CNT + 1}
END {printf ("%-6s %-4d %-4d %-3d %-3d %-8s",LVName,MIRS,SIZE,LES,PES,STRIPES); for (s in PV) { printf PV[s]; }printf "\n";}'

done
done

hth.
Easy to suggest when don't know about the problem!
Trusted Contributor
Posts: 278
Registered: ‎07-15-1999
Message 6 of 6 (311 Views)

Re: handling arrays in "awk"

These all look good. I'll try them today. thanks.
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.