How to use the Perl script to substitute the string ?

Discussion in 'Perl' started by Richard Lee, Jul 15, 2007.

  1. Richard Lee

    Richard Lee Guest

    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
    Richard Lee, Jul 15, 2007
    #1
    1. Advertising

  2. Richard Lee

    shaimelz Guest

    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" <> 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
    >
    >
    >
    shaimelz, Jul 15, 2007
    #2
    1. Advertising

  3. Richard Lee

    Richard Lee Guest

    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" <> ¼¶¼g©ó¶l¥ó·s»D:469a727e$0$13527$4all.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" <> 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
    >>
    >>
    >>
    Richard Lee, Jul 16, 2007
    #3
  4. Richard Lee

    shaimelz Guest

    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" <> 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" <>
    > ¼¶¼g©ó¶l¥ó·s»D:469a727e$0$13527$4all.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" <> 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
    >>>
    >>>
    >>>

    >
    >
    shaimelz, Jul 16, 2007
    #4
  5. First, comp.lang.perl has been officially dead for 12 years.
    Please stop posting here.

    >>>>> "Dshaimelz" == shaimelz <> 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
    <> <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
    Randal L. Schwartz, Jul 16, 2007
    #5
  6. Richard Lee

    Joe Smith Guest

    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
    Joe Smith, Jul 17, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,769
  2. Replies:
    7
    Views:
    766
    Pasi Savolainen
    Feb 26, 2006
  3. Replies:
    3
    Views:
    151
    Joe Smith
    May 27, 2005
  4. kevin kitenik
    Replies:
    9
    Views:
    190
    Uri Guttman
    Dec 18, 2008
  5. Luc Leemans
    Replies:
    5
    Views:
    143
    C.DeRykus
    Jun 21, 2009
Loading...

Share This Page