使用 bash 合并两个文件的所有列

Combine all the columns of two files using bash

我有两个文件

A B C D E F
B D F A C E
D E F A B C

1 2 3 4 5 6
2 4 6 1 3 5
4 5 6 1 2 3

我想要这样的东西:

A1 B2 C3 D4 E5 F6
B2 D4 F6 A1 C3 E5
D4 E5 F6 A1 B2 C3

我的意思是,合并粘贴所有列内容的两个文件。

非常感谢!

这是一个 bash 解决方案:

paste -d' ' file1 file2 \
| while read -a fields ; do
      (( width=${#fields[@]}/2 ))
      for ((i=0; i<width; ++i)) ; do
          printf '%s%s ' "${fields[i]}" "${fields[ i + width ]}"
      done
      printf '\n'
done
  • paste 并排输出文件。
  • read -a 将列读入数组。
  • for循环中,我们遍历数组并打印相应的值。

能否请您尝试跟随,尝试在此处使用 xargs + paste 的组合来获得一些乐趣。

xargs -n6 < <(paste -d'[=10=]' <(xargs -n1 < Input_file1) <(xargs -n1 < Input_file2))