-1

I am trying to grab the 3rd to the last column in the "df -m" command but it does not seem to work. Well its works on one and does not work on other. Part of the reason is for one mount point we are using LVM and other we are using standard linux mount...

But i need to always access the 3rd to the last column(Available space) so i can make sure my script works...how can this be achieved or what am i doing wrong ?

hostname:oraSID 292> df -m /oracle/SID/logs/
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/xvdf           5040  1139      3646  24% /oracle/SID/logs
hostname:oraSID 293>
hostname:oraSID 293> df -m /oracle/SID/sapdata6/
Filesystem           1M-blocks   Used Available Use% Mounted on
/dev/mapper/grp1-log_vsap1
                        856743 800202     13022  99% /oracle/SID/sapdata6
hostname:oraSID 294>
hostname:oraSID 294>
hostname:oraSID 295> df -m /oracle/SID/logs/ | awk '{ field = $(NF-2) }; END{ print field }'
3646
hostname:oraSID 296>
hostname:oraSID 296> df -m /oracle/SID/sapdata6/ | awk '{ field = $(NF-2) }; END{ print field }'
awk: (FILENAME=- FNR=2) fatal: attempt to access field -1
hostname:oraSID 297>
hostname:oraSID 297>
max scalf
  • 329
  • 1
  • 8
  • 19

3 Answers3

0

Using the --output option might be easier :

df -m --output=avail
Aaron
  • 24,009
  • 2
  • 33
  • 57
  • i dont think that is a valid cmd, at least not on Redhat 6...i get error "df: unrecognized option '--output=used'" – max scalf Oct 22 '15 at 23:09
  • @maxscalf Could you still check df --help or man df for a similar option? – Aaron Oct 22 '15 at 23:14
  • @maxscalf Yeah it seems specific to my "distro" (I'm using cygwin). I'm working on a sed regex. Btw I think your solution doesn't work because of the possible line feed. – Aaron Oct 22 '15 at 23:17
0

This works on my Ubuntu system:

df -m | awk -F "\t" 'NR == 1 {print "Available"} NR > 1 {print $(NF-2)}'

The space in "Mounted on" throws it off, so I print that explicitly.

Andrew
  • 475
  • 4
  • 15
-1

I found answer, to keep my formatting that same i am using the below option..so seems like df -P keep the format the same...

-P, --portability use the POSIX output format

max scalf
  • 329
  • 1
  • 8
  • 19