Operating System - HP-UX
1753823 Members
8757 Online
108805 Solutions
New Discussion юеВ

Re: Inserting text into a file after a particular line reading form another file

 
SOLVED
Go to solution
Nirupam
Occasional Contributor

Inserting text into a file after a particular line reading form another file

Hi All
I got some problem I have a file(Eg File1) which contains some text and after 12th line I want to insert one line from another file(Eg File2) can any body help me out how to write a script so as to satisify my cause

Thanks in advance
Nirupam
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Inserting text into a file after a particular line reading form another file

Hi:

You could do this:

# sed -n '1,12p' file1 > file.out
# sed -n '1p' file2 >> file.out
# sed -n '13,$p' file1 >> file.out

...JRF...
Ravi_8
Honored Contributor

Re: Inserting text into a file after a particular line reading form another file

hi,

open both files using vi (vi file1 file2)
in file1 at 12th line press 'o' which creates a blank line. switch over to file2(esc, :!2)
yank the line you wanted from file2 , again switch to file1 and put in blank line.
never give up
Fred Martin_1
Valued Contributor

Re: Inserting text into a file after a particular line reading form another file

If file2 is the file you want to insert into - you could do this:

awk '{if(NR<12){print}}' file2 > tmpfile
somehow print the one line >> tmpfile
awk '{if(NR>11){print}}' file2 >> tmpfile
cp tmpfile file2 ; rm tmpfile

The first line prints the first 11 lines of file 2. The second line, supplied by you, adds in the line from the other source. The third line adds the remainder of file 2. Line 4 replaces file 2 with the new version.

Note this could be made more efficient, if you have a very large file2. Awk could be told to exit when it has nothing else to do, rather than have it go through all the lines in the file regardless.

Fred
fmartin@applicatorssales.com
Robin Wakefield
Honored Contributor

Re: Inserting text into a file after a particular line reading form another file

or...

head -12 file1;cat file2;tail +13 file1

Robin