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


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 2 Weeks Ago
thomasraj87 thomasraj87 is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 11 January 2017, 2:54 PM EST
Posts: 30
Thanks: 12
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; 2 Weeks Ago at 06:48 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 2 Weeks Ago
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 5 January 2017, 11:02 AM EST
Posts: 3,143
Thanks: 43
Thanked 692 Times in 668 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 (2 Weeks Ago)
Sponsored Links
    #3  
Old Unix and Linux 2 Weeks Ago
thomasraj87 thomasraj87 is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 11 January 2017, 2:54 PM EST
Posts: 30
Thanks: 12
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 2 Weeks Ago
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 23 January 2017, 12:38 PM EST
Location: Chennai
Posts: 2,357
Thanks: 517
Thanked 1,096 Times in 993 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 2 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 January 2017, 4:06 PM EST
Location: Aachen, Germany
Posts: 10,164
Thanks: 230
Thanked 3,075 Times in 2,851 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 (2 Weeks Ago), rbatte1 (2 Weeks Ago)
Sponsored Links
    #6  
Old Unix and Linux 2 Weeks Ago
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 January 2017, 4:12 PM EST
Location: Amsterdam
Posts: 11,201
Thanks: 458
Thanked 3,170 Times in 2,800 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 2 Weeks Ago
thomasraj87 thomasraj87 is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 11 January 2017, 2:54 PM EST
Posts: 30
Thanks: 12
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; 2 Weeks Ago at 11:06 AM..
Sponsored Links
Reply

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-05-2016 12:01 AM
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 12:02 PM
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 07:05 AM
Insert content of a file after a certain line in another file ktm Shell Programming and Scripting 4 08-31-2009 06:33 AM



All times are GMT -4. The time now is 09:00 PM.