我有两个长度相同且内容相同的列表:
file1 file2
apple apple
cat cat
dog dog
frog frog
我想编写一个shell脚本,它给出了如下输出:
output.txt
apple apple
apple cat
apple dog
apple frog
cat apple
cat cat
cat dog
cat frog
dog apple
... ...
任何人都可以帮我这个吗?
答案 0 :(得分:1)
你想要一个交叉产品,这是一个简单的方法
join -j9 file1 file2
答案 1 :(得分:0)
您的问题可能有很多问题,例如 - >你的行号需要相同的字符串顺序吗?仅根据您显示的示例Input_file(s),您可以尝试关注并告诉我这是否有帮助。
awk 'FNR==NR{a[FNR]=$0;v=FNR;next} {for(i=1;i<=v;i++){print $0,a[i]}}' Input_file1 Input_file2
输出如下。
apple apple
apple cat
apple dog
apple frog
cat apple
cat cat
cat dog
cat frog
dog apple
dog cat
dog dog
dog frog
frog apple
frog cat
frog dog
frog frog
此处也添加说明。
awk '
FNR==NR{ ##Checking here condition if FNR==NR, which will be only TRUE when first Input_file is getting read. FNR and NR both represents number of lines in a Input_file, only difference between them is FNR value will be RESET whenever it starts reading a next Input_file, on other hand NR value will be keep increasing till it reads all the Input_files.
a[FNR]=$0; ##Creating an array named a whose index current line number and value is current line too.
v=FNR; ##Creating a variable named V here whose value is value of FNR value.
next ##next is awk out of the box keyword which will skip all further statements.
}
{ ##This block statements will be executed when 2nd Input_file is being read.
for(i=1;i<=v;i++){ ##Starting a for loop, which will traverse through an array
print $0,a[i] ##Printing the value of current line along with value of array a whose index is value of variable named i.
}}
' Input_file1 Input_file2 ##Mentioning the Input_files here.