1830231 Members
1913 Online
109999 Solutions
New Discussion

PERL - pattern match

 
Pat Tom
Occasional Contributor

PERL - pattern match

I am writing a PERL script that would search for a pattern in a file. Once the pattern is found, the script should store the portion of the file from the pattern till the end of the file in a buffer for further processing.

I am somehow not able to get the logic.

Can anyone please help.

Thanks,
Pat
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: PERL - pattern match

Hi Pat:

OK, this script meets your requirement. Once a pattern is found, the remainder of a line is stored and every line in the file thereafter. You can do what you wish with the array --- I printed what I stored (buffered) to show the technique.

# cat ./buffer
#!/usr/bin/perl
use strict;
use warnings;
my $pattern = shift;
my $ok;
my @lines;
while (<>) {
chomp;
if (/($pattern)(.*)/) {
push( @lines, $2 );
$ok++;
}
push( @lines, $_ ) if $ok;
}
print "$_\n" for (@lines)

...run as:

# ./buffer local /etc/hosts

...that is, pass the pattern to match followed by the file to be processed.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: PERL - pattern match

Hi Pat:

OK, this script meets your requirement. Once a pattern is found, the remainder of a line is stored and every line in the file thereafter. You can do what you wish with the array --- I printed what I stored (buffered) to show the technique.

# cat ./buffer
#!/usr/bin/perl
use strict;
use warnings;
my $pattern = shift;
my $ok;
my @lines;
while (<>) {
chomp;
if (/($pattern)(.*)/) {
push( @lines, $2 );
$ok++;
}
push( @lines, $_ ) if $ok;
}
print "$_\n" for (@lines)

...run as:

# ./buffer local /etc/hosts

...that is, pass the pattern to match followed by the file to be processed.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: PERL - pattern match

Pat,

JRF has the 'de luxe' solution.
If you study it, you'll see it raises (and answer) some questions:

'from the pattern' is that to be taken literally? That is, should it included the pattern itself, or perhaps even the whole 'line' which contained the pattern?

'till the end': What to do with new-lines?


probably not what you need, but here is a one line for the simplest interpretation of the request...

perl -ne '$go++ if /pattern/; print if $go' >


Hein.

James R. Ferguson
Acclaimed Contributor

Re: PERL - pattern match

Hi (again) Pat:

Sorry for the double post. The Forum returned an error in posting the first time --- I should know better.

There is a dumb correction, anyway I need to make! The original solution dropped a 'next' and treated multiple instances of the pattern downstream from the first occurance as reason to chop the beginning of the line again. That's probably not desirable. Hence, use this:

#!/usr/bin/perl
use strict;
use warnings;
my $pattern = shift;
my $ok;
my @lines;
while (<>) {
chomp;
if ( /($pattern)(.*)/ && ! $ok ) {
push( @lines, $2 );
$ok++;
next;
}
push( @lines, $_ ) if $ok;
}
print "$_\n" for (@lines);

Note, however, that you will *miss* the line which *ends* with the pattern sought. According to your specifications, you only want "...from the pattern until the end...". Perhaps you can clarify what behavior you want.

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: PERL - pattern match

# perl -ne'1../pattern/ or print' file > part

Explain what is wrong with that?

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Nikitka
Honored Contributor

Re: PERL - pattern match

Hi Merijn,

your oneliner will not output the line with the first match :-)

mfG Peter

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"