Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Regular Expression Question

by Anonymous Monk
on Mar 27, 2001 at 21:17 UTC ( [id://67540]=perlquestion: print w/replies, xml ) Need Help??

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

I have a file that looks like this
box1 Filesystem 1k-blocks Used Available Use% Mounted on /dev/hda1 124427 54401 63602 46% / /dev/hda6 5289348 3682364 1338300 73% /usr box2 Filesystem 1k-blocks Used Available Use% Mounted on /dev/hda1 124427 54401 63602 46% / /dev/hda6 5289348 3682364 1338300 73% /usr box3 Filesystem 1k-blocks Used Available Use% Mounted on /dev/hda1 124427 54401 63602 46% / /dev/hda6 5289348 3682364 1338300 73% /usr

This is the output from df -l. The info I need is every box that is over 88% full.Thank You in advance

Replies are listed 'Best First'.
Re: Regular Expression Question
by mirod (Canon) on Mar 27, 2001 at 21:25 UTC

    The answer is that none of them is over 88% full.

    You are welcome.

(boo) Regexp'ing through a logfile
by boo_radley (Parson) on Mar 27, 2001 at 22:27 UTC
    While I am annoyed at the presentation of your question, it gave me a chance to abuse $/ in an entertaining fashion. I think I'm either thinking more perly, or going insane. :)
    use strict; open LOG, "C:/fs.log"; $/="\n\n"; while(<LOG>){ m |^(\w+)\n|; print "Machine $1\n"; while (m|\n(/.*?\s).*?(\d+)%|gi) { print "\t",$2 > 88? "WARNING --> ":"","$1 at $2 %\n" } }
    does the undef need to be there?
    Update No, it can be replaced by the "" you see in the ?: .
Re: Regular Expression Question
by Masem (Monsignor) on Mar 27, 2001 at 21:30 UTC
    OOTOMH...
    while (<FILE>) { my $boxname; if ( ! ( /^Filesystem/ ) ) { $boxname = $_; } else { while (<FILE>) { my ( $title, $blocks, $used, $avail, $percent, $mounted ) += split /\s+/; last if ( !defined( $title ) ); my $number = ( $percent =~ /(\d*)\%/ ); if ( $number > 88 ) { print "$boxname's $title is at $percent!\n"; } } } }

    Update Should be split \/s+\ rather than \/s*\


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Did you mean split(/\s+/)? split(/\s*/) will split it into individual characters because /\s*/ can match a 0 character string.
Re: Regular Expression Question
by japhy (Canon) on Mar 27, 2001 at 23:11 UTC
    Do you mean box, or filesystem? I'll do both. (I'm good like that.)
    $/ = ""; while (<>) { my ($box) = /(.*)/; my ($t_used, $t_avail); for (split /\n/) { my ($fd,$used,$avail,$per) = /^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\%/ or next; $t_used += $used; $t_avail += $avail; warn "$box:$fd at $per%\n" if $per > 88; } my $t_per = int(100 * $t_used / $t_avail); warn "$box at $t_per%\n" if $t_per > 88; }


    japhy -- Perl and Regex Hacker
Re: Regular Expression Question
by Chady (Priest) on Mar 27, 2001 at 21:23 UTC

    read on at this


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
Re: Regular Expression Question
by SilverB1rd (Scribe) on Mar 27, 2001 at 21:39 UTC
    Please tell us exactly what you want to do? If your trying to pull the information out of that output try using a regex.

    Something like this,
    m/(\/.+) +(\d) +(\d) +(\d+) +(\d)% +(.+)/

    You would then be able to test $5 to see if it is over 80.

    NOTE My regex is not in anyway the best and I'm not even sure if that would work. But I hope you get the idea. If you want to know more read up on regex. UPDATE My post was outdated before I even post it :)

    ------
    The Price of Freedom is Eternal Vigilance
      you are right to suspect that this would not work - the single \d as opposed to \d+ before the % would mean that the regex would fail to match unless the % was between 0 and 9.

      what I expect you are looking for is for(<>) { /(\d+)%/; print if $1 >= 88 }

        Thanks larry. Typo on my part. It was supposed to be (\d+)%

        ------
        The Price of Freedom is Eternal Vigilance

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://67540]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2025-06-24 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.