If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > insert a file in a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-05-04, 13:49
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
insert a file in a file

I have a file1 with following lines :

aaa
bbb
ccc
ddd

Now I want to insert file2

111
222

after the line bbb from file1

that I have following effect in file1:

aaa
bbb
111
222
ccc
ddd


can anyone help me out by this problem ?

thanx a lot
__________________
Greetings from germany
Peter F.
Reply With Quote
  #2 (permalink)  
Old 02-05-04, 16:47
msuluhan msuluhan is offline
Registered User
 
Join Date: Jan 2004
Posts: 18
Re: insert a file in a file

Quote:
Originally posted by fla5do
I have a file1 with following lines :

aaa
bbb
ccc
ddd

Now I want to insert file2

111
222

after the line bbb from file1

that I have following effect in file1:

aaa
bbb
111
222
ccc
ddd


can anyone help me out by this problem ?

thanx a lot

Hi

head -n 2 file1 > file3
cat file2 >> file3
tail -n 2 file1>> file3

and then if the file3 is true that you want,
mv file3 file1

you should change number of lines parameter (I used 2) head and tail commands, but if you want more functionallity you have to supply more info
__________________
Murat SULUHAN
Live Free or Die
Reply With Quote
  #3 (permalink)  
Old 02-06-04, 04:54
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
An awk solutiion for you...

awk '{print}/bbb/{while (getline < "file1" > 0){print}}' file2

Damian
Reply With Quote
  #4 (permalink)  
Old 02-06-04, 13:52
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Thanxs for your interest to help me,

some more info !!
The position of pattern bbb can be in n th line

I have a file1 with following lines :

aaa
n th lines ....
.....
.....
bbb
ccc
ddd

@ Damian.
I think you are on the right way, but file2 is given out to screen
and not into file1
__________________
Greetings from germany
Peter F.
Reply With Quote
  #5 (permalink)  
Old 02-06-04, 14:26
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Damian just invert file1 and file2.
If you want to replace file1, redirect output to a file, for example :
Code:
awk '{print}/bbb/{while (getline < "file2" > 0){print}}' file1 > file1.tmp
mv file1.tmp file1
__________________
Jean-Pierre.
Reply With Quote
  #6 (permalink)  
Old 02-07-04, 08:59
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
I think you are on the right way, but file2 is given out to screen
and not into file1
Apologies Peter, I mixed up your filenames. I also took it as read that you'd know to redirect the output if you wanted it in a file!

By the way, what would you want to happen if your file contained more than one instance of bbb?

Damian
Reply With Quote
  #7 (permalink)  
Old 02-07-04, 11:54
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hi, thanx @ all,
the string surely exist only for one time in file1. I have to update the etc/profile on 7 hundred machines. After the line "umask 022"(bbb) I have to insert the lines from file2 . File2 contains the commands for a second password question after the normal login is done. There are a lot of other updates needed and I want to do as much as possible automatically by a script.

Thanx for helping me out.
With your help I get the solution for my problem.
__________________
Greetings from germany
Peter F.

Last edited by fla5do; 02-07-04 at 12:43.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On