简体   繁体   English

将File1与File2合并(继续从File1追加到File2,直到不再有行)

[英]Merge File1 with File2 (keep appending from File1 to File2 until no more rows)

I can't find a solution. 我找不到解决方案。 So here is the problem. 所以这就是问题所在。 Result should be 100 rows (File1) with contents from File2 repeating 25 times. 结果应为100行(File1),其中来自File2的内容重复25次。 What I want is to join the contents even though the number of rows is not equal. 我想要的是即使行数不相等也要加入内容。 Keep repeating including lines from File2 until number of rows from File1 is met. 继续重复包括File2中的行,直到满足File1中的行数。

File1: 文件1:

test1@domain.com
test2@domain2.com
test3@domain3.com
test4@domain4.com

File2: 文件2:

A1,B11
A2,B22
A3,B33
A4,B44

What I want is to combine the files in the following to have the following expected result: 我想要的是组合以下文件以获得以下预期结果:

File3: 文件3:

test1@domain.com,A1,B12
test2@domain2.com,A2,B22
test3@domain3.com,A3,B33
test4@domain4.com,A4,B44

Note here: After it finishes with the 4 rows from File2, start again from first line, then repeat. 注意:从File2完成4行后,从第一行重新开始,然后重复。

test5@domain5.com,A1,B12
test6@domain6.com,A2,B22
test7@domain7.com,A3,B33
test8@domain8.com,A4,B44

The example in your question isn't clear but I THINK this is what you're trying to do: 您问题中的示例不明确,但我认为这是您要做的事情:

$ awk -v OFS=',' 'NR==FNR{a[++n]=$0;next} {print $0, a[(FNR-1)%n+1]}' file2 file1
test1@domain.com,A1,B11
test2@domain2.com,A2,B22
test3@domain3.com,A3,B33
test4@domain4.com,A4,B44
test5@domain5.com,A1,B11
test6@domain6.com,A2,B22

The above was run against this input: 以上是针对此输入运行的:

$ cat file1
test1@domain.com
test2@domain2.com
test3@domain3.com
test4@domain4.com
test5@domain5.com
test6@domain6.com
$
$ cat file2
A1,B11
A2,B22
A3,B33
A4,B44

Could you please try following. 你可以尝试一下吗?

awk '
BEGIN{
  OFS=","
}
FNR==NR{
  a[++count]=$0
  next
}
{
  count_curr++
  count_curr=count_curr>count?1:count_curr
  print a[count_curr],$0
}
'  Input_file2  Input_file1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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