LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help regarding df command and spaces (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-regarding-df-command-and-spaces-891635/)

khandu 07-13-2011 11:34 PM

Need help regarding df command and spaces
 
Hey Guys..

Just to clarify.. I am running the command on a Mac .. but due to it being a generic unix command and a command line query.. I thought I can write on this forum..

I am running the command

Code:

df -h | grep '/dev/'
I get

Code:

/dev/disk0s2                            389Gi  62Gi  327Gi    16%    /
/dev/disk0s3                              76Gi  24Gi  52Gi    32%    /Volumes/Backup
/dev/disk3s2                            500Gi  47Gi  453Gi    10%    /Volumes/Misc

Note the huge space between the 1st and 2nd Column..

This is because currently I have some NAS drives mounted.. which are not showing due to grep..

when they are not mounted.. the output is fine.. with equal spaces between each column (like between col 2 and 3.. or 3 and 4)

I want to do a (dare I say) sed or awk or something to reduce the space between 1st and 2nd col. so that it has space like between col 3 and 4.. or 2 and 3..

This is because I am showing this output somewhere and because of the space its not showing up correctly..

Also I hope the command will still work when the NAS drives (afp) are not mounted.. basically consistency..

Hope i explained myself properly..

any help is great :D

EDIT: Damn.. after posting realized.. the spaces are not showing properly in the quote tag.. changed it to CODE tag

Tinkster 07-13-2011 11:46 PM

Something like this?
Code:

echo "/dev/disk3s2                            500Gi  47Gi  453Gi    10%    /Volumes/Misc" | sed -r 's/ {20}//g'
/dev/disk3s2        500Gi  47Gi  453Gi    10%    /Volumes/Misc


Beware that using this approach can result in funky results if the figures
for the sizes vary widely.


Cheers,
Tink

khandu 07-13-2011 11:58 PM

Quote:

Originally Posted by Tinkster (Post 4414460)
Something like this?
Code:

echo "/dev/disk3s2                            500Gi  47Gi  453Gi    10%    /Volumes/Misc" | sed -r 's/ {20}//g'
/dev/disk3s2        500Gi  47Gi  453Gi    10%    /Volumes/Misc


Beware that using this approach can result in funky results if the figures
for the sizes vary widely.


Cheers,
Tink

Not working.. -r is illegal operation.. without it also same result..

Well, another approach

How about we restrict the 1st column to say 12 characters?? can we do that.. so no matter whatevr is the name of 1st column... it will cut itself and present neatly.. I am not concerned with the full name to appear in first column...

Tinkster 07-14-2011 12:02 AM

If your sed is antique you can just use
Code:

s/                    //g
instead of
Code:

s/ {20}//g
"space"{20} is just cleaner and easier, I feel. ;}

grail 07-14-2011 12:10 AM

How about:
Code:

df -h | awk '$1=$1'
Looks a bit crowded, but you can play with it.

khandu 07-14-2011 12:15 AM

Quote:

Originally Posted by Tinkster (Post 4414468)
If your sed is antique you can just use
Code:

s/                    //g
instead of
Code:

s/ {20}//g
"space"{20} is just cleaner and easier, I feel. ;}

Hi, It kinda helps but not exactly.. I am looking at another approch now..

1) Either restrict the 1st column output to 12 char (don't think that might work)
OR
2) if I do

Code:

df -lh
I get

Code:

Filesystem    Size  Used  Avail Capacity  Mounted on
/dev/disk0s2  389Gi  62Gi  327Gi    16%    /
/dev/disk0s3  76Gi  24Gi  52Gi    32%    /Volumes/Backup

I want that display.. WITHOUT the 1st row.. i.e the

Code:

Filesystem    Size  Used  Avail Capacity  Mounted on
How do I modify the df -lh command to do so?

Diantre 07-14-2011 12:25 AM

Quote:

Originally Posted by khandu (Post 4414473)
I want that display.. WITHOUT the 1st row.. i.e the

Code:

Filesystem    Size  Used  Avail Capacity  Mounted on
How do I modify the df -lh command to do so?

How about this one:

Code:

$ df -lh | grep -v '^F.\+'

khandu 07-14-2011 12:32 AM

Thanks that worked.. just wondering if I mount external disks will df -lh show up..

anyways.. time will tell :D

Diantre 07-14-2011 12:55 AM

Quote:

Originally Posted by khandu (Post 4414484)
Thanks that worked.. just wondering if I mount external disks will df -lh show up..

It should work in most cases. That grep command prints every line that doesn't start with an uppercase F.

Tinkster 07-14-2011 01:23 AM

Try this one ;}

Reasonably flexible, tested on slackware64 13.1

Code:

script, save as whatever
NR>1{
  for(i=1;i<=NF;i++){
    l=length($i)
    if(l > a[i]){
      a[i]=l
    }
  }
 b[NR]=$0
}
END{
  for(i in b){
    split( b[i], c)
    printf "%-"a[1]+2"s ", c[1]
    for(j=2;j<length(a);j++){
      printf "%"a[j]+2"s ", c[j]
    }
    print a[length(a)]
  }
}

Code:

df -lh | awk -f whatever
/dev/sda8    176G    62G  115G  36% /home
tmpfs        952M      0  952M    0% /dev/shm
/dev/sda5      21G    17G  3.8G  82% /
/dev/sda7      11G  1.8G  8.3G  18% /var


For comparison:
df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda5              21G  17G  3.8G  82% /
/dev/sda7              11G  1.8G  8.3G  18% /var
/dev/sda8            176G  62G  115G  36% /home
tmpfs                952M    0  952M  0% /dev/shm


Cheers,
Tink

grail 07-14-2011 01:41 AM

Not sure what is on MAC, but if you have column:
Code:

df -h | awk 'NR > 1 && $1 = $1' | column -t

syg00 07-14-2011 02:51 AM

Mungs the alignment.
+1 for Tink

grail 07-14-2011 03:29 AM

Looked fine on my screen :)


All times are GMT -5. The time now is 10:37 PM.