简体   繁体   中英

Combine all the columns of two files using bash

I have two files

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

and

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

I want to have something like this:

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

I mean, combine both files pasting the content of all columns.

Thank you very much!

Here's a bash solution:

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 outputs the files side by side.
  • read -a reads the columns into an array.
  • in the for loop, we iterate over the array and print the corresponding values.

您能否尝试以下操作,尝试在此处使用xargs + paste组合来获得一些乐趣。

xargs -n6 < <(paste -d'\0' <(xargs -n1 < Input_file1) <(xargs -n1 < Input_file2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM