Inserting a file in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting a file in another file
# 1  
Old 06-21-2010
Data Inserting a file in another file

I wish to insert all the lines of b.txt in certain places(more than one place) of another file a.txt. Can anyone help? I'd prefer a short script(perl/shell)

Thanks
# 2  
Old 06-21-2010
Code:
awk '/PATTERN/  { getline < "b.txt" }' a.txt

didn't test

Last edited by Scott; 06-21-2010 at 08:08 AM.. Reason: Code tags, PLEASE!
# 3  
Old 06-21-2010
One way
Code:
# insert fileB after line 10  and line 20
otherfile=$(< fileA )
cnt=0
while read line
do
 cnt=$(( $cnt + 1 ))
 echo "$line"
 if [[ $cnt -eq 10  || $cnt -eq 20 ]]; then
   echo "$otherfile"
 fi

done  < fileA > tmpfile

mv tmpfile fileA

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 06-21-2010
If file1 contains the text to be inserted, before any line that contains the text "To"
Code:
awk '/To/{while(getline x<f)print x;close(f)}1' f=file1 file2

Code:
--------------
Text
to be inserted
--------------
To be or not to be– that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
--------------
Text
to be inserted
--------------
 And, by opposing, end them. To die, to sleep
 No more – and by a sleep to say we end
 The heartache and the thousand natural shocks
That flesh is heir to – ‘tis a consummation
--------------
Text
to be inserted
--------------
Devoutly to be wished. To die, to sleep
--------------
Text
to be inserted
--------------
To sleep, perchance to dream. Ay, there's the rub,
 For in that sleep of death what dreams may come,
When we have shuffled off this mortal coil,
 Must give us pause. There's the respect
That makes calamity of so long life.

Or to append to that every line that contains "To"
Code:
sed '/To/rfile1' file2

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-21-2010
Thanks fellas... Scrutinizers advise did work for me..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting IDs from a text file into a sequence alignment file

Hi, I have one file with one column and several hundred entries File1: NA1 NA2 NA3And now I need to run a command within a mapping aligner tool to insert these sample names into a sequence alignment file (SAM) such that they look like this @RG ID:Library1 SM:NA1 PL:Illumina ... (7 Replies)
Discussion started by: nans
7 Replies

2. Shell Programming and Scripting

Inserting Header to another file

Need your help in appending header(file1 contains header ) to my file2. I am using KSH AIX OS. I know how to do with taking temporary files. cat file1 >temp cat file2 >>temp mv temp file2 Is there way to append directly to a file in ksh. I don't find Sed -i option on my... (10 Replies)
Discussion started by: gvkumar25
10 Replies

3. Shell Programming and Scripting

Inserting data - from one file to another?

Hi Experts, I have a config file (file1) & a data file (file2) : - The file1 I want to modify : to replace "2nd fields c7? & m7? from the data from file2, -The below CPU line fields need to fill by file2's 1st colmns correspoding data. - The below MEM lines to be replaced by... (3 Replies)
Discussion started by: rveri
3 Replies

4. Shell Programming and Scripting

Inserting file after specific line in another file

Im attempting to insert the contents of File1 at a specific point of File2. File1 AD004 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 File2 AA001 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 AB002 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 AC003 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 Result AA001 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1... (13 Replies)
Discussion started by: ncwxpanther
13 Replies

5. UNIX for Dummies Questions & Answers

Best method for inserting a field from a file into another file?

Hello, Hoping someone can help! I have a file (fileA) with a potentially different number of | delimited fields the file looks like: fileA A|B|C|D|E|F|G| A|B|C|D|E|F|G| This file could have 100+ fields and have 2million+ records I have another file (fileB) which contains an 8 digit... (3 Replies)
Discussion started by: dendright
3 Replies

6. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

7. Shell Programming and Scripting

Inserting a column from one file into another big file

Hi I have two files, one is 1.6 GB. I would like to add one extra column of information to the large file at a specific location (after its 2nd column). For example: File 1 has two columns more than 1000 rows like this MM009987 1 File 2 looks like this MM00098 MM00076 3 4 2 4 2... (1 Reply)
Discussion started by: sogi
1 Replies

8. Shell Programming and Scripting

Inserting a file into another file above a specified location

Hi.I have two files say XXX.dat and YYY.dat The contents of XXX.dat and YYY.dat are as follows XXX.dat Jan Feb Mar Apr Method 1 { echo "This is method 1" } YYY.dat Delhi Culcutta One (4 Replies)
Discussion started by: Rajendra_1510
4 Replies

9. Shell Programming and Scripting

Inserting file into another file at a specified line number

Hi, I have two files XXX.dat and YYY.dat and their contents are as follows. The contents of XXX.dat(The line numbers are just for reference) 1) Mango 2) Banana 3) Papaya 4) Apple 5) Pomegranate The contents of YYY.dat 1) Custard apple 2) Grape 3) ... (4 Replies)
Discussion started by: Rajendra_1510
4 Replies

10. UNIX for Advanced & Expert Users

Inserting a new column in a file

Hey.. I'm writing a code to download some stuff from Informix database and put it on Xls. It works fine, but I have a problem fitting in a new requirement. I have currently a file which has information like below. f_name|Ronnie|Johnson|23.00| f_sal|Ronnie|Jhonson|4000.00|... (4 Replies)
Discussion started by: rosh0623
4 Replies
Login or Register to Ask a Question