Reputation: 1015
I am using Runtime.getRuntime().exec("df") to get the space details for my partitions.
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sdb12 41022792 4219168 34713128 11% /
udev 8103980 4 8103976 1% /dev
tmpfs 3245332 924 3244408 1% /run
none 5120 0 5120 0% /run/lock
none 8113328 160 8113168 1% /run/shm
/dev/sdb2 262144 28584 233560 11% /boot/efi
How can I use regex or UNIX commands to get as the output just the 2nd,3rd and 4th columns - changing the headers as follows - Available Memory, Used Memory and Total memory exactly in this order.
So the desired output is :
Available Used Total
34713128 4219168 41022792
8103976 4 8103980
3244408 924 3245332
...
Thank you!
Upvotes: 1
Views: 255
Reputation: 46375
The following rather long string does what you ask:
df | awk 'BEGIN {print " Available Used Total";} {if (NR>1) printf("%12d%12d%12d\n", $4, $3, $2);}'
Explanation:
| awk take the output of the df command as input to awk
BEGIN do this first: print headers. Note the use of spaces to align things
if (NR > 1) skip "record 1" = do not do anything with the headers
printf() do formatted printing
%12d print integer in fixed width of 12 characters (maintains alignment
Result on my machine:
Available Used Total
71644456 551306776 623463232
0 372 372
0 0 0
0 0 0
407510704 377391216 784901920
618400760 549288520 1167689280
Not terribly useful without the name of the disks, but this is what you were asking...
Upvotes: 2
Reputation: 46432
For me this works
df | perl -pe \
's!^(?:\S+\s+){2}(\S+)\s+(\S+).*!"$1\t$2\t" . ($1 eq "Used" ? "Total" : $1 + $2)!e'
It's not nice: It finds the columns as non-blanks separated by blanks, which is a bit fragile. It computes the last column as the sum or "Total" based on the column name. Rather fragile, I'd say.
Upvotes: 0