请看以下示例:
假设我有一个名为File1.txt的文件,其中有三行内容(a,b,c);File2也有3行(1,2,3)。
文件1
我想要获取一个像以下这样的文件:
文件3
许多感谢!
文件1
a
b
c
文件 2
1
2
3
...
我想要获取一个像以下这样的文件:
文件3
a1
a2
a3
b1
b2
b3
c1
c2
c3
...
许多感谢!
#!/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
readarray -t a <file1
和readarray -t b <file2
:IFS=$'\n' read -r -d '' -a a < <(cat -- file1 && printf '\0')
IFS=$'\n' read -r -d '' -a b < <(cat -- file2 && printf '\0')
paste
命令不会做到这一点。 - Barmarjoin
,只是没有任何要连接的内容。我不认为有任何可以产生完整交叉乘积的东西。 - Barmarpaste
只会输出a1 b2 c3
,而不是所有的组合。 - Barmarjoin -j 2 file1 file2
,即在不存在的字段上创建笛卡尔积。只需要从输出中删除空格即可。 - Benjamin W.