join - Comparison of columns from two files and create new column -
i want compare first 2 columns file1.txt
, file2.txt
, if match found add new columns (3rd , 4th) file1.txt
values found column 3 , 4 of file2.txt
, "na" non-match.
file1.txt
ch1 100 ch1 200 ch3 100 ch4 200
file2.txt
ch1 100 0.5 0.6 ch1 200 0.1 1.2 ch3 400 0.2 0.9 ch4 200 1.0 3.0
outputfile.txt
ch1 100 0.5 0.6 ch1 200 0.1 1.2 ch3 100 na na ch4 200 1.0 3.0
i tried join/awk commands not giving desired output.
the standard awk
technique reads whole of file1.txt
memory. if files big fit, considerably more effort required (but can done so).
awk 'fnr == nr { k[$1,$2] = 1; next } { if (k[$1,$2] == 1) { print $0; k[$1,$2] = 2 } } end { (i in k) { if (k[i] == 1) { sub(subsep, " ", i); print i, "na", "na" } } }' \ file1.txt file2.txt
the first line reads first file , records keys read. second line of processing. if key of $1, $2 in second file matches record, print $0
, , record key matched (by setting value 2 1). third line (the end block) looks @ keys in k
, if value not 2, not matched key printed 2 na columns. sub(subsep, " ", i)
part fixes sub-separator between 2 keys in i
space.
raw output awk
:
ch1 100 0.5 0.6 ch1 200 0.1 1.2 ch4 200 1.0 3.0 ch3 100 na na
after passing through column -t
(on mac):
ch1 100 0.5 0.6 ch1 200 0.1 1.2 ch4 200 1.0 3.0 ch3 100 na na
Comments
Post a Comment