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

I have tried and failed, you guys have been great. Can you please assist in formatting my output which goes to a file (spaceavail). I am trying to format disk space output, well something similar anyway. TIA
Filesystem kbytes used avail capacity Mounted on /xxxdev/xxxx/xxxxx 2016343 1392038 563815 72% / /ppppp 0 0 0 0% /proc mnrop 0 0 0 0% /etc/mnrop hd 0 0 0 0% /xxxdev/hd ssss 3888816 40 3888776 1% /var/run ssss 3888848 72 3888776 1% /tmp /xxxdev/xxx/xxxxxxxx 32442879 523718 31594733 2% /export/ho +me
format DISKSPACE = ====================== |@<<<<<<<<<<<<<<<<<<<| $Filesystem |@<<<<<<<<<<<<<<<<<<<| $kbytes |@<<<<<<<<<<<<<<<<<<<| $used |@<<<<<<<<<<<<<<<<<<<| $avail |@<<<<<<<<<<<<<<<<<<<| $capacity |@<<<<<<<<<<<<<<<<<<<| $Mounted_On ====================== . sub df_total { open my $dfout, "df -k|" or die "couldn't start df -k: $!"; open (DISKSPACE, "> spaceavail") || die "Error opening file spaceavail +"; while (<$dfout>) { #chomp; ($Filesystem, $kbytes, $used, $avail, $capacity, $Mounted_On) = split +(//); write (DISKSPACE); } } print "\n My First Perl Menu\n"; print "A. Current Date & Time\n"; print "B. Users currently logged in\n"; print "C. Name of working directory\n"; print "D. Contents of the working directory\n"; print "E. Names & Login names of user currently logged on to aries\n" +; print "F. Aries Disk Space Utilization\n"; print "Enter A, B, C, D, E, F: \n"; $answer = <STDIN>; chomp ($answer); if ($answer eq "A") { system "date \n"; } elsif ($answer eq "B") { system "who \n"; } elsif ($answer eq "C") { system "pwd \n"; } elsif ($answer eq "D") { system "ls -Fla \n"; } elsif ($answer eq "E") { system "finger \n"; } elsif ($answer eq "F") { system "df -k \n"; &df_total } else { print "The selection you entered: $answer, is incorrect. Please ent +er A, B, C , D, E or F \n"; }

Replies are listed 'Best First'.
Re: Formatting Text Output
by Paladin (Vicar) on Jan 30, 2004 at 22:42 UTC
    You are using // to split on, which gives you a list of each character. You probably want to split on /\s+/ to split on 1 or more spaces.
      I have tried that and the entire file blows up. This is what is returned. I get the headers and the header results fall in the next block. Thanks for taking a look see.
      ====================== |Filesystem | |kbytes | |used | |avail | |capacity | |Mounted | ====================== ====================== |/dev/dsk/c1t0d0s0 | |2016343 | |1392095 | |563758 | |72% | |/ | ====================== ====================== |/proc | |0 | |0 | |0 | |0% | |/proc | ====================== ====================== |mnttab | |0 | |0 | |0 | |0% | |/etc/mnttab | ====================== ====================== |fd | |0 | |0 | |0 | |0% | |/dev/fd | ====================== ====================== |swap | |3888376 | |40 | |3888336 | |1% | |/var/run | ====================== ====================== |swap | |3888432 | |96 | |3888336 | |1% | |/tmp | ====================== ====================== |/dev/dsk/c1t0d0s7 | |32442879 | |523735 | |31594716 | |2% | |/export/home | ======================
        It sounded like that was the output you were looking for. Can you show what you want it to look like?
Re: Formatting Text Output
by demerphq (Chancellor) on Jan 30, 2004 at 23:44 UTC

    Ah, from the way youve put things its not entirely clear what your problem is. It kinda seems like you want to output in columns, but the format you have displayed is not in columns. In fact the output you show in the other reply is pretty much exactly what I expected from your code so I suspect your understanding of formats is a little shaky. Have a look at perlform and then rethink.

    BTW, a good chunk of the code you have posted is not relevent to question. Try to reduce your problems down to their essential. Not only becuase its easier for people to explain a clear simple example, but also because many times by doing the reduction you find the answer anyway. Anyway, have read of perlform and if you stil can't get it to work as you expect come back with a more refined question. May I also suggest a look at sprintf?


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi