BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

handling arrays in "awk"

SOLVED
Go to Solution
Highlighted
Trusted Contributor

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
5 REPLIES
Acclaimed Contributor

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

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

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

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

Re: handling arrays in "awk"

These all look good. I'll try them today. thanks.