Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to insert the content of a file into another file?

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-05-2017
thomasraj87 thomasraj87 is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 10 April 2017, 1:45 AM EDT
Posts: 37
Thanks: 13
Thanked 1 Time in 1 Post
How to insert the content of a file into another file?

I have the two files. And i want the each line of first file to be inserted in the beginning of second file each line and the final output should be like this. Could anyone help me on this?

Code:
cat file1
1
2
3
4


Code:
cat file2
text1
text2
text3
text4

final_output

Code:
1 text1
2 text2
3 text3
4 text4


Last edited by Scrutinizer; 01-05-2017 at 05:48 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 01-05-2017
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 14 September 2017, 8:59 PM EDT
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts

Code:
paste file1 file2


Code:
$ awk 'NR==FNR{a[NR]=$0;next}{print a[FNR],$0}' file1 file2
1 text1
2 text2
3 text3
4 text4

The Following User Says Thank You to itkamaraj For This Useful Post:
thomasraj87 (01-05-2017)
Sponsored Links
    #3  
Old Unix and Linux 01-05-2017
thomasraj87 thomasraj87 is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 10 April 2017, 1:45 AM EDT
Posts: 37
Thanks: 13
Thanked 1 Time in 1 Post
Thanks for the solution given. Its works fine. But my exact requirement is, i am having one <td> data in one file and i want to inset that into another file within the <tr> tag. Could you please help me on this.

file1

Code:
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>

file2

Code:
<tr><td>text1</td></tr>
<tr><td>text2</td></tr>
<tr><td>text3</td></tr>
<tr><td>text4</td></tr>

output

Code:
<tr><td>1</td><td>text1</td></tr>
<tr><td>2</td><td>text2</td></tr>
<tr><td>3</td><td>text3</td></tr>
<tr><td>4</td><td>text4</td></tr>

    #4  
Old Unix and Linux 01-05-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 18 October 2017, 5:03 AM EDT
Location: Chennai
Posts: 2,630
Thanks: 578
Thanked 1,251 Times in 1,126 Posts
Hello thomasraj87,

Could you please try following, not tested with many permutations and combinations though.

Code:
awk 'FNR==NR{A[FNR]=$0;next} {gsub(/<tr>|<\/tr>/,"")} {print "<tr>" A[FNR] $0 "</tr>"}'   Input_file

Thanks,
R. Singh
Sponsored Links
    #5  
Old Unix and Linux 01-05-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 October 2017, 5:11 AM EDT
Location: Aachen, Germany
Posts: 11,470
Thanks: 309
Thanked 3,555 Times in 3,272 Posts
With the fine working examples by itkamaraj, how would YOU tackle the NEW problem? BTW, wouldn't it have been nice to post the real samples in the first place, saving your and our time?
The Following 2 Users Say Thank You to RudiC For This Useful Post:
drl (01-05-2017), rbatte1 (01-05-2017)
Sponsored Links
    #6  
Old Unix and Linux 01-05-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 October 2017, 11:38 PM EDT
Location: Amsterdam
Posts: 11,567
Thanks: 508
Thanked 3,350 Times in 2,956 Posts
Another awk approach:

Code:
awk '{p=$0} getline<f>0{$1=$1 p}1' FS='<td>' OFS='<td>' f=file2 file1

Sponsored Links
    #7  
Old Unix and Linux 01-05-2017
thomasraj87 thomasraj87 is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 10 April 2017, 1:45 AM EDT
Posts: 37
Thanks: 13
Thanked 1 Time in 1 Post
Dear R. Singh,
It works well and getting the desired output. In some very rare case, my sql is giving more than one <tr> tag as output in the same line as given below.

For ex: file2

Code:
<tr><td>text1</td></tr>
<tr><td>text2</td></tr><tr><td>txt2</td></tr>
<tr><td>text3</td></tr><tr><td>txt3</td></tr><tr><td>txt4</td></tr>
<tr><td>text4</td></tr>

In this case if i use this awk command, some <tr> tags are missing as given below.

Code:
awk 'FNR==NR{A[FNR]=$0;next} {gsub(/<tr>|<\/tr>/,"")} {print "<tr>" A[FNR] $0 "</tr>"}'   file1 file2
<tr><td>1</td><td>text1</td></tr>
<tr><td>2</td><td>text2</td><td>txt2</td></tr>
<tr><td>3</td><td>text3</td><td>txt3</td><td>txt4</td></tr>
<tr><td>4</td><td>text4</td></tr>

In this case, how can i insert my <td> tag from file1 into file2 as first <td> tag independent of how many <tr> tags are present in same line in file2.

Desired output:

Code:
<tr><td>1</td><td>text1</td></tr>
<tr><td>2</td><td>text2</td></tr><tr><td>txt2</td></tr>
<tr><td>3</td><td>text3</td></tr><tr><td>txt3</td></tr><tr><td>txt4</td></tr>
<tr><td>4</td><td>text4</td></tr>


Last edited by thomasraj87; 01-05-2017 at 10:06 AM..
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert content of a file right after pattern in another file SkySmart Shell Programming and Scripting 1 11-04-2016 11:01 PM
Insert content of file before the first occurrence of a line starts with a pattern in another file Jagadeesh Kumar Shell Programming and Scripting 14 10-05-2016 11:02 AM
Insert content of a file to another file at a line number which is given by third file Jagadeesh Kumar Shell Programming and Scripting 4 09-29-2016 06:05 AM
Insert content of a file after a certain line in another file ktm Shell Programming and Scripting 4 08-31-2009 05:33 AM



All times are GMT -4. The time now is 05:11 AM.