My data files are exactly similar to what I have shown in the initial file. I have just installed strawberry perl so don't know how to code. I want to perform find and replace in text file using perl regular expression. I am fan of PCRE and have some knowledge. While asking for command line alternative to notepad++, I was suggested, perl is good choice. I tried for following file

1

2

3

4

dog

1

2

3

4

5

puppy

7

8

I want to replace dog with puppy. Sometimes I want preserve lines between them too sometimes and wan't to remove the lines other times.

While using this one liner

perl -i.bak -pe "BEGIN{undef $/;} s/(dog)((.*[\r\n]+){6})(.*)/$1/smg" 4.txt it gives

1

2

3

4

dog

while notepad pcre gives desired result wile using find (dog)((.*[\r\n]+){6})(.*) and replace with \1

1

2

3

4

7

dog

8

like wise when I replace it with \2 it gave all the lines without dog and puppy. and while \3 gave me puppy in place of dog in above mentioned result. all behaves perfectly.

So the main thing that I want is to make grouping as follow. group 1= dog, Group 2 = 6 lines after found string i.e.dog. group 3 = whatever present on line 7. I want to be able to put back the groups as required.

Questions un answered

How to use grouping in perl while using regex that matches new line? Am I using rightly grouping in perl in my above code?

How to specify substitution? does substitution formula is correct as a whole in my code?

How to specified remembered groups? Am I making any mistake in substitution position while specifying group like $1, $2, $3 or sometimes $& (what is found)

I don't have sufficient knowledge to understand what you have suggested so I tried

perl -i.bak -pe "BEGIN{undef $/;} s/(dog)(.*([^\n]*\r?\n){6})(.*)/$1/smg" 4.txt

gave same results as with earlier oneliner


In reply to Re^4: Regex find and replace involving new line by PRA007
in thread Regex find and replace involving new line by PRA007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.