LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 06-03-2011, 06:29 AM   #1
rahul_dubeyin
Member
 
Registered: Apr 2011
Posts: 34

Rep: Reputation: 12
Changing pattern from string in Perl


Hi All,

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.
 
Old 06-03-2011, 06:41 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Would you mind providing a second example of what does not work?
It is not clear how adding a different string between () affects your process?
 
Old 06-03-2011, 07:58 AM   #3
rahul_dubeyin
Member
 
Registered: Apr 2011
Posts: 34

Original Poster
Rep: Reputation: 12
Here is the second example.

Quote:
Power Redundancy Lost (6032): The Power Supplies are no longer redundant on Chassis 0.
The above mentioned code is not working for this scenario. The number between the () is keep on changing in every string.
 
Old 06-03-2011, 08:42 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 06-03-2011, 11:54 AM   #5
rahul_dubeyin
Member
 
Registered: Apr 2011
Posts: 34

Original Poster
Rep: Reputation: 12
Quote:
Originally Posted by grail View Post
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.
 
Old 06-03-2011, 11:58 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by rahul_dubeyin View Post
My apologies here I pasted code from the testing script.

here is the correct code.

Code:
$opt_a =~ s/\(s\)//;
Why do you have the item in red ?
 
Old 06-03-2011, 12:00 PM   #7
rahul_dubeyin
Member
 
Registered: Apr 2011
Posts: 34

Original Poster
Rep: Reputation: 12
Quote:
Originally Posted by Sergei Steshenko View Post
Why do you have the item in red ?

The code will replace (s) to null(nothing).

Last edited by rahul_dubeyin; 06-03-2011 at 12:04 PM.
 
Old 06-03-2011, 12:03 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 06-03-2011, 12:11 PM   #9
rahul_dubeyin
Member
 
Registered: Apr 2011
Posts: 34

Original Poster
Rep: Reputation: 12
Quote:
Originally Posted by grail View Post
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.
 
Old 06-03-2011, 12:16 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by rahul_dubeyin View Post
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.
See http://perldoc.perl.org/perlrequick.html -> http://perldoc.perl.org/perlrequick....racter-classes ; look for the 'digit' word, then read http://perldoc.perl.org/perlrequick....ng-repetitions .
 
Old 06-04-2011, 04:54 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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 )
 
Old 06-05-2011, 10:21 AM   #12
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-06-2011, 04:41 AM   #13
rahul_dubeyin
Member
 
Registered: Apr 2011
Posts: 34

Original Poster
Rep: Reputation: 12
Hmm Thanks all of you.
 
Old 06-06-2011, 05:05 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Don't forget to show your solution to help others and then mark thread as SOLVED
 
Old 06-06-2011, 08:08 AM   #15
rahul_dubeyin
Member
 
Registered: Apr 2011
Posts: 34

Original Poster
Rep: Reputation: 12
Yes grail. Here I am.

You were correct about it, has to be the same way. The only difference is that I have to do it twice.

Code:
$opt_a =~ s/\(//;
$opt_a =~ s/\)//;
In first it will replace the "(" by null and in second command it will replace the ")" by null.
 
  


Reply



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
bash search for a pattern within a string variable nutthick Programming 8 03-17-2015 07:26 PM
extract a string within a string using a pattern adshocker Linux - Newbie 1 11-04-2010 10:44 PM
how to match for multiple pattern at the end of given string Santoshkb Programming 2 06-23-2008 10:42 AM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM
Perl : Changing a single char in a string richhill Programming 2 09-17-2003 04:31 PM

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

All times are GMT -5. The time now is 04:38 AM.

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
Open Source Consulting | Domain Registration