Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How to repeat a string multiple times if found match

by suvendra123
on Mar 09, 2021 at 02:07 UTC ( #11129359=perlquestion: print w/replies, xml ) Need Help??

suvendra123 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to repeat a string multiple times if found match
by GrandFather (Saint) on Mar 09, 2021 at 02:35 UTC

    Your mistake is not formatting your question so that we can read it.

    Your coding mistake is mixing reading and writing to the file at the same time. You read a line then write a line overwriting content in the file that followed the content you just read. This would be more obvious if you mixed up line lengths more.

    Usual practice is to create an output file to write the updated content to, then after the update is complete delete the old file and rename the new version to the name of the old version.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Usual practice is to create an output file to write the updated content to, then after the update is complete delete the old file and rename the new version to the name of the old version.

      suvendra123: You may be interested in my module File::Replace to help you with this.

        I don't want foo_ foo line only
        foo_1 foo_2 foo_3
Re: How to repeat a string multiple times if found match
by davido (Cardinal) on Mar 09, 2021 at 16:54 UTC

    Steps so that you can fix your post:

    1. Click on How to repeat a string multiple times if found match
    2. Edit by placing <p>...</p> tags around paragraphs, and <code>...</code> tags around code. Code tags work well for formatted data too. And your post has some that belongs in such tags.
    3. Click the "Update" button.

    It will take you five minutes, and your question will become legible. The editors can do it for you, but you should know how to do it yourself so that others don't have to take time fixing your lack of formatting.

    For more information, refer to Writeup Formatting Tips.


    Dave

Re: How to repeat a string multiple times if found match
by tybalt89 (Prior) on Mar 09, 2021 at 09:30 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11129359 use warnings; use Path::Tiny; my $path = path( 'test3.txt' ); $path->spew(<<END); hello world foo_ foo abc def END $path->edit_lines( sub { s/^(?=(foo_))/join '', map "$1$_\n", 1..3/e } +); print $path->slurp;

    Outputs:

    hello world foo_1 foo_2 foo_3 foo_ foo abc def
Re: How to repeat a string multiple times if found match
by BillKSmith (Prior) on Mar 10, 2021 at 03:54 UTC
    This one-liner even creates a back-up in case things go wrong!
    >perl -pibak -e"$_ = qq(foo_1\nfoo_2\nfoo_3\nfoo_4\n) if /^foo_$/" xx. +txt >dir xx.txt* Volume in drive C is Windows Volume Serial Number is AE03-D45F Directory of C:\Users\Bill\forums\monks 03/09/2021 10:47 PM 57 xx.txt 03/09/2021 07:04 PM 35 xx.txtbak 2 File(s) 92 bytes 0 Dir(s) 379,932,401,664 bytes free >type xx.txt hello world foo_1 foo_2 foo_3 foo_4 foo abc
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11129359]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2021-06-21 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (97 votes). Check out past polls.

    Notices?