http://www.perlmonks.org?node_id=1126175


in reply to How to insert the content of a file into another file before/after a pattern match?

I want to insert file2 in file1 either after architecture line or before signal line... i.e. i want to insert this component in this component declaration part....Give me some idea to do this monks....

Easy:

Or did you expect us to write code for a general solution?

OK, how about this:

Enable strict checks and warnings. Load a module that automatically handles I/O errors. Open file handle IN1 for reading from "file1". Open file handle OUT for writing to "result". While reading a line from IN1 does not indicate end-of-file: Write the line just read to OUT. If the line just read contains "architecture": Open file handle IN2 for reading from "file2". While reading a line from IN2 does not indicate end-of-file: Write the line just read to OUT. Close file handle IN2. Close file handle OUT. Close file handle IN1.

Hint for the first line: use strict; use warnings;. Hint for the error handling module: autodie. Everything else is in perlfunc.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)