如何在bash中重复行并粘贴不同的列?

qua*_*rky 3 bash awk file data-manipulation paste

在bash中有一个简短的方法可以根据需要重复文件的第一行,将其粘贴到kronecker产品类型中的另一个文件中(对于你的数学家)?我的意思是,我有一个文件A:

    a
    b
    c
Run Code Online (Sandbox Code Playgroud)

和文件B:

    x
    y
    z
Run Code Online (Sandbox Code Playgroud)

我想合并它们如下:

    a x
    a y 
    a z
    b x
    b y
    b z
    c x
    c y
    c z
Run Code Online (Sandbox Code Playgroud)

我可能会编写一个脚本,逐行读取文件并循环遍历它们,但我想知道是否有一个简短的单行命令可以执行相同的工作.我想不出一个,你可以看到,我也缺少一些搜索关键词.:-D

提前致谢.

anu*_*ava 5

你可以使用这个单行awk命令:

awk 'FNR==NR{a[++n]=$0; next} {for(i=1; i<=n; i++) print $0, a[i]}' file2 file1
a x
a y
a z
b x
b y
b z
c x
c y
c z
Run Code Online (Sandbox Code Playgroud)

分手:

NR == FNR {                  # While processing the first file in the list
  a[++n]=$0                  # store the row in array 'a' by the an incrementing index
  next                       # move to next record
}
{                            # while processing the second file 
  for(i=1; i<=n; i++)        # iterate over the array a
  print $0, a[i]             # print current row and array element
}
Run Code Online (Sandbox Code Playgroud)