Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Removing matched pattern except the first pattern

by AnomalousMonk (Monsignor)
on May 28, 2013 at 23:44 UTC ( #1035750=note: print w/ replies, xml ) Need Help??


in reply to Removing matched pattern except the first pattern

When I first read the OP, the phrase "matched pattern" triggered an "I know, I'll use a regex..." knee-jerk response, and I formulated the problem statement "keep the first n occurrences of a pattern in a string along with any intervening matter and delete all occurrences thereafter".

It turns out that a regex-based approach is not appropriate for the OPed problem unless it be a "keep all lines that match a given regex" strategy, which would, IMHO, be quite good because it combines a possibly quite large element of validation with data extraction and is also completely scaleable.

In any event, proceeding along the lines of my first-but-not-necessarily-best thought, I came up with this, which may be of interest:

>perl -wMstrict -le "my $s = 'x foo1 foo2 x foo3 x yfoo9 foo4 foo5 foo9y x foo6 foo7 x'; print qq{'$s' \n}; ;; my $pat = qr{ \b foo \d \b }xms; ;; for my $n (0 .. 4) { (my $t = $s) =~ s{ \A (?: .*? $pat){$n} \K | $pat }''xmsg; print qq{'$t'}; } " 'x foo1 foo2 x foo3 x yfoo9 foo4 foo5 foo9y x foo6 foo7 x' 'x x x yfoo9 foo9y x x' 'x foo1 x x yfoo9 foo9y x x' 'x foo1 foo2 x x yfoo9 foo9y x x' 'x foo1 foo2 x foo3 x yfoo9 foo9y x x' 'x foo1 foo2 x foo3 x yfoo9 foo4 foo9y x x'

Update: I should add that the  \K regex operator used above is available with Perl versions 5.10+.


Comment on Re: Removing matched pattern except the first pattern
Select or Download Code
Re^2: Removing matched pattern except the first pattern
by rahulruns (Sexton) on May 29, 2013 at 05:28 UTC

    I was able to make the file look like the way I needed but still I am not able to parse the VMstat logs

    use strict; use warnings; use Test::Parser::Vmstat; use Tie::File; my @vmstat_data; tie @vmstat_data, 'Tie::File', $ARGV[0] or die $!; `sed '/[procs|swpd]/d' $ARGV[0] > /tmp/vmstat_intermidiate`; open (my $in, '<', "/tmp/vmstat_intermidiate") or die "Can't read ol +d file: $!"; open (my $out, '>', "/tmp/vmstat_log") or die "Can't write new file: $ +!"; print $out "$vmstat_data[0]\n"; print $out "$vmstat_data[1]\n"; while( <$in> ) { print $out $_; } close $out; close $in; `rm -rf /tmp/vmstat_intermidiate`; my $parser = new Test::Parser::Vmstat or die "Couldn't create Test::Parser::Vmstat object\n"; my $logfile = '/tmp/vmstat_log'; $parser->parse($logfile) or die "Could not parse Vmstat log.\n"; print $parser->to_xml(); OUTPUT procs -----------memory---------- ---swap-- -----io---- --system-- --- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 1 0 0 50122424 234628 9616504 0 0 0 20 2 4 +0 0 99 0 0 0 0 0 50121664 234628 9616864 0 0 0 0 7813 5956 +1 1 98 0 0 1 0 0 50122252 234628 9616864 0 0 0 190 10727 6872 + 2 1 97 0 0 0 0 0 50122092 234628 9616864 0 0 0 164 8645 6966 +1 1 98 0 0 PERL SCRIPT OUTPUT [root@r01mgt ~]# perl parse_vmstat.pl /tmp/vmstat <vmstat> </vmstat>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1035750]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2014-07-25 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls