Hello Endurance,

I hope you are doing well and you have become a Perl expert by now. ;-) I was looking for something here on PerlMonks (I use the search function quite a lot), and I stumbled upon this post by accident. For sake of future visitors and people who use to search for answers, I thought I would make a few hints.

When you're dealing with a relatively small file, then it's okay to read the entire file into memory. Next, you can split it so it occupies an array where each line is stored in an array element. I split it like this:

my @ARRAY = split(/[\r\n]+/, $ENTIRE_FILE_CONTENT);

Now, let's create a for loop that prints each line, so you can see how we can access each line in the array:

for (my $i = 0; $i < @ARRAY; $i++) { print "\n$ARRAY[$i];"; }

The current line is $ARRAY[$i], and to access the 3rd line from here, you would do that by writing $ARRAY[$i + 3]

The problem is if you reach the end of the array, then addressing the 3rd line from the last does not exist. So, you could get an error! To fix that, you could either check each time to make sure the array has enough elements:

for (my $i = 0; $i < @ARRAY; $i++) { if ($i + 3 < @ARRAY) { print "\n$ARRAY[$i + 3]"; } }

OR you could just set up the for loop so it skips the last 3 lines. That way you don't have to worry about checking each time:

my $STOP = @ARRAY - 3; for (my $i = 0; $i < $STOP; $i++) { print "\n$ARRAY[$i + 3]"; }

Now, when you want to search for a line, you should know whether you want to search for a text that occurs anywhere in the line OR if the requirement is that you have to find a line that matches EXACTLY what you are looking for.

To find whether a string occurs in the line, I would use the index() function:

my $FOUND = index($ARRAY[$i], 'solar winds'); if ($FOUND >= 0) { print "\nFOUND SOLAR WINDS IN LINE $i"; }

To see if a line precisely matches a certain string, use the eq operator:

if ($ARRAY[$i] eq 'solar winds') { print "\nLINE $i MATCHES SOLAR WIND +S"; }

So, let's put this together now!

my $STOP = @ARRAY - 3; for (my $i = 0; $i < $STOP; $i++) { if (index($ARRAY[$i], 'solar winds') >= 0) { $ARRAY[$i + 3] =~ s/country/place/; } # Use a regex to replace c +ountry... }

2023-04-03 Athanasius fixed code formatting.


In reply to Re: Edit lines from a file and replace multiple lines. by harangzsolt33
in thread Edit lines from a file and replace multiple lines. by Endurance

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.