Join 3 files using first column with join (was awk)?

I have three similar files, they are all like this:

File a

ID1 Value1a
ID2 Value2a
  .
  .
  .
IDN Value2n

and I want this conclusion to look like

Output

ID1 Value1a Value1b Value1c
ID2 Value2a Value2b Value2c
.....
IDN ValueNa ValueNb ValueNc

Looking at the first line, I want the value 1A to be the value id1 in file A, value1B to the value id1 in file B, etc., in which each field and each line. I understand this as a sql connection. I tried several things, but not one of them, where even close.

EDIT: all files have the same length and identifiers.

+5
source share
4 answers

Give join (1) a try:

join fileA fileB | join - fileC
+8
source

. , , , (join) , . , , .


, :

$ cat file_a
    ID5 Value5a
    ID1 Value1a
    ID3 Value3a
    ID4 Value4a
    ID2 Value2a
$ cat file_b
    ID1 Value1b
    ID3 Value3b
$ cat file_c
    ID2 Value2c
    ID3 Value3c
    ID4 Value4c
    ID5 Value5c
$ cat qq.sh
    #!/bin/bash
    keylist=$(awk '{print $1'} file_[abc] | sort | uniq)
    for key in ${keylist} ; do
        val_a=$(grep "^${key} " file_a | awk '{print $2}') ; val_a=${val_a:--}
        val_b=$(grep "^${key} " file_b | awk '{print $2}') ; val_b=${val_b:--}
        val_c=$(grep "^${key} " file_c | awk '{print $2}') ; val_c=${val_c:--}
        echo ${key} ${val_a} ${val_b} ${val_c}
    done
$ ./qq.sh
    ID1 Value1a Value1b -
    ID2 Value2a - Value2c
    ID3 Value3a Value3b Value3c
    ID4 Value4a - Value4c
    ID5 Value5a - Value5c

, -, .

grep , ( 1 ), - . grep :

grep "^[ X]*${key}[ X]"

X tab, .

, awk, , , , , .

+2

join ( ) , , awk:

awk '{a=$0; getline b <"fileB"; getline c <"fileC"; $0=a" "b" "c; print $1,$2,$4,$6}' <fileA
+1

, , , . awk- . ( ).

awk 'END {
  for (K in k) print K, k[K]
    }
{ 
  k[$1] = k[$1] ? k[$1] FS $2 : $2 
  }' file1 file2 [.. filen]
0

All Articles