Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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....


  • Assuming Windows: Start the build-in editor (notepad.exe). Open file1. Position the cursor after the architecture line or before the signal line. Start another instance of the editor. Open file2. Press Ctrl-A to mark the entire file. Press Ctrl-C to copy it to the clipboard. Switch back to the first editor instance. Press Ctrl-V to paste from the clipboard. Save the file.
  • Assuming Unix/Linux with joe: Enter joe file1 file2 at the command prompt. Position the cursor after the architexture line or before the signal line. Press Ctrl-K, then N to switch to file2. Press Ctrl-K, then B to mark the start of a block. Press Ctrl-K, then Ctrl-V to move to the end of file2. Press Ctrl-K, then K to mark the end of a block. Press Ctrl-K, then P to switch back to file1. Press Ctrl-K, then C to copy the block into file1. Press Ctrl-K, then D to save file1. Press Ctrl-C to leave joe.

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.


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

In reply to Re: How to insert the content of a file into another file before/after a pattern match? by afoken
in thread How to insert the content of a file into another file before/after a pattern 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
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (8)
    As of 2021-04-14 11:06 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found