Merge and Append columns for more than 2 files

awkpastesedtext processing

I have "N" number of files with the contents shown in the image, and I want to merge and append the 5th column

The files are tab separated.

I have "N" number of files whose contents are as shown in the screenshot above. I want to merge them and append the 5th column. The first 4 columns are the same.

I tried with the awk command below for files 1 and 2:

awk 'NR==FNR{a[NR]=$0;next} {print a[FNR] "\t",$5}' file1 file2

it only appends for 2 files, not more than 2.

How can I do this correctly using awk, paste or other tools?

Best Answer

If the first four columns are identical across files you could run something like

set -- file*
fields="-f-5,$(seq -s, 10 5 $((5*$#)))"
paste "$@" | cut ${fields%?} >outfile

This will paste all files then extract fields 1-5 and every 5th field after.

Related Question