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

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


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