1753505 Members
4743 Online
108794 Solutions
New Discussion юеВ

Re: Perl question

 
SOLVED
Go to solution
Allanm
Super Advisor

Perl question

perl -pi.back -e "s:(ap\d+) :ap1 \$1 :" filename

Can you explain in detail the above command, I understand the part abt ap\d+ but dont quite get the rest of colons and $1.

Thanks,
Allan.
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl question

HI Allan:

The colons are used as delimiters. As written, the whitespace is considered part of the characters to match (which seems odd).

The parenthesized part captures the match which is then available in $1 and used as a backreference in the substitution.

Thus:

# echo "I am ap3 "|perl -pe 's:(ap\d+) :ap1 $1 :'
I am ap1 ap3

Yet, if we don't have trailing whitespace there is no match:

# echo "I am ap3"|perl -pe 's:(ap\d+) :ap1 $1 :'
I am ap3

Unless we use the 'x' modified to write more readable expressions where whitespace can be used to spread out the pieces:

# echo "I am ap3"|perl -pe 's:(ap\d+) :ap1 $1 :'x
I am ap1 ap3

Regards!

...JRF...

Allanm
Super Advisor

Re: Perl question

awesome explaination and analysis.Is there something more than 10!! :)

Allan.