Search code examples
unixawkpastecut

adding columns from one file after a column in a second file while considering the two files have different number of columns


File 1

001 00A 892 J27
002 00G 742 M65
003 00B 934 B32
004 00J 876 K57
005 00k 543 N21

File 2 has 1,628,433 columns but, would like to add all four columns from file 1 after column one in this file.

a 2 T ..........
b 3 C ..........
c 4 G ..........
d 5 A ..........
e 6 B ..........

Desired output

a 001 00A 892 J27 2 T ..........
b 002 00G 742 M65 3 C ..........
c 003 00B 934 B32 4 G ..........
d 004 00J 876 K57 5 A ..........
e 005 00k 543 N21 6 B ..........

Tried the following

awk 'NR==FNR{a[FNR]=$1,$2,$3,$4} {print $1,a[FNR],$5}' file2 file1

Solution

  • awk -F'\t' -v OFS="\t" '{getline f1 < "file1"; $1 = $1 OFS f1; print}' file2