Merging columns from two separate files

There's a dedicated tool for that: paste. It concatenates each full line from the first file with the corresponding line from the second file; you can remove unwanted columns before or after. For example, assuming that your columns are tab-delimited:

paste file1.txt file2.txt | cut -f 1,2,3,6

Here's a way to pre-filter both files that relies on ksh/bash/zsh process substitution.

paste <(<file1.txt sed 's/[[:space:]][[:space:]]*[^[:space:]]*$//') \
      <(<file1.txt sed 's/^[^[:space:]]*[[:space:]][[:space:]]*//')

Awk is primarily geared to processing one file at a time, but you can call getline to read from another file in parallel.

awk '
  BEGIN {file2=ARGV[2]; ARGV[2]="";}
  {$0 = $0 ORS getline(); print $1, $2, $3, $6;}
' file1.txt file2.txt

So far I've assumed that you want to match line 1 of file 1 with line 1 of file 2, line 2 of file 1 with line 2 of file 2, etc. If you want to match the contents of a column, that's a completely different matter. join will do the job provided that the column you want to match is sorted.


Try this:

$ awk 'FNR==NR{a[FNR]=$2;next};{$NF=a[FNR]};1' file2 file1
A 23 8 0
A 63 9 6
B 45 3 5