cancel
Showing results for 
Search instead for 
Did you mean: 

perl - substitution

SOLVED
Go to solution
Balaji N
Honored Contributor

perl - substitution

this is not an hp ux question, but i know perl gurus including the Pharaoh of perl is here. so i will post.

i am writing a perl script. and this does something like below.

$_=@tmporaCommand[$size-1];
#$_ contains something like `ora_pmon_(A1|A2|A3)` or 'ora_pmon_A1'
s/'ora_pmon_\(//;
s/\(//;
s/\)//;
s/'//;
@oraInstance = split(/\|/);
#want the array elements to be like A1, A2, A3


but there should be something like

sed -e 's/'ora_pmon_\(//' -e 's/\(//;' ...

here as well! right?

any pointer?
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
7 REPLIES
Balaji N
Honored Contributor

Re: perl - substitution

ping...

Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Ramkumar Devanathan
Honored Contributor

Re: perl - substitution

Balaji,

In fact perl supports a -e command line option through which you can avoid writing a multi line program, although at the cost of readability, probably...

try this -

perl -h

Just learning perl.. so can't give any examples... avlovu nalla theriyaadu...

HTH.
- ramd.
HPE Software Rocks!
Balaji N
Honored Contributor

Re: perl - substitution

thanks ram. i am aware of that.

this a big program and what is shown above is snippet of the code.

-balaji

Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Tom Maloy
Respected Contributor
Solution

Re: perl - substitution

Sounds like you want to combine all of the substitutions into one line. Try this:

s/ora_pmon_[(]?([^)]*)[)]?/\1/

The square brackets and ? indicate an optional pattern (the parens). Putting parens around a pattern allows you to reference it later as \1.
Carpe diem!
Balaji N
Honored Contributor

Re: perl - substitution

thanks tom. it works perfectly. but dont understand a bit of it.


if possible can u explain this better or point me to a resource.

tia
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Tom Maloy
Respected Contributor

Re: perl - substitution

Almost any of the O'Reilly books on Perl would be a good choice, and other publishers also put out good books.

What you want to take a close look at is regular expressions (regexps). Very powerful. Once you start playing with them, they will make your life easier. Just watch out for "greedy" behavior - by default, a pattern will match as much text as possible.

Let's look at the pattern I suggested:

s/ora_pmon_[(]?([^)]*)[)]?/\1/

The basic structure is

s/patternToMatch/replacementText/flags

We'll ignore flags, since we did not use them.

The patternToMatch is

ora_pmon_[(]?([^)]*)[)]?

The first part (ora_pmon_) is *literal*. That is, we need an exact match.

Anything inside square brackets is a character set. So [A-Z] is a range that would match ONE upper case letter. After the square brackets, there can be modifiers. A ? matches one optional character. A * matches one or more characters, and it will match ALL that it can - this is "greedy". So [(]? will match ONE left parenthesis IF it is present. If there is no left parenthesis, it will not match anything, but since that match was optional, this is not a failure. Likewise, [)]? is an optional match for a right parenthesis.

We can also negate patterns, or "not match", with the ^ at the start of a character set. So the [^)]* matches EVERYTHING until it hits a right paren. So this should match A1|A2|A3 or A1.

By putting a pattern inside parentheses, we tell perl to remember the resulting match, and make that available in the replacementText. The result from the first set of parens is available as \1, the second as \2, and so on.

So with input of "ora_pmon_A1", we match "ora_pmon_" with the literal characters. There is no left paren to (optionally) match. We match "A1" with the pattern in parens, so "A1" is now \1. There is no right paren to match. In the replacementText, we put in \1, which results in "A1" as output (replacement for $_).

With input of "ora_pmon_(A1|A2|A3}", we match "ora_pmon_" with the literal characters. We match the left paren. We match "A1|A2|A3" with the pattern in parens, so "A1!A2|A3" is now \1. We match the right paren. In the replacementText, we put in \1, which results in "A1" as output (replacement for $_).

HTH.

Tom
Carpe diem!
Balaji N
Honored Contributor

Re: perl - substitution

thank you very very very .... much tom.

though my head is still spinning, i got most of what you said.

now i need to do my home work.

-balaji

ps: am astonished by your patience in spending so much time to write a detailed reply. thanks a lot.
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.