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

Perl Gurus...

Please suggest me some idea to write particular statement after certain lines of regex match...

(1)first line. (2)second line. (3)third line. (4)fourth line. (5)fifth line.

when it finds regex "first", then output need to be like this...

(1)first line. (2)second line. yes, first word is found (3)third line. (4)fourth line. (5)fifth line.

Give me some example codes which may guide me to write the required code for my requirement??

Replies are listed 'Best First'.
Re: how to insert new line in text file after regex match?
by Corion (Patriarch) on May 21, 2015 at 10:54 UTC

    This is simple - in your loop reading the lines, you remember when you see a line containing first. Then you count and output two more lines, and then output the additional line.

    If you want to help us help you better, please show us the code you have already written and explain how it fails to do what you want it to do.

Re: how to insert new line in text file after regex match?
by aaron_baugher (Curate) on May 21, 2015 at 11:48 UTC

    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.

      An interesting approach, Aaron.

      Most folks here would make OP show the current code set, to ensure they understood the whole read file/write file process, and that all they were stuck on were particulars of the logic.

      You kind of took an interesting middle ground and fed him a complete-from-scratch working example, but he had to at least show enough cleverness to adapt it to his needs.

      Not sure it's the middle ground I would have chosen, but it is a creative way to not do absolutely everything for the student.

        That's what I was shooting for; didn't know how well I succeeded. We get a lot of questions from people who clearly should still be working through getting a "Hello world!" script working. I figured if he was one of them, my example would be no help to him; but if he had some experience and skill, he'd be okay. It's a hard balance to find, though, between giving a fish and teaching to fish.

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

      Yeah i modified it to my requirement and it is working for me guru....