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

Good morning, I am working hard to get a grip on regular expressions and have tried working with them for the past few days to accomplish my object. That objective is: to break apart output from the 'quota -v' command and print it in an html table. I am confident (thanks to the very helpful responses of previous posters) that I can apply html tags to the individual parts of the output once I have it broken apart with regular expressions. The 'quota -v' command outputs:
Disk quotas for jjasinsk (uid 6722945): Filesystem usage quota limit timeleft files quota limit + timeleft /var/mail 10688 15360 16384 0 0 0 + /export/home3 98386 307200 308224 2003 0 0

With the following code, I am able to remove all of the extra white space and that weird newline that occurs after /export/home:
#!/usr/bin/perl $input=`quota -v`; $input =~ s/\/export\/home[1-5] *\n/\/home/; $input =~ s/\s+/ /g; $input =~ s/\/var/\n\/var/; $input =~ s/\/home/\n\/home/; $input =~ s/: /:\n/; print $input;

The output of this code is:
Disk quotas for jjasinsk (uid 6722945): Filesystem usage quota limit timeleft files quota limit timeleft /var/mail 10688 15360 16384 0 0 0 /home 98386 307200 308224 2003 0 0

However, I am finding that, what used to be very simple in unix is slightly more difficult in perl. I would like to "grep" for and remove the first two lines of the output. Better yet, I would like to store the first line in a variable so that I might display things like the uid or the username somewhere else on my html page.
I have read through various sources on regular expressions, including one from "Perl Black Book", the online source "Perl Regular Expression Tutorial," and have looked up information on grep and egrep, but have had no luck in being able to separate those first two lines from my output. If anyone here would be gracious enough to give me a few pointers, I would be very appreciative. Thanks for your time.

Replies are listed 'Best First'.
Re: Irregular Expressions
by shotgunefx (Parson) on Jun 17, 2003 at 16:21 UTC
    Instead of using a single scalar, why not call quota in list context?
    @lines = `quota -v`; my $header = shift @lines;
    As an aside, regular expressions might not be best for something that has fairly predictable formatting. Then again, It's my weakest area of Perl knowledge by far so take that with a grain of salt. Faced with the same problem, I came up with Parsing program output snippets.

    -Lee

    "To be civilized is to deny one's nature."
      Thanks a bunch! That an impressive parsing program that you have included. I will take a close look at it to see exactly what you have done. Thanks again.
Re: Irregular Expressions
by monsieur_champs (Curate) on Jun 17, 2003 at 16:36 UTC

    Hello, JoeJaz.

    I think that you're looking for the Tom Zoerner's Quota Module. This module provides you with everything you would like to know about your disk quotas, without forcing you to learn about regular expressions now.

    I think you should look at the CGI module, too. It will help you generating clean HTML, in a comprehensible and easy-to-use way.

    Hope that helps.

    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Just Another Perl Monk

      Thanks for those suggestions! I will have a look at the quota module. It might help make my output more clean. Once I am confident with my output, I will integrate it with the rest of my program and plan to use the CGI module there. Thanks again.
Re: Irregular Expressions
by Anonymous Monk on Jun 17, 2003 at 16:18 UTC

    Try my @lines = split(/\n/, $quotaOutput);

    perldoc -f split for more info. This'll get you a nice list with each line in a separate element, so you can throw lines away or handle them separately. To go back to a string when you're done, use join.

      Thanks for the information. That seemed to work well. I can't believe that the answer is that simple! Thanks again.
(jeffa) Re: Irregular Expressions
by jeffa (Bishop) on Jun 17, 2003 at 21:41 UTC
    "... I am finding that, what used to be very simple in unix is slightly more difficult in perl."

    Enter CPAN ;)

    use strict; use warnings; use Quota; use DBIx::XHTML_Table; my @dev = qw(/var/mail /home); my $data = [map [Quota::query($_)], @dev]; my $header = [qw( block_curr block_soft block_hard block_timelimit inode_curr inode_soft inode_hard inode_timelimit )]; my $table = DBIx::XHTML_Table->new($data,$header); $table->modify(table=>{border=>1}); $table->modify(td=>{align=>'right'}); print $table->output;
    UPDATE: i finally decided to 'bless' the add_cols() and drop_cols() methods that were currently undocumented. Grab the latest version of DBIx::XHTML_Table (1.30 at the time of this update).

    By using add_cols(), you can list the device as well:

    ... rest of code untouched $table->add_cols({name=>'Device',data=>[@dev],before=>0}); print $table->output;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Very Cool! Seems fairly simple afterall and much cleaner than regular expressions. I appreciate the example.
Re: Irregular Expressions
by ant9000 (Monk) on Jun 17, 2003 at 16:34 UTC
    Well, you do have the option to use grep: try perldoc -f grep.
    After you cleanup, you might try:
    print grep(/^\//,split(/\n/,$input));
    which greps only lines beginning with a '/'.
      Thanks! When I tried using grep for perl, I lacked the farmiliarity with the syntax to get any results. This really helps me get a better understanding of how grep is used in perl.
Re: Irregular Expressions
by hmerrill (Friar) on Jun 17, 2003 at 18:00 UTC
    Along the lines of the 'Teach a man to fish...' saying, always remember that there is excellent documentation included with perl called the 'perldocs'. The perldocs come with a great index that you can see by doing
    perldoc perl or perldoc perldoc
    at a command prompt. If you are wondering if perl has a grep function, or how to use perl's grep function, you can use
    perldoc -f grep
    and if perl *has* a grep function, that command will list the 'help' for it. Or, you can actually *search* the perldocs for a topic by doing 'perldoc -q <search term>'.

    HTH.
      I really appreciate that information. I had no idea that a tool like that was built into the perl distribution. Hopefully it will be a lot easier and more strait forward than using a web browser to search for sometimes very vague informaiton online. Thanks a bunch.

      You can also use perldoc perldoc to read about perldoc itself.

      And a minor nitpick: perldoc -q <search item> looks for the topic in the FAQ. So it's possible to miss something that is in the perldoc using the -q switch if it's not in the FAQ. qx is one example (although admittedly it took me a bit of searching to find this example).

      Specifically, perldoc -q qx gives me the output No documentation for perl FAQ keyword 'qx' found even though perldoc -f qx produces Generalized quotes. See the section on "Regexp Quote-Like Operators" in the perlop manpage.