简体   繁体   中英

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. 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.

File1:

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

File2:

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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