Korn: How to zero fill df output so it will sort properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn: How to zero fill df output so it will sort properly
# 1  
Old 06-04-2008
Korn: How to zero fill df output so it will sort properly

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:

Code:
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%):

Code:
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:

Code:
92%
88%
28%
27%
 7%
 6%
 6%
 1%
 1%
 0%
 0%
 0%

Any ideas?
# 2  
Old 06-04-2008
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.
Code:
:> 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.
# 3  
Old 06-04-2008
Code:
df -k | awk '/[/]/ { print $5}' | sort -rn

# 4  
Old 06-04-2008
Or:
if perl is acceptable:

Code:
df -k | 
  perl -ane'
    /%/ and push @x,$F[4];
    print join "\n" , reverse sort { $a <=> $b } @x 
if eof'

Otherwise:

Code:
df -k |awk '1<NR&&NF>1{print $5}'|sort -nr

# 5  
Old 06-04-2008
System Shock,

>>A quick look at the man pages would've given you the answer.

I don't think so:

Code:
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...

# 6  
Old 06-04-2008
vgersh99,

Quote:
Originally Posted by vgersh99
Code:
df -k | awk '/[/]/ { print $5}' | sort -rn

Cool! This seems to work. Thanks for the help.
# 7  
Old 06-04-2008
And another one:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I sort data in column properly?

Can i use 'column' command to get the required 3rd column output? Input example: 1 2 345678 90 2 2 356 42 3 3 8265 55 Output required: 1 2 345678 90 2 2 356 42 3 3 8265 55 Basically i want the 3rd column to be justified to the right, instead of left.... (3 Replies)
Discussion started by: nurul_nadzirah
3 Replies

2. Shell Programming and Scripting

Why is sort not working properly here ?

Platform: RHEL 5.4 In the below text file I have strings like following. $ cat /tmp/mytextfile.txt DISK1 DISK10 DISK101 DISK102 DISK103 DISK104 DISK105 DISK106 DISK107 DISK108 DISK109 DISK110 DISK111 DISK112 DISK113 DISK114 (8 Replies)
Discussion started by: kraljic
8 Replies

3. Shell Programming and Scripting

[SOLVED] Capturing output in a korn variable

Hi, I'm new to korn and having trouble capturing the text output from one program in an array that I can then feed into another program. Direct approaches didn't work, so I've tried to break it down thus: The program lonlat2pixline gives the values I need in the second column, so I print that... (4 Replies)
Discussion started by: daurin
4 Replies

4. Shell Programming and Scripting

korn shell to cut command output

hello, i use following command: md5sum TEST.xml the output looks like: 900hjidur84hjr938ikv TEST.xml as you can see, the first part is the md5 code, the second part is the file name, but i only want the first part(md5 code), and save it to a file, how to do that? thanks. (2 Replies)
Discussion started by: zbc
2 Replies

5. Shell Programming and Scripting

How to fill data from other file and get some output

Greetings, I have a hard time creating a large number of user profiles in a database. The data file looks like this : 01/01/80 Mitch Conley . . . . And I need to put the output into: Name: Mitch Surname: Conley Birthday: 01/01/80 Thanks in advance! (3 Replies)
Discussion started by: hemo21
3 Replies

6. Shell Programming and Scripting

Write output to a file using Korn shell script

All, Can anyone please help me with the below scenario in korn shell script. Can anyone please give me some hints to proceed on this. I have a Flat file of the below format. Input file format:... (1 Reply)
Discussion started by: sp999
1 Replies

7. UNIX for Advanced & Expert Users

Command to output ones (like zero-fill), with progress indicator

Well, I was originally going to post this snippet in the original thread titled "how to output ones endlessly like /dev/zero", but that topic was closed without an efficient answer. It was difficult to find (build) a satisfactory answer to this one, so I thought I'd share it here and as a "fill... (1 Reply)
Discussion started by: Gliktch
1 Replies

8. Solaris

not getting the output properly

i am entering the command, not get the total out put see the output what i am getting. SQL> select dbms_metadata.get_ddl ('TABLESPACE','SYSTEM') FROM DUAL; DBMS_METADATA.GET_DDL('TABLESPACE','SYSTEM') -------------------------------------------------------------------------------- ... (0 Replies)
Discussion started by: pmrajesh21
0 Replies

9. Shell Programming and Scripting

Output in my shell isn't showing properly.

Hi! Can anyone tell me what went wrong in my shell script? for dt_val in `cut -f 1 -d '|' /prod/ods/satyaki/sqlldr/grp.dat` do echo $dt_val done And, the output is - 23 39 (7 Replies)
Discussion started by: satyakide
7 Replies

10. UNIX for Dummies Questions & Answers

How to output the results of the AT command - properly!

Hi, I am new to UNIX and I am more used to simple commands like those in VMS. One of them is the ability to get the output from a job using the /out=<file> command in VMS. I want to submit a job (a set of unix commands) using the AT command but to get the output in a file like that used in... (4 Replies)
Discussion started by: SpanishPassion
4 Replies
Login or Register to Ask a Question