http://qs321.pair.com?node_id=1126171

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

Hi all,

Good day to all...

I have two files as follows.

File1 is as follows.... architecture DEF_ARCH of fulladder_postsyn is component INBUF port( PAD : in std_logic := 'U'; Y : out std_logic ); end component; component OUTBUF port( D : in std_logic := 'U'; PAD : out std_logic ); end component; component VCC port( Y : out std_logic ); end component; component XOR3 port( A : in std_logic := 'U'; B : in std_logic := 'U'; C : in std_logic := 'U'; Y : out std_logic ); end component; component GND port( Y : out std_logic ); end component; component MAJ3 port( A : in std_logic := 'U'; B : in std_logic := 'U'; C : in std_logic := 'U'; Y : out std_logic ); end component; signal \GND\, \VCC\, N_5, a_c, b_c, c_c, sum_c, GND_0, VCC_0 : std_logic;
File2 is as follows.. component XOR2 port (A : in std_logic := 'U'; B : in std_logic := 'U'; Y : out std_logic); end component;

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....

Replies are listed 'Best First'.
Re: How to insert the content of a file into another file before/after a pattern match?
by CountZero (Bishop) on May 09, 2015 at 06:24 UTC
    Open both files for reading and open a new third file for writing.

    In a while-loop, read file1 line by line

    print each line to the third file.

    test if you have reached the "architecture DEF_ARCH of fulladder_postsyn is" line and if so, read the whole of file 2 and print this to the third file.

    Continue with reading and printing file1.

    Close all filehandles.

    unlink file1 and rename "file3" to "file1".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: How to insert the content of a file into another file before/after a pattern match?
by afoken (Chancellor) on May 09, 2015 at 06:42 UTC
    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:

    • 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.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How to insert the content of a file into another file before/after a pattern match?
by shmem (Chancellor) on May 09, 2015 at 18:41 UTC

    In File1 I see a pattern: the component blocks, architecture and signal declarations are separated by an empty line. Thus, the file is organized into paragraphs.

    There is a command line switch for reading files in paragraph mode: -00 (see perlrun). The -n and -p switches construct an implicit loop around your code which does the read for you.

    So, one way to achieve your goal (remember TIMTOWTDI - There Is More Than One Way To D It) would be something along the following:

    #!/usr/bin/perl -n00 use strict; use warnings; our $snippet; BEGIN { my $snippetfile = shift; # first file on command line open my $fh, '<', $snippetfile or die "Can't read '$snippetfile': $!\n"; $snippet = <$fh>; # get entire block into $snippet close $fh or die "Can't close filehandle of '$snippetfile' properl +y: $!\n"; } if (/architecture/s) { print; # print current block print $snippet; # print snippet from file 1 } elsif (/signal/s) { print $snippet; # print snippet first print; # then the current block } else { print; }

    Code is untested. Note that the second file containing the snippet comes first on the command line.
    See perlmod for information about the BEGIN block. See perlfunc and perlop for open, close, my, our, shift, print and // (or m//) along with its modifiers, see also perlre.
    Please read the docs. Please read the docs!

    Doing the same using the implicit print of the -p switch is left as an exercise to the reader.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'