Front page | perl.beginners |
Postings from August 2011
Re: Output formatting help
Thread Previous
From:
Paolo Gianrossi
Date:
August 1, 2011 02:53
Subject:
Re: Output formatting help
Message ID:
CACssvf2gcvGdryAN7ex4y=oW3VFJoLyWApFMHo7cRW3j2Pentg@mail.gmail.com
2011/8/1 newbie01 perl <newbie01.perl@gmail.com>
>
> Hi all,
>
>
Hi!
> I have several *nix version of Unix and while some I can use df -h, for
> some I can't. The only common thing that I can use that works for all is df
> -k.
>
> ========================================
> Below is the output when running df -k:
> ========================================
>
> Filesystem kbytes used avail capacity Mounted on
> /dev/md/dsk/d1 8268461 4133009 4052768 51% /
> /proc 0 0 0 0% /proc
> mnttab 0 0 0 0% /etc/mnttab
> fd 0 0 0 0% /dev/fd
> /dev/md/dsk/d3 8268461 5751241 2434536 71% /var
> swap 46176776 64 46176712 1% /var/run
> dmpfs 46176712 0 46176712 0% /dev/vx/dmp
> dmpfs 46176712 0 46176712 0% /dev/vx/rdmp
> swap 53453216 7276504 46176712 14% /tmp
> /dev/md/dsk/d4 16526762 8075725 8285770 50% /opt
> /dev/vx/dsk/dg_volarch/SUNWspro
> 6094894 5173296 860650 86% /opt/SUNWspro.local
> /dev/vx/dsk/dg_volarch/volarch
> 83886080 61130760 22577632 74% /vx_mnt/volarch
> aklns001:/vol/vol_admin/public
> 322355200 291922136 30433064 91% /nas_mnt/public
> /dev/odm 0 0 0 0% /dev/odm
> aklns002:/vol/vol_admin/prodhome
> 33646592 25395656 8250936 76% /home/users
> /vol/dev/dsk/c0t0d0/sol_10_1008_sparc
> 2599020 2599020 0 100%
> /cdrom/sol_10_1008_sparc
> aklns003-va2:/vol/vol_aftp/aftp/app
> 48179200 39619484 8559716 83%
> /nas_mnt/aklns022/app_aftp
> /dev/vx/dsk/dg_ico/prod01_db
> 188743680 136754136 51583424 73%
> /vx_mnt/db/prod01
> /dev/vx/dsk/dg_ico/prod01_app
> 52428800 31237744 21030000 60%
> /vx_mnt/app/prod01
>
> Interestingly, if I re-direct the output to a file, I get one line for each
> which is a lot better than the screen output. For example, if I do df -k >
> /tmp/x, the contents of /tmp/x is as below:
>
>
> Filesystem kbytes used avail capacity Mounted on
> /dev/md/dsk/d1 8268461 4133009 4052768 51% /
> /proc 0 0 0 0% /proc
> mnttab 0 0 0 0% /etc/mnttab
> fd 0 0 0 0% /dev/fd
> /dev/md/dsk/d3 8268461 5750757 2435020 71% /var
> swap 45929352 64 45929288 1% /var/run
> dmpfs 45929288 0 45929288 0% /dev/vx/dmp
> dmpfs 45929288 0 45929288 0% /dev/vx/rdmp
> swap 53205808 7276520 45929288 14% /tmp
> /dev/md/dsk/d4 16526762 8079410 8282085 50% /opt
> /dev/vx/dsk/dg_volarch/SUNWspro 6094894 5173296 860650 86%
> /opt/SUNWspro.local
> /dev/vx/dsk/dg_volarch/volarch 83886080 61184016 22524792 74%
> /vx_mnt/volarch
> aklns001:/vol/vol_admin/public 322355200 291933108 30422092 91%
> /nas_mnt/public
> /dev/odm 0 0 0 0% /dev/odm
> aklns002:/vol/vol_admin/prodhome 33646592 25395684 8250908 76%
> /home/users
> /vol/dev/dsk/c0t0d0/sol_10_1008_sparc 2599020 2599020 0 100%
> /cdrom/sol_10_1008_sparc
> aklns003-va2:/vol/vol_aftp/aftp/app 48179200 39619484 8559716 83%
> /nas_mnt/aklns022/app_aftp
> /dev/vx/dsk/dg_ico/prod01_db 188743680 136754136 51583424 73%
> /vx_mnt/db/prod01
> /dev/vx/dsk/dg_ico/prod01_app 52428800 31239768 21027992 60%
> /vx_mnt/app/prod01
>
> I've attached a screenshot of the desired output that am hoping to have.
> Tried to paste the manual formatting that I did but it comes out jagged
> similar to the output to a file.
>
> At the moment, only way I can think of getting the kind of formatting that
> am wanting to have is to do as below:
> - Read each line of the df output where each column of the line is a
> separate field
> - Count the number of characters for each respective field
> - Note down what is the max characters for each respective field, this will
> be the max number of characters + 10 for each field, each column that is
> less than this max characters will be padded with spaces
> - Then print out each line left-justified using Perl print formatting
>
>
Can you get to CPAN modules? I'd use e.g. Text::Table:
use strict;
use warnings;
use Text::Table;
my $tb = Text::Table->new(
'Filesystem', 'kbytes','used', 'avail', 'capacity', 'Mounted on',
);
while(<DATA>){
$tb->load($_);
}
print $tb;
__DATA__
/dev/md/dsk/d1 8268461 4133009 4052768 51% /
/proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
Not sure if this is the best way to format the output to the one that I want
> to but can't think of any way of doing it.
>
> Preferably, would want to be able to show the df output in GB/MB as well.
> That means I have to do my own conversion since some of the Solaris/*nix
> that I have does not have the -h option. That should be the next objective
> once I've figure out how to do the format thingy.
my $mb = $kb * 1024;
my $gb = $mb * 1024;
BTW, am using the system command or backticks to run df -k.
>
> Hopefully, the df output to file will always be one line for each. Any
> suggestion will be much appreciated. Thanks in advance.
>
HTH
cheers
paolino
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
> --
Paolo Gianrossi
(An unmatched left parenthesis
creates an unresolved tension
that will stay with you all day
-- xkcd
Thread Previous