The easiest way to combine 2 files using bash and both of their keys appear as a result

I have 2 input files

file1

A   0.01
B   0.09
D   0.05
F   0.08

file2

A   0.03
C   0.01
D   0.04
E   0.09

The output I want

A   0.01    0.03
B   0.09    NULL
C   NULL    0.01
D   0.05    0.04
E   NULL    0.09
F   0.08    NULL

The best I can do is

join -t'    ' -a 1 -a 2 -1 1 -2 1 -o 1.1,1.2,2.2 file1 file2

which does not give me what I want

+2
source share
1 answer

You can write:

join -t $'\t' -a 1 -a 2 -1 1 -2 1 -e NULL -o 0,1.2,2.2 file1 file2

where i made these changes:

  • The output format I changed 1.1( "first column of the file # 1") to 0( "merge field"), so that the values from the file # 2 can be displayed in the first field, where necessary, (in particular, so Cand Ewill.)
  • I added a parameter -eto specify the value ( NULL) for missing / empty fields.
  • $'\t', Bash . , , . , , , , .: -)
+4

Source: https://habr.com/ru/post/1662506/


All Articles