LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2019, 02:35 PM   #1
Corrado
Member
 
Registered: Aug 2004
Location: Washington
Distribution: RHEL 5.0
Posts: 161

Rep: Reputation: 16
Unable to pattern match in Perl


The pattern which I'm trying to match is...

Quote:
Linux Kernel 2.6.32-504.el6.x86_64 on Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
Below is what I have and doesn't appear to be matching. Trying to grab version into $1.

Quote:
if ($line =~ /Linux Kernel 2\.6\.32-504\.el6\.x86_64 on Red Hat Enterprise Linux Workstation release (6\.6) \(Santiago\)/) {
$version = $1;
}
Where have I gone wrong?
 
Old 08-23-2019, 03:10 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205
Quote:
Originally Posted by Corrado View Post
The pattern which I'm trying to match is...
Code:
 	The pattern which I'm trying to match is...
Linux Kernel 2.6.32-504.el6.x86_64 on Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
Below is what I have and doesn't appear to be matching. Trying to grab version into $1.
Code:
if ($line =~ /Linux Kernel 2\.6\.32-504\.el6\.x86_64 on Red Hat Enterprise Linux Workstation release (6\.6) \(Santiago\)/) {
$version = $1;
}
Where have I gone wrong?
What is in the $line variable? Where did it come from?
Do you get any error messages?
Code:
$version = $1;
will put $1 into $version. Where is $1 coming from?
 
1 members found this post helpful.
Old 08-23-2019, 06:59 PM   #3
Corrado
Member
 
Registered: Aug 2004
Location: Washington
Distribution: RHEL 5.0
Posts: 161

Original Poster
Rep: Reputation: 16
Quote:
$line = 'Linux Kernel 2.6.32-504.el6.x86_64 on Red Hat Enterprise Linux Workstation release 6.6 (Santiago)'
My understanding of Perl, is that if the pattern is matched, whatever is between () will be stored in $1.
 
Old 08-23-2019, 10:04 PM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Quote:
Originally Posted by Corrado View Post
My understanding of Perl, is that if the pattern is matched, whatever is between () will be stored in $1.
Yes. That is correct. However, the more complex or specific the pattern is the more unlikely it will get matched. So I would simplify the pattern a bit:

Code:
#!/usr/bin/perl                                                                 

use strict;
use warnings;

my $version;

while (my $line = <DATA>) {
    chomp $line;
    print qq($line\n);
    if( $line=~/ release ([\d\.]+) \(\w+\)/ ) {
        $version = $1;
        print qq(Version = $version\n);
    }
}

__DATA__
Linux Kernel 2.6.32-504.el6.x86_64 on Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
Do you have other examples of strings to be matched? What is the source of these strings?

Last edited by Turbocapitalist; 08-23-2019 at 10:23 PM.
 
1 members found this post helpful.
Old 08-23-2019, 11:38 PM   #5
Corrado
Member
 
Registered: Aug 2004
Location: Washington
Distribution: RHEL 5.0
Posts: 161

Original Poster
Rep: Reputation: 16
Because of your help, I was able to get it working in my script. You really saved me. Thank you.

Am parsing a log file from Nessus. The particular string you helped me with can commonly be found in redhat hosts at /etc/redhat-release

On a slightly different topic but still within Perl, I have to be able to tell the script when the last line in an array has been read, in an if statement. Is there a last line function or something I should be looking at to make it as easy as possible?

P.S - I'm using the foreach function to go through each line in the array.

Last edited by Corrado; 08-23-2019 at 11:41 PM.
 
Old 08-24-2019, 12:56 AM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Quote:
Originally Posted by Corrado View Post
On a slightly different topic but still within Perl, I have to be able to tell the script when the last line in an array has been read, in an if statement. Is there a last line function or something I should be looking at to make it as easy as possible?

P.S - I'm using the foreach function to go through each line in the array.
I'm not sure of the context, bue in general you could find it with a -1 to find the first element counting from the end of the array.

Code:
#!/usr/bin/perl                                                                 

use strict;
use warnings;

my @a = ('a', 'b', 'c', 'd', 'e', 'f', 'g');

foreach my $element ( @a ) {
    if ( $element ne $a[-1] ) {
        print $element,qq(\n);
    } else {
        print $element,qq( - last\n);
    }
}

exit 0;
See "man perlintro" for a quick overview of arrays, "man perllol" for a details on lists (arrays), and "man perltoc" for the table of contents for the manuals.
 
1 members found this post helpful.
Old 08-24-2019, 01:09 AM   #7
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
I should also add that the documentation for the regular expression pattern matching can be found in "man perlre" and "man perlretut". Most of that will apply to PCRE found elsewhere.
 
Old 08-24-2019, 06:18 PM   #8
Corrado
Member
 
Registered: Aug 2004
Location: Washington
Distribution: RHEL 5.0
Posts: 161

Original Poster
Rep: Reputation: 16
Thank you for the perl man pages info and the -1 for finding last index in an array.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 12:36 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
irrelevant characters match in PERL pattern matching gaynut Programming 2 08-21-2008 10:52 PM
perl pattern match question lluciano Programming 4 02-28-2006 05:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration