有没有';这是一种使用bashshell逐行组合两个文件的方法

有没有';这是一种使用bashshell逐行组合两个文件的方法,bash,shell,Bash,Shell,请参见示例: 假设我的File1.txt有三行(a、b、c);文件2还有3行(1,2,3) 文件1 文件2 1 2 3 ... 我想得到一个文件3,如下所示: 文件3 a1 a2 a3 b1 b2 b3 c1 c2 c3 ... 非常感谢 假设bash 4.x: #!/usr/bin/env bash # ^^^^-- NOT /bin/sh readarray -t a <file1 # read each line of file1 into an

请参见示例: 假设我的File1.txt有三行(a、b、c);文件2还有3行(1,2,3)

文件1

文件2

1
2
3
...
我想得到一个文件3,如下所示:

文件3

a1
a2
a3
b1
b2
b3
c1
c2
c3
...
非常感谢

假设bash 4.x:

#!/usr/bin/env bash
#              ^^^^-- NOT /bin/sh

readarray -t a <file1   # read each line of file1 into an element of the array "a"
readarray -t b <file2   # read each line of file2 into an element of the array "b"
for itemA in "${a[@]}"; do
  for itemB in "${b[@]}"; do
    printf '%s%s\n' "$itemA" "$itemB"
  done
done
#/usr/bin/env bash
#^^^^--NOT/bin/sh

readarray-t a@CharlesDuffy
粘贴
不会执行此操作。请将两个文件的内容放入数组中。然后使用嵌套循环打印所有组合。@CharlesDuffy
paste
只输出
a1b2c3
,而不是所有组合。它看起来像是
join-j2file1file2
,即在不存在的字段上创建笛卡尔乘积。只需要删除输出中的空格。或者类似的。
#!/usr/bin/env bash
#              ^^^^-- NOT /bin/sh

readarray -t a <file1   # read each line of file1 into an element of the array "a"
readarray -t b <file2   # read each line of file2 into an element of the array "b"
for itemA in "${a[@]}"; do
  for itemB in "${b[@]}"; do
    printf '%s%s\n' "$itemA" "$itemB"
  done
done
IFS=$'\n' read -r -d '' -a a < <(cat -- file1 && printf '\0')
IFS=$'\n' read -r -d '' -a b < <(cat -- file2 && printf '\0')