Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Let's say I wanted to find the word 'banana' in a file, then add a line later, such that there were three lines between the 'banana' line and my new line. I could do this:

#!/usr/bin/env perl use 5.010; use warnings; use strict; while(<DATA>){ print; # print the line if(/banana/){ # if it contained banana print scalar <DATA> for 1..3; # print the next three lines print "This line was added.\n"; # then add the new line } } # then print the rest of the +lines __DATA__ a is for apple b is for banana c is for cantaloupe d is for dill e is for elderberry f is for fennel g is for grapefruit h is for honeydew

You should be able to adapt that for your needs. For production code, you'd want to add something to handle the case where there aren't three lines between 'banana' and the end of the file, but since this looks like a homework assignment, I'll assume that's not necessary. Another improvement would be to make the search word and the number of lines to skip arguments set with variables at the top of the problem or taken from the command line. You could also write it as a subroutine which takes the input filename/filehandle, search word, and number of lines as arguments.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

In reply to Re: how to insert new line in text file after regex match? by aaron_baugher
in thread how to insert new line in text file after regex match? by sumathigokul

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others lurking in the Monastery: (3)
    As of 2024-07-25 18:29 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.