LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-14-2011, 12:34 AM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 93

Rep: Reputation: 0
Question 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

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

Last edited by khandu; 07-14-2011 at 12:35 AM.
 
Old 07-14-2011, 12:46 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 07-14-2011, 12:58 AM   #3
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
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...
 
Old 07-14-2011, 01:02 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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. ;}
 
Old 07-14-2011, 01:10 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
How about:
Code:
df -h | awk '$1=$1'
Looks a bit crowded, but you can play with it.
 
Old 07-14-2011, 01:15 AM   #6
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
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?
 
Old 07-14-2011, 01:25 AM   #7
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 224Reputation: 224Reputation: 224
Quote:
Originally Posted by khandu View Post
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.\+'

Last edited by Diantre; 07-14-2011 at 03:32 AM. Reason: A correction in the regex
 
1 members found this post helpful.
Old 07-14-2011, 01:32 AM   #8
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Thanks that worked.. just wondering if I mount external disks will df -lh show up..

anyways.. time will tell
 
Old 07-14-2011, 01:55 AM   #9
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 224Reputation: 224Reputation: 224
Quote:
Originally Posted by khandu View Post
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.
 
Old 07-14-2011, 02:23 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 07-14-2011, 02:41 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Not sure what is on MAC, but if you have column:
Code:
df -h | awk 'NR > 1 && $1 = $1' | column -t
 
Old 07-14-2011, 03:51 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,988

Rep: Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217
Mungs the alignment.
+1 for Tink
 
Old 07-14-2011, 04:29 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Looked fine on my screen
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Spaces in command line switches ProgrammerTim Linux - Newbie 2 10-06-2010 05:54 PM
Bash Command to Remove Spaces CincinnatiKid Linux - General 16 09-18-2010 10:46 AM
Need Command to see free spaces shipon_97 Linux - Newbie 4 11-24-2006 09:50 AM
Use spaces in command line? Geminias Linux - Newbie 2 01-03-2006 08:37 PM
Spaces on command line odd Linux - Software 2 05-22-2004 09:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration