Text Processing – Merging Columns from Two Separate Files

awkcolumnstext processing

How to create a new file merging selective columns from two separate files using awk? Without messing up the elements orders of BOTH files.

Exemple: File 3 may contain column 1,2,3 from File 1 and column 4 from File 2.

File 1
A   23  8   T
A   63  9   9
B   45  3   J

File 2
A   0
A   6   
B   5

File 3
A   23  8   0
A   63  9   6
B   45  3   5

Best Answer

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
Related Question