For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > need help for text substitution using Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author need help for text substitution using Perl
Jerry

2006-10-20, 6:56 pm

Hi,

Being a slow Perl learner who hasn't make much progress, I'm wondering
if some kind souls could help me achieve the following 2 text
substitution tasks detailed below.

********substitution 1************
The characteristic of original string:
1, always start with "$"
2, then followed by an integer, could be more than 1 digit, such as 23
3, always end with a period "."
4, there is always a blank before & after original string
For example: $2.

The characteristic of target string: always has "ebcdic" inserted into
the original string between "$" and the integer
For example: $ebcdic2.

So the substitution will look like this
$2. -> $ebcdic2.
$67. -> $ebcdic67.

Should the regular expression for original string be: \$\d+\. ?

********substitution 2************
The characteristic of original string:
1, always start with an integer, could be more than 1 digit, such as 23
2, then end with a period "."
3, there is always a blank before & after original string
For example: 2.

The characteristic of target string:
always has "s370ff" added to the beginning of original string
For example: s370ff2.

So the substitution will look like this

2. -> s370ff2.
14. -> s370ff14.

Should the regular expression for original string be: \d+\. ?

My real situation is that I have a bunch of files at one directory, of
which for the files whose name contained "readme", I need to do 2
substitutions described above.

Any input (and your time) is greatly appreciated.

Thanks,
Jerry

nobull67@gmail.com

2006-10-22, 7:57 am


Jerry wrote:
> Hi,
>
> Being a slow Perl learner who hasn't make much progress, I'm wondering
> if some kind souls could help me achieve the following 2 text
> substitution tasks detailed below.
>
> ********substitution 1************
> The characteristic of original string:
> 1, always start with "$"
> 2, then followed by an integer, could be more than 1 digit, such as 23
> 3, always end with a period "."
> 4, there is always a blank before & after original string


What is "a blank"? Do you mean a space character?

> For example: $2.
>
> The characteristic of target string: always has "ebcdic" inserted into
> the original string between "$" and the integer
> For example: $ebcdic2.
>
> So the substitution will look like this
> $2. -> $ebcdic2.
> $67. -> $ebcdic67.
>
> Should the regular expression for original string be: \$\d+\. ?


It is conventional when talking about regular expressions to enclose
them in // for readability. This is particularly relevant if the first
and/or last character of the regex is a space!

I shall assume you meant / \$\d+\. /

The answer if yes, but since you want to retain the number (which is
variable) you need some capturing e.g.: / \$(\d+)\. /

The complete substitution would be

s/ \$(\d+)\. / \$ebcdic$1. /g;

Most people would actually choose more capturing to avoid repetition in
the replacement.

s/( \$)(\d+\. )/$1ebcdic$2/g;

> ********substitution 2************


[ snip essentially the same question ]

Same answer.

> My real situation is that I have a bunch of files at one directory, of
> which for the files whose name contained "readme", I need to do 2
> substitutions described above.


perl -i -pe's/( \$)(\d+\. )/$1ebcdic$2/g;s/xxxx/xxxxx/' *readme*

(Where xxxx are the search and replace for your second substitution).

(Note: you OS's shell syntax may vary)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com