How to merge two or more fields from two different files where there is non matching column?

Hi,

Please excuse for often requesting queries and making R&D, I am trying to work out a possibility where i have two files field separated by pipe and another file containing only one field where there is no matching columns, Could you please advise how to merge two files.

$more top_list.txt_temp_1
PID|USER|PR|NI|VIRT|RES|SHR|S|%CPU|%MEM|TIME+|
2961|aaaa_a|15|0|100m|3196|2624|S|0.0|0.0|0:01.34|
11501|aaaa_a|15|0|100m|3192|2624|S|0.0|0.0|0:00.15|
12759|aaaa_a|16|0|100m|3188|2616|S|0.0|0.0|0:00.49|
21241|aaaa_a|16|0|100m|3200|2628|S|0.0|0.0|0:00.20|
30257|aaaa_a|16|0|100m|3200|2624|S|0.0|0.0|0:00.19|
$more top_list.txt_temp
COMMAND
ssh aaaa_a@aaaaaa
ssh aaaa_a@aaaaaa
ssh aaaa_a@aaaaaa
ssh aaaa_a@aaaaaa
ssh aaaa_a@aaaaaa

required merged file should be like this.

PID|USER|PR|NI|VIRT|RES|SHR|S|%CPU|%MEM|TIME+|COMMAND
2961|aaaa_a|15|0|100m|3196|2624|S|0.0|0.0|0:01.34|ssh aaaa_a@aaaaaa
11501|aaaa_a|15|0|100m|3192|2624|S|0.0|0.0|0:00.15|ssh aaaa_a@aaaaaa
12759|aaaa_a|16|0|100m|3188|2616|S|0.0|0.0|0:00.49|ssh aaaa_a@aaaaaa
21241|aaaa_a|16|0|100m|3200|2628|S|0.0|0.0|0:00.20|ssh aaaa_a@aaaaaa
30257|aaaa_a|16|0|100m|3200|2624|S|0.0|0.0|0:00.19|ssh aaaa_a@aaaaaa

I tried using awk command ,

awk 'FNR == NR { h[$[1]=1; next } !h[$1]'  top_list.txt_temp_1
  top_list.txt_temp

But it is not working and there is no matching column in my two files.

i am not familiar with join and merge using awk.

Please advsie how to merge the two files.

Thanks,
Regards,
karthikram

Hello,

with command paste:

paste -d'\0' top_list.txt_temp_1 top_list.txt_temp

Regards.

1 Like
awk 'FNR==NR{A[NR]=$0;next}{print A[FNR]""$0}' top_list.txt_temp_1 top_list.txt_temp
1 Like

Hi disedorgue/pamu,

Thanks both the commands are working good and as expected, very much Thanks for your valueable advise.

$awk 'FNR==NR{A[NR]=$0;next}{print A[FNR]""$0}' top_list.txt_temp_1 top_list.txt_temp
PID|USER|PR|NI|VIRT|RES|SHR|S|%CPU|%MEM|TIME+|COMMAND
2961|aaaa_a|15|0|100m|3196|2624|S|0.0|0.0|0:01.34|ssh aaaa_a@aaaaaa
11501|aaaa_a|15|0|100m|3192|2624|S|0.0|0.0|0:00.15|ssh aaaa_a@aaaaaa
12759|aaaa_a|16|0|100m|3188|2616|S|0.0|0.0|0:00.49|ssh aaaa_a@aaaaaa
21241|aaaa_a|16|0|100m|3200|2628|S|0.0|0.0|0:00.20|ssh aaaa_a@aaaaaa
30257|aaaa_a|16|0|100m|3200|2624|S|0.0|0.0|0:00.19|ssh aaaa_a@aaaaaa
$paste -d'\0' top_list.txt_temp_1 top_list.txt_temp
PID|USER|PR|NI|VIRT|RES|SHR|S|%CPU|%MEM|TIME+|COMMAND
2961|aaaa_a|15|0|100m|3196|2624|S|0.0|0.0|0:01.34|ssh aaaa_a@aaaaaa
11501|aaaa_a|15|0|100m|3192|2624|S|0.0|0.0|0:00.15|ssh aaaa_a@aaaaaa
12759|aaaa_a|16|0|100m|3188|2616|S|0.0|0.0|0:00.49|ssh aaaa_a@aaaaaa
21241|aaaa_a|16|0|100m|3200|2628|S|0.0|0.0|0:00.20|ssh aaaa_a@aaaaaa
30257|aaaa_a|16|0|100m|3200|2624|S|0.0|0.0|0:00.19|ssh aaaa_a@aaaaaa

Thanks,
Regards,
karthikram