Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > How to use the Perl script to substitute the string ?

Reply
Thread Tools

How to use the Perl script to substitute the string ?

 
 
Richard Lee
Guest
Posts: n/a
 
      07-15-2007
Hi all,

How to use the Perl script to substitute the string as below ?

Before :-
"This is a test message on 20070715230010987654321 abcd123456"
change to
"This is a test message on 200707152300109876***** abc****456"

Please help ! Many Thanks.

Richard



 
Reply With Quote
 
 
 
 
shaimelz
Guest
Posts: n/a
 
      07-15-2007
substr EXPR,OFFSET,LENGTH,REPLACEMENT

$string = "This is a test message on 20070715230010987654321 abcd123456";

substr ($string, -7, 4, '****');
substr ($string, -18, 5, '*****');

print "$string \n";


"Richard Lee" <(E-Mail Removed)> wrote in message
news:469a3e26$1@127.0.0.1...
> Hi all,
>
> How to use the Perl script to substitute the string as below ?
>
> Before :-
> "This is a test message on 20070715230010987654321 abcd123456"
> change to
> "This is a test message on 200707152300109876***** abc****456"
>
> Please help ! Many Thanks.
>
> Richard
>
>
>

 
Reply With Quote
 
 
 
 
Richard Lee
Guest
Posts: n/a
 
      07-16-2007
Hi Shaimelz,

Many Thanks for your help ! Sorry, my question may be not clear. In a text
file, this string may be occurred many times and I need to replace it by "*"
for masking. So I need to locate this string first and replace part of the
characters to "*".

Sample of the Text file :-
This is a test message on 20070715230010987654321 abcd123456
This is 2nd line
This is 3rd line
This is a test message on 20070715230112112233445 qazw123456
This a 5th line
This is another type : This is a test message on 20070715230112112233445
qazw123456
This a 6th line

Result of the Text file:-
This is a test message on 200707152300109876***** abc****456
This is 2nd line
This is 3rd line
This is a test message on 200707152301121122***** qaz****456
This a 5th line
This is another type : This is a test message on 200707152301121122*****
qaz****456
This a 6th line

How to use the reg expression to locate "This is a test message on 200707"
and replace some of the character follow by this criteria ? Pls help ! Many
Thanks.

Richard

"shaimelz" <(E-Mail Removed)> 撰寫於郵件新聞:469a727e$0$13527$e4fe514c@dreader17 .news.xs4all.nl...
> substr EXPR,OFFSET,LENGTH,REPLACEMENT
>
> $string = "This is a test message on 20070715230010987654321
> abcd123456";
>
> substr ($string, -7, 4, '****');
> substr ($string, -18, 5, '*****');
>
> print "$string \n";
>
>
> "Richard Lee" <(E-Mail Removed)> wrote in message
> news:469a3e26$1@127.0.0.1...
>> Hi all,
>>
>> How to use the Perl script to substitute the string as below ?
>>
>> Before :-
>> "This is a test message on 20070715230010987654321 abcd123456"
>> change to
>> "This is a test message on 200707152300109876***** abc****456"
>>
>> Please help ! Many Thanks.
>>
>> Richard
>>
>>
>>



 
Reply With Quote
 
shaimelz
Guest
Posts: n/a
 
      07-16-2007
my $textfile = <<TEXTFILE;
your text here
TEXTFILE

my @textfile = ($textfile =~ m/^\s*(.+)/gm);

foreach (@textfile) {

if ($_ =~ /(^.+)(\d{23})(\s+)(\D{4}\d{6}$)/mx) {
my ($a, $b, $c, $d) = ($1, $2, $3, $4);
substr ($b, -5, 5, '*****');
substr ($d, -7, 4, '****');
print "$a$b$c$d\n";
} else {
print "$_\n";
}

}


"Richard Lee" <(E-Mail Removed)> wrote in message
news:469b865f$1@127.0.0.1...
> Hi Shaimelz,
>
> Many Thanks for your help ! Sorry, my question may be not clear. In a text
> file, this string may be occurred many times and I need to replace it by
> "*" for masking. So I need to locate this string first and replace part of
> the characters to "*".
>
> Sample of the Text file :-
> This is a test message on 20070715230010987654321 abcd123456
> This is 2nd line
> This is 3rd line
> This is a test message on 20070715230112112233445 qazw123456
> This a 5th line
> This is another type : This is a test message on 20070715230112112233445
> qazw123456
> This a 6th line
>
> Result of the Text file:-
> This is a test message on 200707152300109876***** abc****456
> This is 2nd line
> This is 3rd line
> This is a test message on 200707152301121122***** qaz****456
> This a 5th line
> This is another type : This is a test message on 200707152301121122*****
> qaz****456
> This a 6th line
>
> How to use the reg expression to locate "This is a test message on 200707"
> and replace some of the character follow by this criteria ? Pls help !
> Many Thanks.
>
> Richard
>
> "shaimelz" <(E-Mail Removed)>
> 撰寫於郵件新聞:469a727e$0$13527$e4fe514c@dreader17 .news.xs4all.nl...
>> substr EXPR,OFFSET,LENGTH,REPLACEMENT
>>
>> $string = "This is a test message on 20070715230010987654321 abcd123456";
>>
>> substr ($string, -7, 4, '****');
>> substr ($string, -18, 5, '*****');
>>
>> print "$string \n";
>>
>>
>> "Richard Lee" <(E-Mail Removed)> wrote in message
>> news:469a3e26$1@127.0.0.1...
>>> Hi all,
>>>
>>> How to use the Perl script to substitute the string as below ?
>>>
>>> Before :-
>>> "This is a test message on 20070715230010987654321 abcd123456"
>>> change to
>>> "This is a test message on 200707152300109876***** abc****456"
>>>
>>> Please help ! Many Thanks.
>>>
>>> Richard
>>>
>>>
>>>

>
>

 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      07-16-2007

First, comp.lang.perl has been officially dead for 12 years.
Please stop posting here.

>>>>> "Dshaimelz" == shaimelz <(E-Mail Removed)> writes:


Dshaimelz> if ($_ =~ /(^.+)(\d{23})(\s+)(\D{4}\d{6}$)/mx) {

Second, don't use "$_ =~ ...", since $_ is the default.

Third, don't use /x if it doesn't actually have an effect.

Dshaimelz> my ($a, $b, $c, $d) = ($1, $2, $3, $4);

Fourth, never use my $a or my $b, because it breaks sort subroutines.

Fifth, might as well have assigned this during the match:

if (my ($w, $x, $y, $z) = /(^.+)(\d{23})(\s+)(\D{4}\d{6}$)/m)


Sixth... you seem to be a junior Perl programmer. Please stop answering
questions. It makes it *harder* for experts to answer questions when junior
people try to help, and just make mistakes and mislead people.

Learn a bit more before answering.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-17-2007
Richard Lee wrote:
> Sorry, my question may be not clear. In a text file, this string


What string? You need to be more explicit. The examples you've shown
can be interpreted different ways; what _exactly_ are you looking for?

Before asking for help, first show us the code you've already written.

If you are serious about getting help on this, repost your question
to comp.lang.perl.misc instead of here. (This newsgroup is defunct.)

-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to use python regular expression to substitute string value Allerdyce.John@gmail.com Python 7 02-26-2006 09:23 PM
substitute perl script Shane NZ Computing 14 11-28-2005 08:07 AM
Help with substitute in perl. Eric.Medlin@gmail.com Perl Misc 3 05-27-2005 05:03 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments