Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
7  
Try using df -P. See also GNU stat. –  Stephane Chazelas May 17 '13 at 19:46
2  
@StephaneChazelas: that's an answer ;) –  tink May 17 '13 at 21:02
    
Worked. Thanks. –  mister mister May 17 '13 at 21:37
add comment

2 Answers

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.