ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to match the following data over multiple lines:
Code:
Jan 21 22:38:09 MSIDCB01DC HEALTH: HMNOTIFY: Link State Propagation (Sensor MSIABC01EIS): Severity: recovery: s2p2_s2p1 is Normal
s1p2_s1p1 is Normal
Jan 21 22:38:56 MSIDCB01DC HEALTH: HMNOTIFY: Link State Propagation (Sensor MSIABC02EIS): Severity: recovery: s2p1_s2p2 is Normal
s1p2_s1p1 is Normal
I need to pull the name of the sensor out and also the interface states and interface names. I have no issues pulling that info off of the first line, but I am unable to pull it from the second line. I am using perl. Any assistnace would be appreciated.
Could you post your script and show where it should be improved ?
I don't get the issue with multiple lines if you get the message line by line :/
Here's the part of the script that is in question. I am receiving these messages line-by-line. So, I can't do a nested if like below because the second line will not match the first if. I need to match across both lines to pull out the Sensor hostname, interface names and also the state of the interface across both lines. I have tried using /n to match the new line and even .* to match the newline and have been unsuccessful.
Code:
#Jan 21 22:38:09 MSIABC01DC HEALTH: HMNOTIFY: Link State Propagation (Sensor MISABC01EIS): Severity: recovery: s2p2_s2p1 is Normal
#s1p2_s1p1 is Normal
if(/link state propagation \(Sensor (MSI[a-z]{3}[0-9]{2,3}[a-z]*)\).* (s[0-9]p[0-9]_s[0-9]p[0-9]) is (normal|triggered)/smi)
{
my $hostname=$1;
my @interface;
push(@interface,$2);
push(@interface,$3);
if(/(^s[0-9]p[0-9]_s[0-9]p[0-9]) is (normal|triggered)/i)
{
push(@interface,$1);
push(@interface,$2);
}
print @interface;
}
#Jan 21 22:38:09 MSIABC01DC HEALTH: HMNOTIFY: Link State Propagation (Sensor MISABC01EIS): Severity: recovery: s2p2_s2p1 is Normal
#s1p2_s1p1 is Normal
my @interface;
if(/link state propagation \(Sensor (MSI[a-z]{3}[0-9]{2,3}[a-z]*)\).* (s[0-9]p[0-9]_s[0-9]p[0-9]) is (normal|triggered)/smi)
{
my $hostname=$1;
push(@interface,$2);
push(@interface,$3);
}
if(/(^s[0-9]p[0-9]_s[0-9]p[0-9]) is (normal|triggered)/i)
{
push(@interface,$1);
push(@interface,$2);
}
print @interface;
#Jan 21 22:38:09 MSIABC01DC HEALTH: HMNOTIFY: Link State Propagation (Sensor MISABC01EIS): Severity: recovery: s2p2_s2p1 is Normal
#s1p2_s1p1 is Normal
my @interface;
if(/link state propagation \(Sensor (MSI[a-z]{3}[0-9]{2,3}[a-z]*)\).* (s[0-9]p[0-9]_s[0-9]p[0-9]) is (normal|triggered)/smi)
{
my $hostname=$1;
push(@interface,$2);
push(@interface,$3);
}
if(/(^s[0-9]p[0-9]_s[0-9]p[0-9]) is (normal|triggered)/i)
{
push(@interface,$1);
push(@interface,$2);
}
print @interface;
That is a possibility, but it would be much cleaner to use regex to match across both lines.
Why cleaner ? For coding style, personally I prefer separate ifs, but YMMV
Another solution is to store first line in a variable, append the second line, then use multiline matching, but I don't see the point, better to process lines as soon as they arrive imho
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.