0

Need a help in printing the Available memory in bytes for logical drive below.

When I try printing column Available for /dev/mapper/VolGroup00-LogVol00, It is displaying Available 26%, where I need Available 28012413kB

Filesystem          1kB-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                     39426261kB 9378776kB 28012413kB  26% /
/dev/sda1             103513kB   20322kB   77847kB  21% /boot
tmpfs                 525345kB       0kB  525345kB   0% /dev/shm


# df -B KB | awk '{print $4}'
Available

26%
77847kB
525345kB
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Pari
  • 21
  • 1
  • 4
  • It seems that the problem you have is with the option given to the `df` command. – nsm Nov 12 '15 at 00:50
  • Better specify your OS because `df` is to some extent platform-dependent. Also, it's weird that your `df` chops up a line when a fs path is long. At least that doesn't happen with either BSD or coreutils `df`. – 4ae1e1 Nov 12 '15 at 01:39
  • Also, there's no guarantee that the filesystem field cannot have spaces in it (for instance, my BSD `df` would have things like `map -hosts`), so assuming the fourth field is "available" is unreliable. – 4ae1e1 Nov 12 '15 at 01:40
  • @4ae1e1 - the `/dev/mapper/VolGroup` is an indicator of LVM. Line splitting like this happens for me in Ubuntu 10.04 (the only Linux I can test with at the moment). – ghoti Nov 12 '15 at 01:43
  • @ghoti Hmm, weird, can't reproduce on vivid with `df` from coreutils 8.23. Does the line splitting happen whenever the field is long, or does this only happen with certain filesystems? Maybe it's a defect in an older `df`? – 4ae1e1 Nov 12 '15 at 01:51
  • Looks like that behaviour changed in version 8.10. [Check out the comments in the source.](http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/df.c#n302) It appears that this was POSIX-compliant behaviour that GNU dropped because it was inconvenient. – ghoti Nov 12 '15 at 02:04
  • 1
    Use `-P` in `df` for POSIX compliant output - `df -P -B KB | awk '{print $4}'`. – alvits Nov 12 '15 at 03:04
  • @ghoti Minor nitpick, actually the comment says changed in `v8.10-40-g99679ff` so the change must have been released in 8.11... – 4ae1e1 Nov 12 '15 at 04:26
  • @4ae1e1, I'm a FreeBSD developer, I don't know how you Linux people handle your tags. :) – ghoti Nov 12 '15 at 04:45
  • @ghoti Well, I'm not a Linux guy either... But isn't that `git describe --long`? – 4ae1e1 Nov 12 '15 at 04:51
  • Ah, indeed it is. :) – ghoti Nov 12 '15 at 07:06

2 Answers2

0

Yes, well, your issue is obviously that you've got a split line.

You can use awk to rejoin lines based on whatever criteria you deem appropriate. For example, here's something that joins lines in the event that awk comes across a line with only one "word" on it.

# df -B KB | awk 'NF==1{old=$0;getline;$0=old FS $0;} {print $4}'
ghoti
  • 45,319
  • 8
  • 65
  • 104
0

You can access the fields from the end as well

... | awk 'NF>3{print $(NF-2)}' 

should work. I haven't tested though.

karakfa
  • 66,216
  • 7
  • 41
  • 56