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?

cat file1
1
2
3
4
cat file2
text1
text2
text3
text4

final_output

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

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

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

file2

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

output

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

Hello thomasraj87,

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

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

Thanks,
R. Singh

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?

2 Likes

Another awk approach:

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

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

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

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:

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

Hello thomasraj87,

As mentioned by RudiC previously, kindly put your requirements all together in a single post with proper sample Input_file and proper expected output_file.

For your current shown files in your previous post, could you please try following and let me know if this helps you.

awk 'FNR==NR{A[FNR]=$0;next} {print "<tr>" A[FNR] $5 "</td></tr>"}' Input_file1 FS="[><]"  Input_file2

Thanks,
R. Singh

1 Like

@OP: And please do this right from the beginning when you start the thread.

--
The example in post #6 should also work with the extra tr tags:

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

Scrutinizer,

Your code is perfectly working irrespective of number of <tr> tags in a line.

R. Singh,

Code given by you is inserting the <td> tags from 1st file to 2nd file. But it is misplacing the tags and and not printing the rest of the <tr> tags from 2nd file.

Anyhow Thanks all for helping me on this.

awk 'FNR==NR{A[FNR]=$0;next} {print "<tr>" A[FNR] $5 "</td></tr>"}' file1 FS="[><]" file2
<tr><td>1</td>text1</td></tr>
<tr><td>2</td>text2</td></tr>
<tr><td>3</td>text3</td></tr>
<tr><td>4</td>text4</td></tr>