- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: PERL - pattern match
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2007 08:57 AM
02-13-2007 08:57 AM
PERL - pattern match
I am somehow not able to get the logic.
Can anyone please help.
Thanks,
Pat
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2007 09:58 AM
02-13-2007 09:58 AM
Re: PERL - pattern match
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2007 09:58 AM
02-13-2007 09:58 AM
Re: PERL - pattern match
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2007 10:11 AM
02-13-2007 10:11 AM
Re: PERL - pattern match
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' >
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2007 12:19 AM
02-14-2007 12:19 AM
Re: PERL - pattern match
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2007 12:23 AM
02-14-2007 12:23 AM
Re: PERL - pattern match
Explain what is wrong with that?
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2007 07:33 PM
02-14-2007 07:33 PM
Re: PERL - pattern match
your oneliner will not output the line with the first match :-)
mfG Peter