Inserting file after specific line in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting file after specific line in another file
# 8  
Old 05-24-2012
Quote:
Originally Posted by ncwxpanther
That does not work. File1 is inserted after each

Code:
AA001 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1

So it looks like

Code:
AA001 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1
File 1
AA002 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1
File 1
AA003 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1
File 1

Any other suggestions?
What OS and version are you using?

I get:

Code:
$ sed '/AC003/rfile1' 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
AD004 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1


Last edited by Scrutinizer; 05-24-2012 at 03:05 PM..
# 9  
Old 05-24-2012
Quote:
Originally Posted by ligedasi
awk '/AC003/ {print;while (getline<"file1">0) print;next} {print}' file2

Again this works, but like the others it leaves in the first line of the set its looking for.

File 1
Code:
AU1150002001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AU1150002002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AU1150002003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AC0030002001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9    
AC0030002002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9    
AC0030002003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9

File 2
Code:
ER12100021001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
ER12100021002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9     
ER12100021003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9

Output
Code:
AU1150002001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AU1150002002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AU1150002003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AC0030002001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9    
ER1210001001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
 ER1210001002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9     
 ER1210001003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9 
AC0030002002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9    
 AC0030002003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9

# 10  
Old 05-24-2012
With your latest example:
Code:
$ sed '/AC003/rfile2' file1
AU1150002001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AU1150002002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AU1150002003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
AC0030002001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9    
ER12100021001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
ER12100021002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9     
ER12100021003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9
AC0030002002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9    
ER12100021001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
ER12100021002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9     
ER12100021003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9
AC0030002003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9
ER12100021001  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  
ER12100021002  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9     
ER12100021003  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9  -99.9

# 11  
Old 05-24-2012
Quote:
Originally Posted by Scrutinizer
What OS and version are you using?

I get:

Code:
$ sed '/AC003/rfile1' 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
AD004 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1


Sun Solaris 10
# 12  
Old 05-24-2012
Try a space between r and "file1":
Code:
sed '/AC003/r file1' file2

This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 05-25-2012
OK - you want it after each appearance of AC003?
Code:
awk '/AC003/ {print;while (getline<"file2">0) print; close("file2");next} {print}' file1

Scrutinizers genious line works also at my Solaris 8 WITH space
Code:
sed '/AC003/r file2' file1

Thanks ScrutinizerSmilie

Last edited by ligedasi; 05-25-2012 at 05:31 AM.. Reason: Please use code tags, thank you
This User Gave Thanks to ligedasi For This Post:
# 14  
Old 05-25-2012
Quote:
Originally Posted by ligedasi
OK - you want it after each appearance of AC003?
Code:
awk '/AC003/ {print;while (getline<"file2">0) print; close("file2");next} {print}' file1


This works perfect - thanks!

Last edited by Franklin52; 05-25-2012 at 09:31 AM.. Reason: Fixed tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

2. Shell Programming and Scripting

Inserting a line to a file

Hi, consider a file called mobile.txt as follows: For type lovers, add a new line at the end of it by copying its previous line and add a +1 to the field1, field2 Additionally, there are only 3 plans available to lovers type, so it should not work for lovers type already having 3 lines under... (11 Replies)
Discussion started by: Gautham
11 Replies

3. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

4. Shell Programming and Scripting

script for inserting line at specific place in file

I use zentyal for my server admin, which is great but zentyal auto-generates config file on boot and hence overwrites any changes made directly to config files. In order to allow multiple user access to a MS ACCESS database, I need to customise the smb.conf file and add the following line to the... (9 Replies)
Discussion started by: barrydocks
9 Replies

5. Shell Programming and Scripting

Inserting IP in one line of a file

Hi, I have this line: ip=111.222.133.144,mac=00:16:3E:2A:08:3C,vifname=veth360','ip=10.2.3.4,vifname=veth360a' ^ | ------- I want to insert this IP 144.133.222.111 between "144"... (4 Replies)
Discussion started by: iga3725
4 Replies

6. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

7. Shell Programming and Scripting

inserting line to a file

I have posted it previously but somehow could not delete the previous post.I felt i could not explain the problem statement well. Here t goes.I have a file say File1. Now i need a specific pattern from the lines to be added to the other line. File: red blue green ABC.txt@ABC END black... (1 Reply)
Discussion started by: ngupta
1 Replies

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

9. Shell Programming and Scripting

inserting a new line in a file

I'm sure you guys have answered this elsewhere but I can't seem to find where so here goes. #!/bin/bash n=120 a=$(sed '120q;d' energy.xvg) while ;do a=$(sed $n'q;d' energy.xvg) echo "$a \n" > newfile n=$(($n+100)) done exit 0 that script should read the file energy.xvg, start at... (1 Reply)
Discussion started by: gelitini
1 Replies

10. Shell Programming and Scripting

Inserting New Line in File using Sed ??

Dear All, I have a file called football where i have a list of 11 players each on different lines. I wish to add a name of another player on the first line. I have created a file called footballscript in vi writing the following sed command to achieve this ... cat football | sed -e '1 i\... (4 Replies)
Discussion started by: Mary_xxx
4 Replies
Login or Register to Ask a Question