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 new to perl and not able to understand all the pattern matching.
I using this script to send the status to another Nagios server using ncsa. Nsca don't transmit "()" So I need to remove them before sending.
Currently I am using
Code:
$opt_a =~ s/\(/"\(/;
$opt_a =~ s/\)/"\)"/;
for the string
Quote:
ARCServe Minor Trap. BK-LO-BAK01 BK-LO-BAK01: [JobID:1931 NEW JOB - London Servers Backup] Backup Operation Incomplete.Number of Error(s)/Warning(s): 9/5
This is working fine but its is not working when there is change inside the bracket for e.g
(6290)
I want help to change this code to work for any change with in the brackets.
Ask me if I am not clear.
Last edited by rahul_dubeyin; 06-03-2011 at 07:59 AM.
hmmmm ... I might be missing something, but it appears to me that the result for both strings is the same, although personally I am not sure I would desire either outcome.
Here is what I tested:
Code:
#!/usr/bin/perl
use warnings;
use strict;
my $opt_a = "ARCServe Minor Trap. BK-LO-BAK01 BK-LO-BAK01: [JobID:1931 NEW JOB - London Servers Backup] Backup Operation Incomplete.Number of Error(s)/Warning(s): 9/5";
$opt_a =~ s/\(/"\(/;
$opt_a =~ s/\)/"\)"/;
print $opt_a . "\n";
$opt_a = "Power Redundancy Lost (6032): The Power Supplies are no longer redundant on Chassis 0.";
$opt_a =~ s/\(/"\(/;
$opt_a =~ s/\)/"\)"/;
print $opt_a . "\n";
When run I get the following output:
Code:
ARCServe Minor Trap. BK-LO-BAK01 BK-LO-BAK01: [JobID:1931 NEW JOB - London Servers Backup] Backup Operation Incomplete.Number of Error"(s")"/Warning(s): 9/5
Power Redundancy Lost "(6032")": The Power Supplies are no longer redundant on Chassis 0.
I have highlighted the changes made to the strings in red.
The affect appears to be the same for both strings ... that I can see. Or am I missing something?
hmmmm ... I might be missing something, but it appears to me that the result for both strings is the same, although personally I am not sure I would desire either outcome.
The affect appears to be the same for both strings ... that I can see. Or am I missing something?
My apologies here I pasted code from the testing script.
here is the correct code.
Code:
$opt_a =~ s/\(s\)//;
I am using it twice because first string have two occurrence of (s)
Last edited by rahul_dubeyin; 06-03-2011 at 12:02 PM.
So of course this will not work if the brackets do not contain a space. Although copying what you have there does not appear to be a space between the second set
of brackets. What is it you would like to happen?
So of course this will not work if the brackets do not contain a space. Although copying what you have there does not appear to be a space between the second set
of brackets. What is it you would like to happen?
Grail, thanks for taking interest.
The code
Code:
$opt_a =~ s/\(s\)//;
will work for (s) but not for (6299). SO I need help to change it to work. Remember please, the number between the bracket will keep on changing.
G
...
will work for (s) but not for (6299). SO I need help to change it to work. Remember please, the number between the bracket will keep on changing.
As an addition to Sergei's helpful links, you may want to look up how to find everything except the last thing you wish to find. I am being a little cryptic so you have
a chance to work it out, but I believe, if I understand correctly, that the scenario you have is that you wish to replace any and all occurrences of (anything here) ... is this correct?
If so I believe my previous tip will help If not and you are only interested in replacing brackets with numbers inside, then Sergei's advice is the way to go (his links are still
great advice either way )
To aid in solving these kinds of problems, I find it almost always easier to describe in words, what operation you are trying to accomplish. Almost always, the phrases that describe your operation map directly to regex elements. Your case seem to be: 'replace strings of digits between closed parentheses with the same inside double quotes'. Here, the phrases 'replace', 'string of digits' & 'the same', map directly to regex notation. The rest is just matching single characters, or adding single characters. Your job is just to identify what the regex elements are, and put them together to make a regex that operates according to your spec.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.