Awk to print specific columns of lines with similar first column

Hi,
I have seen many posts on this and made progress but did not find a post that solves an issue I still have.
I have 2 files (actually more than 2 but I want to do them 2 by 2, however if you know how to do all of them at the same time that would be great) with 7 columns each. each file does not have the same number of lines so even sorting wouldn't work for the "join" unix command I believe.
So I tried the NR==FNR awk command

awk FNR==NR{a[$1];next}($1 in a){print}' OFS=\\t file1.tabular.txt file2.tabular.txt > outputfile.tabular.txt

this only gives me the complete line of one of the 2 files
then

awk FNR==NR{a[$1]=$3 ;next}($1 in a){print $1,$3,$7,a[$1]}' OFS=\\t file1.tabular.txt file2.tabular.txt > outputfile.tabular.txt

This is better, I get only the columns I want from one file but only one additional column from the second file.
I tried replacing with $0 for the first a[$1] but then I get the whole line, I don t know how to extract the 2 columns I want from it. Could you help me?
thanks a lot,
best regards

welcome to the community, @dartagnan32.
It always helps when the OP provides a minimal reproducible example with concise, testable sample input and expected output so we can help you.
Make your data sample to cover all/most expected "edge cases" and the expected output based on the sample input.

Please do so using markdown code tags.

Hi, thanks for your reply. I'll try to give an example with part of the 2 files I try to compare/join.
file1

A	384,8501167	-10,31883333	0,810273223	-12,73500473	3,78E-37	1,02E-34
B	32,37392665	-9,430933701	0,805473451	-11,70855934	1,15E-31	1,87E-29
C	231,2364012	-7,150907364	1,081647089	-6,61112801	3,81E-11	7,35E-10
D	582,6902317	-7,146286767	0,769708672	-9,284404636	1,63E-20	9,84E-19

then file 2

A	384,8501167	-5,680193603	0,862062234	-6,589076034	4,43E-11	5,20E-10
E	12,96139326	-8,012089098	1,269991444	-6,308774075	2,81E-10	2,91E-09
B	32,37392665	-5,252298161	0,858612472	-6,11719295	9,52E-10	8,89E-09

then joined file

A	-5,680193603	5,20E-10 -10,31883333	1,02E-34
B	-5,252298161	8,89E-09 -9,430933701	1,87E-29

knowing that there are about 2600 lines in common (I mean $1 in common) and about 4000 lines not in common in each file. So with my second attempt (second code from my first post), I managed to get close but lack the 3rd column of the output file.
of note the column 2 and 3 of output file can be also 4rth and 5th, I do not mind.
let me know if you need any more information.
thanks again

So it looks like you want to print $3 and $7 of each line that have $1 in common.
The following uses a $1-indexed array for storing $3 and $7 and another array for counting (for printing only two or more matches).

awk '
  {
    c[$1]++
    s[$1]=(s[$1] OFS  $3 OFS $7)
  }
  END {
    for (i in c) if (c[i]>1) print i, s[i]
  }
' file1 file2 file3

Not counting saves some memory:

awk '
  {
    s[$1]=(s[$1] OFS  $3 OFS $7)
  }
  END {
    for (i in s) print i, s[i]
  }
' file1 file2 file3

Thanks for your reply.
I tried both but I'm not entirely sure yet that it worked properly. I tried also adding

OFS '\t'

to your line of code and kept only file1 and file 2
however my output always looks like

Gm15517		3,611293643	0,037341989
	3,579037781	0,030845049
Rab20		3,871370296	0,001249517
	3,072203811	0,008426281

it looks like after $1 there is a space then a tab (or 2 tab) and after the first $3 and $7, there is a line break. Would you know how to change that?
I get the same with

awk -F '\t' 'FNR==NR{a[$1]=$3 OFS $7 ;next}($1 in a){print $1,$3,$7,a[$1]}' OFS='\t' file1.txt file2.txt > output_file.txt

I wonder if don't get a line break within my $7 which is the last field of the line is that possible?

also a bonus if you have time, could you explain where in ur code it checks if $1 is present in the 2 files?

thanks a lot for your help

My code works with many input files.
You are right, it prints two first OFS.
Because s[$1]=(s[$1] OFS $3 OFS $7) stores a leading OFS, and in the print the comma means another OFS.
A quick fix is
for (i in s) print (i s[i])

I have no explanation for the extra new lines.
Maybe your files are WinDOS format?

Hi thanks again.
Indeed I first made some sorting through excell and saved as tabular txt file but apparently that's not good. I should do everything using Terminal in Linux.
For those who run into such problems and don t have time to reprocess their files correctly,
you can start your code with

awk -v RS='\r\n'

it appears to work for me. I still have multiple empty lines at the end of the file that I will also remove.
I will re-do everything from my original files in terminal to be more clean.

thanks again for your help.
best

Each use of Excel is one too much :wink:
The following first awk code line converts WinDOS lines to Unix lines on the fly (effectively "eats" all):

{ sub("\r$", "") }