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

Greetings, I'm trying to parse out the following statement and convert it to an html table
Disk quotas for testuser (uid 65530): Filesystem usage quota limit timeleft files quota limit + timeleft /var/mail 1394 15360 16384 0 0 0 + /home 255999 307200 308224 34 0 0
My first thought was $quota_string =~ s/\s+/ /g; $quota_string =~ s/:/:\n/; and then try to match the whitespace /words and put a new line in front of it $quota_string =~ s/\s{1}\/$1\n/g; and then convert all the \n to <tr> and all the spaces to <td> I haven't entirely worked all the bugs out of this idea yet, because the mildly complicated regex doesn't work....

And then there is also the problem of, when converting all the whitespaces to one space the files field shifts over to the timeleft field... anyone have any thoughts on how to insert perhaps a "-" if there is nothing there?

I was considering something as hidous as this to help but....

my $ugly_regex = '! / # match / \w*? # match optional minimum word characters (/dir +) /? # match single optional / \w*? # match optional minimum word characters (/dir +/dir) /? # match single optional / \w*? # match optional minimum word characters (/dir +/dir/dir) /? # match single optional / \w*? # match optional minimum word characters (/dir +/dir/dir/dir) \s? # match a single optional whitespace \d+? # match 1 or more numbers \s? # match a single optional whitespace \d+? # match 1 or more numbers \s? # match a single optional whitespace \d+? # match 1 or more numbers \s? # match a single optional whitespace \d+? # match 1 or more numbers \s? # match a single optional whitespace \d+? # match 1 or more numbers \s? # match a single optional whitespace \d+? # match 1 or more numbers \s? # match a single optional whitespace \d*? # match 0 or more numbers \s? # match a single optional whitespace \d*? # match 0 or more numbers !x';
that is ugly as sin and........ yeah....doesn't work...
Anyone got any thoughts in that direction? I would use Quota, except this is over a remote connection and I cannot always be sure that module has been installed.

Update: By remote I mean through an telnet/ssh session emulated by either Net::Telnet or Net::SSH::Perl


jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)

Replies are listed 'Best First'.
Re: patern matching whitespace, / and others
by revdiablo (Prior) on Mar 01, 2004 at 19:20 UTC

    I think unpack might make sense here, since these appear to be fixed-width columns. The only thing that makes me hesitate is the information about /home is on the line below it. I'm not sure whether this was intentional or not, but could probably be solved with a bit of data massaging before passing to unpack. Here's a snippet that assumes the data has been cleaned up:

    #!/usr/bin/perl use strict; use warnings; my $template = "A14 A7 A7 A9 A10 A7 A7 A9 A9"; my @quotas; push @quotas, [ unpack $template, $_ ] while <DATA>; use Data::Dumper; print Dumper(\@quotas); __DATA__ Filesystem usage quota limit timeleft files quota limit + timeleft /var/mail 1394 15360 16384 0 0 0 /home 255999 307200 308224 34 0 0
      Cool, thanks I'll look into that
      sadly yes, /home is on a new line on some of my machienes (I HATE YOU SOLARIS), but I may have a way around that so.......

      thanks


      jcpunk
      all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)
Re: patern matching whitespace, / and others
by esskar (Deacon) on Mar 01, 2004 at 16:52 UTC
    well, does Quota help you?

    Update:
    about the remote stuff: "The Quota module provides access to file system quotas. The quotactl system call or ioctl is used to query or set quotas on the local host, or queries are submitted via RPC to a remote host"
      I'm looking over the source for it, but sadly it doesn't appear that I can figure the important guts of it out, and, as mentioned before, I cannot be sure all of the hosts this will be running on will have it installed....
      And this also needs to work on solaris, irix, and linux(x86), which makes that little bit it compiles tricky for doing the use lib thing

      jcpunk
      all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)
        just write a little setup script which checks if "Quota" is installed and if not, install it using the version in your self-made setup-package.
        reinevnting the wheel can be a pain; and when i look at your regexp above, it really is. :)
Re: patern matching whitespace, / and others
by Roy Johnson (Monsignor) on Mar 01, 2004 at 18:34 UTC
    For matching the directory paths, you don't want to specify every possible level. You probably want:
    (?:/\w*?)+ # /dir or /dir/dir2 or ....
    But you probably don't want to use a regex to process the data lines. To get the numbers paired with the right columns, you probably ought to figure out which columns the headers are in, and use unpack() to grab what lines up below them.

    The PerlMonk tr/// Advocate