hello,
my objective is to calculate the swap size
-bash-3.00# swap -l
swapfile dev swaplo blocks free
/dev/md/dsk/d1 85,1 8 33559776 16966160
so size=blocks*512/(1024*1024*1024)
since blocks are 512-blocks
any idea how to get it?
so far I was able to get the 4th value:
swap -l |grep -v blocks| awk '{print $4}'
33559776
how to get the size in GB?
thanks
actually I want also to get the 4th column of the 2nd line in the file:
more /opt/SUNWexplo/output/explorer.zeus/disks/swap-l.out
swapfile dev swaplo blocks free
/dev/md/dsk/d1 85,1 8 33559776 16966152
I want 33559776
how to get it?
how to convert it to GB? (*512/(1024)^3
thanks
Let awk do the job for you:
awk 'NR==2{print $4*512/(1024*1024*1024)}' /opt/SUNWexplo/output/explorer.zeus/disks/swap-l.out
Regards
thanks indeed
1] how to concact GB to the result?
2] how to have an output such as:
Swap Space: xxxxxx GB
3] Any way to round this number? 16.0025 to 16?
thanks
hello again
let me start with this:
echo "Swap Space in GB:"
awk 'NR==2{print $4*512/(1024*1024*1024)}' /opt/SUNWexplo/output/explorer.zeus/disks/swap-l.out
gives:
Swap Space in GB:
16.0025
I want to get
Swap Space in GB: 16.0025
i.e concatenate the 2 outputs in one line.
how to do that?
many thanks
It is ok:
v_swap1="Swap Space in GB:"
v_swap2=`awk 'NR==2{print $4*512/(1024*1024*1024)}' /opt/SUNWexplo/output/explorer.zeus/disks/swap-l.out`
echo $v_swap1 $v_swap2
any idea how to round 16.0025 to 16??
thanks
If the printf statement rounding is "unbiased" (system dependent) which means it doesn't round a trailing 0.5 up you can just add 0.5 to the result:
awk '{printf("Swap Space: %d\n", $4*512/(1024*1024*1024)+0.5)}' file
To verify weather printf is rounding biased or not you can try this:
awk 'BEGIN{printf("%d\n", 15.9)}'
Regards