Use Bash to merge column by column between two files

Viewed 98

Use Bash to merge column by column between two files

Hi all,

I have two files which i want to merge column by column in bash. I have had a good look but cannot find an answer. Below are example input files and a desired output.

file1.txt
A,S,G,S,G
K,A,G,A,G
K,S,A,S,A

file2.txt
K,S,B,S,G
K,S,G,D,G
K,S,G,S,E

expected-output.txt
AK,SS,GB,SS,GG
KK,AS,GG,AD,GG
KK,SS,AG,SS,AE

The sets of input files will have a variable number of fields and a variable number of lines (however these will always be the same within sets of input files)

Many Thanks!

1 Answers

More generic solution and with N number of fields in Input_files following may work.

awk '
BEGIN{
  FS=OFS=","
}
FNR==NR{
  for(i=1;i<=NF;i++){
    array[FNR,i]=$i
  }
  next
}
{
  for(i=1;i<=NF;i++){
    $i=array[FNR,i] $i
  }
}
1
' file1  file2


You could try following, for fun + written and tested with shown samples only, in case your real files are different then it may not work.

xargs -n5 < <(paste -d',' <(xargs -d',' -n1 < file1) <(xargs -d',' -n1 < file2)) |
awk '{gsub(/,/,"@");gsub(/ /,",");gsub(/@/,"")} 1' |
sed '$ d'
Related