rajeshatbuzz has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I could get output of following commands in such fashion...
ssh -q $server 'df -hP /raj* Size Used Avail Capacity Mounted On 200G 154G 44G 79% /raj_day 200G 154G 44G 49% /raj1_day 200G 154G 44G 39% /raj2_day
Now I would like to convert into Tabular format so presentation should be good. Also, want to display Capacity in sorting order. Any tips for me in perl?

Replies are listed 'Best First'.
Re: df output in tabular form using perl
by choroba (Cardinal) on Apr 26, 2012 at 15:45 UTC
    One solution using Text::Table:
    #!/usr/bin/perl use warnings; use strict; use Text::Table; use constant CAPACITY => 3; my @lines = `df -hP`; my $header = shift @lines; # Do not create two columns for "Mounted" and "On" $header =~ s/Mounted /Mounted_/; my $table = Text::Table->new(split ' ', $header); { no warnings 'numeric'; # Ignore % signs @lines = sort { $a->[CAPACITY] <=> $b->[CAPACITY] } map [split ' ', $_], @lines; } $table->load(@lines); print $table;
    In my systems, Capacity is called "Use%" and is in a different column.
Re: df output in tabular form using perl
by Anonymous Monk on Apr 26, 2012 at 15:26 UTC
Re: df output in tabular form using perl
by 2teez (Vicar) on Apr 26, 2012 at 16:42 UTC
Re: df output in tabular form using perl
by rajeshatbuzz (Novice) on Apr 30, 2012 at 09:31 UTC
    I have following script but its not working as expected. Any clue???
    ############################################# #!/usr/local/bin/perl # Use either -h or -k flag or leave it blank for default (-k) # -h for human-readable size output # -k for 1k-block output $flag = "-h"; @df = `df $flag`; print "Content-type: text/htmln\n"; print "<table border=2>\n"; print "<tr>\n"; print "<td><b>Filesystem</b></td>\n"; if ($flag eq "-h") { print "<td><b>Size</b></td>\n"; } else { print "<td><b>1k-blocks</b></td>\n"; } print "<td><b>Used</b></td>\n"; print "<td><b>Avail</b></td>\n"; print "<td><b>Capacity</b></td>\n"; print "<td><b>Mounted on</b></td>\n"; print "</tr>\n"; foreach $line (@df) { next if ($line =~ /Filesystem/); ($fsystem,$blocks,$used,$avail,$capacity,$mounted) = split(/s+ +/,$line); print "fsystem is $fsystem\n"; print "blocks is $blocks\n"; print "used is $used\n"; print "avail is $avail\n"; print "capacity is $capacity\n"; print "mounted is $mounted\n"; ($number,$sign) = split(/%/,$capacity); if ($number < 60) { print "<tr bgcolor=green>\n"; } elsif (($number >= 60) && ($number < 90)) { print "<tr bgcolor=yellow>\n"; } else { print "<tr bgcolor=red>\n"; } # print "<td>$fsystem</td>\n"; print "<td>$blocks</td>\n"; print "<td>$used</td>\n"; print "<td>$avail</td>\n"; print "<td>$capacity</td>\n"; print "<td>$mounted</td>\n"; print "</tr>\n"; } print "</table>\n";