I'm looking for a way in Korn shell to zero fill (or space fill) the output from df so that it will sort properly.
"Raw" output from df -k:
df -k
Filesystem kbytes used avail capacity Mounted on
/dev/vx/dsk/rootvol 4131866 3593302 497246 88% /
/proc 0 0 0 0% /proc
fd 0 0 0 0% /dev/fd
mnttab 0 0 0 0% /etc/mnttab
swap 19891520 144 19891376 1% /var/run
swap 19913552 22176 19891376 1% /tmp
/dev/vx/dsk/u01 112302757 30833295 80346435 28% /u01
/dev/vx/dsk/node@1 96975 4977 82301 6% /global/.devices/node@1
/dev/vx/dsk/node@2 96975 4988 82290 6% /global/.devices/node@2
/dev/vx/dsk/stkdg/s03
1047527424 60483821 925353439 7% /s03
/dev/vx/dsk/stkdg/s02
1048576000 266754165 732957997 27% /s02
/dev/vx/dsk/sandg/s01
355923968 322572197 31422792 92% /global/s01
Sorted df output (which incorrectly sorts 28% and 27% after 7%):
df -k | grep / | awk '{ print $5}' | sort -r
92%
88%
7%
6%
6%
28%
27%
1%
1%
0%
0%
0%
I would like the output to display in the following format:
92%
88%
28%
27%
7%
6%
6%
1%
1%
0%
0%
0%
Any ideas?
The output you are getting is not "incorrect". It is actually correct with the flags you are giving sort.
You have to tell sort that you are sorting numbers and not text.
:> df -k | grep / | awk '{print $5}' | sort -gr
100%
7%
2%
0%
0%
A quick look at the man pages would've given you the answer.
df -k | awk '/[/]/ { print $5}' | sort -rn
Or:
if perl is acceptable:
df -k |
perl -ane'
/%/ and push @x,$F[4];
print join "\n" , reverse sort { $a <=> $b } @x
if eof'
Otherwise:
df -k |awk '1<NR&&NF>1{print $5}'|sort -nr
System Shock,
>>A quick look at the man pages would've given you the answer.
I don't think so:
df -k | grep / | awk '{print $5}' | sort -gr
sort: illegal option -- g
usage: sort [-cmu] [-o output] [-T directory] [-S mem] [-z recsz]
[-dfiMnr] [-b] [-t char] [-k keydef] [+pos1 [-pos2]] files...
shew01
June 4, 2008, 10:01am
6
vgersh99,
Cool! This seems to work. Thanks for the help.
And another one:
df -k |
while read fs kb us av ca mo; do
case $ca in
*[0-9]% ) printf "%s\n" "$ca";;
esac
done | sort -rn
You should use df -P on Linux.
shew01
June 4, 2008, 10:03am
8
radoulov,
radoulov:
Or:
if perl is acceptable:
df -k |
perl -ane'
/%/ and push @x,$F[4];
print join "\n" , reverse sort { $a <=> $b } @x
if eof'
Otherwise:
df -k |awk '1<NR&&NF>1{print $5}'|sort -nr
This works for me too. Thanks!
shew01:
System Shock,
>>A quick look at the man pages would've given you the answer.
I don't think so:
df -k | grep / | awk '{print $5}' | sort -gr
sort: illegal option -- g
usage: sort [-cmu] [-o output] [-T directory] [-S mem] [-z recsz]
[-dfiMnr] [-b] [-t char] [-k keydef] [+pos1 [-pos2]] files...
..well, obviously you are running at the very least a different version of sort because it works for me; you didn't specify an OS or sort version. In any case, it still stands that a quick look at the man pages of whatever OS and sort version you are using would've given you the answer.
shew01
June 4, 2008, 10:34am
10
..well, obviously you are running at the very least a different version of sort because it works for me; you didn't specify an OS or sort version. In any case, it still stands that a quick look at the man pages of whatever OS and sort version you are using would've given you the answer.
I'm not interested in having a posting war with you. Please just ignore my posts.