1

I can't seem to find a solution for this and it's driving me crazy. I know I can use awk to print a column(s). I'm having trouble printing a specific column though because of the way my file system is arranged.

This is what I have when I run df:

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_root-root
                      36623968   4484592  30278976  13% /
tmpfs                   961312         0    961312   0% /dev/shm
/dev/sda1               516040    102896    386932  22% /boot

If I awk for the 1K-blocks column it returns values from other columns because of the length of the filesystem location. Basically this happens:

1K-blocks

4484596
961312
516040

How can I list the right column info?

2
  • 7
    Try using df -P. See also GNU stat. May 17, 2013 at 19:46
  • 2
    @StephaneChazelas: that's an answer ;)
    – tink
    May 17, 2013 at 21:02

4 Answers 4

6

If you're piping df into awk, pipe df -P instead. It's designed to be easily parsable, and in particular doesn't break lines. Remember to skip the header line (NR >= 2).

If you need to parse some existing output with weird line breaks, you can tell a continuation line because it starts with a space.

awk '
  NR==1 {next}
  /^ / {print $1}
  !/^ / {print $2}
'

Beware however that there are unix variants (OSX, at least — and I wouldn't be surprised if it was the only one where this happens in practice) where the filesystem designation can contain spaces, which makes any column-based parsing problematic (unfortunately, df -P doesn't use tabs).

0

I don't have an unusually long FS name but you can try this:

df |grep "%" |grep -v Use |awk {'print $(NF-4)'}

This is assuming you don't need the title line with the 1K-Blocks.

0
df -Pk|tail -n +2|column -t | awk '{print $4}'
  • df -Pk: will list the file system.
  • tail -n +2: will remove header.
  • column -t: used to format the output.
  • awk '{print $4}': will print the contents of fourth column.
2
  • Please don't answer with one-liner commands. Even if they work, OP will have to figure out what is this, why does it work, etc. Please add an explanation to your answer.
    – psimon
    Jul 7, 2014 at 7:22
  • df -Pk : will list the file system....tail -n +2 : will remove header.....column -t: used to format the ouput.....awk '{print $4}' :will print the contents of fourth column...................
    – Sorav
    Jul 7, 2014 at 7:42
0

Using GNU df, you can use the -P flag and pipe it into awk as suggested in other answers. An example to make it clear and simple: for the case where you want the number of available kilobytes (df -k), the following works fine:

df -kP | awk 'NR>1 {print $4}'

This tells awk to print the 4th column for all rows after the first (i.e. not the header row).

Anyway, the main reason for adding this answer is the observation that on Solaris (checked on Solaris 9 & 10), this flag isn't actually necessary despite similar linebreaks in the output formatting; the following works on in this case:

df -k | awk 'NR>1 {print $4}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .