Reputation: 631
I've two files with 3 identical columns and 4th a different one.
File A
a b c 100
e f g 50
h i j 25
File B
a b c 200
e f g 20
h i j 15
How can files A
and B
be combined to look like file C
?
File C
a b c 100 200
e f g 50 20
h i j 25 15
--UPDATE--
I've used the solutions provided by Jotne an Kent but both of the script's output have . (dot) instead of comma. it looks like
a,b,c,100.200
e,f,g,50.20
Upvotes: 0
Views: 91
Reputation: 289535
If they had just a column in common, join
could make it. But let's use it and then parse the output:
$ join <(sort f1) <(sort f2)
a b c 100 b c 200
e f g 50 f g 20
h i j 25 i j 15
This joined based on the first column. Now, let's use cut
to get everything but columns 5 and 6:
$ join <(sort f1) <(sort f2) | cut -d' ' -f1-4,7
a b c 100 200
e f g 50 20
h i j 25 15
Note the usage of sort
to sort the files, because join
needs files to be sorted to work. With the sample data given it worked without sort
, but added for consistency.
Upvotes: 0
Reputation: 41456
Here is one awk
awk 'FNR==NR {a[$1,$2,$3]=$4;next} {print $0,a[$1,$2,$3]}' B A > C
cat C
a b c 100 200
e f g 50 20
h i j 25 15
Upvotes: 2