Multiple file matching in awk

head 1.txt

chr1	1   2   s1
chr1	3   4   s1
chr1	5   6   s1
chr1	20 11  s1
chr1	7   90  s1

head 2.txt

chr1	1   2   s2
chr1	3   4   s2
chr1	5   6   s2
chr1	20 11  s2
chr1	7   90  s2

Code I have used

awk ' NR==FNR{(a[$1]=$2) && (a[$2]=$3) && (a[$3]=$4);next} (a[$1]) && (a[$2]) && (a[$3]) {print $1"\t"$2"\t"$3"\t"$4"\t"a[$3]}' 2.txt 1.txt | head -5

Output

chr1	1   2   s1 s2
chr1	3   4   s1 s2
chr1	5   6   s1 s2
chr1	20 11  s1 s2
chr1	7   90  s1 s2

My question now is if I have another couple of files or more with the following data

head 3.txt

chr1	1   2   s3
chr1	3   4   s3
chr1	5   6   s3
chr1	20 11  s3
chr1	7   90  s3

head 4.txt

chr1	1   2   s4
chr1	3   4   s4
chr1	5   6   s4
chr1	20 11  s4
chr1	7   90  s4

How do I get the following output

chr1	1   2   s1 s2 s3 s4
chr1	3   4   s1 s2 s3 s4
chr1	5   6   s1 s2 s3 s4
chr1	20 11  s1 s2 s3 s4
chr1	7   90  s1 s2 s3 s4

Please keep in mind that my files are unsorted.

Thanks in advance.

How about this (output will be in same order as last file):

awk 'FNR==1{f++}
{key[FNR]=$1"\t"$2"\t"$3; val[f,key[FNR]]=$4}
END {
   for(i=1;i in key; i++) {
       printf "%s", key;
       for(c=1;c<=f;c++) printf "\t%s", val[c,key]
       printf "\n";
}}' *.txt

head 1.txt

chr1	1	2	s1
chr1	3	4	s1
chr1	20	11	s1
chr1	7	90	s1

head 2.txt

chr1	1	2	s2
chr1	3	4	s2
chr1	5	6	s2
chr1	20	11	s2
chr1	7	90	s2

head 3.txt

chr1	1	2	s3
chr1	5	6	s3
chr1	20	11	s3
chr1	7	90	s3

head 4.txt

chr1	1	2	s4
chr1	3	4	s4
chr1	5	6	s4
chr1	20	11	s4
awk 'FNR==1{f++} {key[FNR]=$1"\t"$2"\t"$3; val[f,key[FNR]]=$4} END { for(i=1;i in key; i++) {printf "%s", key; for(c=1;c<=f;c++) printf "\t%s", val[c,key]; printf "\n";}}' *.txt

chr1	1	2	s1	s2	s3	s4
chr1	3	4	s1	s2		s4
chr1	5	6		s2	s3	s4
chr1	20	11	s1	s2	s3	s4
chr1	7	90	s1	s2	s3

Thanks the code works fine.

But, if there are some records which are present only in two or three files and not in all the four or multiple files, the code generates the same output and leaves a blank space at records that doesn't contain any value. Please check my example data above.

Thanks in advance.

@Mods: Please excuse me for not including the code tags. Could someone tell me how to include code tags?

You didn't say what you wanted when a record was missing this version puts *NONE*

awk 'FNR==1{f++}
{key[FNR]=$1"\t"$2"\t"$3; val[f,key[FNR]]=$4}
END { 
   for(i=1;i in key; i++) {
       printf "%s", key;
       for(c=1;c<=f;c++) printf "\t%s", val[c,key]?val[c,key]:"*NONE*"
       printf "\n";
   }
}' *.txt

Sorry friend. When I work with things, I encounter certain things, so kind of wondering how to do it. Thanks for the update.

If I have more than 4 columns in each file and still the first three columns needs to be matched and the remaining columns needs to be printed, how do I do it?

1.txt

2.txt

3.txt

4.txt

Output.txt

Please note that my files are unsorted and not all files have common values. If file one has 100 records, the other one might have only 50 records but some recors will be common.

Thanks in advance.

how about this:

awk 'FNR==1{f++}
{
    key=$1"\t"$2"\t"$3;
    C[key]++;
    for(i=4;i<=NF;i++)
        val[f,key]=val[f,key]"\t"$i
}
END { 
   for(key in C) {
       if (C[key] == f) {
           printf "%s", key;
           for(c=1;c<=f;c++) printf "%s", val[c,key]
           printf "\n";
       }
   }
}' *.txt