1

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!

3
  • This is most definitely a job for awk, not regex. Would that be OK? Commented Nov 3, 2013 at 14:51
  • 1
    as long as I can execute it all in getRuntime().exec(...), Yes! Commented Nov 3, 2013 at 14:53
  • Do you want the text centered like that for the middle column, or is right-aligned preferred (as in the original command) Commented Nov 3, 2013 at 15:04

3 Answers 3

2

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...

Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help - clearly the above can be modified in many ways and I hope you will… awk is really the perfect tool for this kind of thing, well worth becoming familiar with it. It can be obscure, but is surprisingly powerful. Best reference I found is the grymoire (unusual word meaning "book of spells") - written by Bruce Barnett. Google grymoire awk.
0

You can use awk to do this:

df | awk '{print ""$4" "$3" "$2""}'

1 Comment

I modified the order. I'd not try to do a modification of the headers in a Unix command; just do the substitution against the resulting Java string.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.