简体   繁体   English

从一个 CSV 复制 n 行并将它们粘贴到另一个 CSV 中 n 次

[英]Copy n lines from one CSV and paste them n times in another CSV

I'm having a bit of a problem trying to figure this one out.我在尝试解决这个问题时遇到了一些问题。

I have two CSV files, one of them without headers and the other with headers.我有两个 CSV 文件,一个没有标题,另一个有标题。

file1.csv (no headers):
apples
bananas

file2.csv (with headers):
HEADER1,HEADER2
item1,item3
item1,item3
item1,item3
item2,item3
item2,item3
item2,item3

I would like to create a new file: "file3.csv" where "apples" and "bananas" are listed the same number of times as "item1" and "item2" respectively.我想创建一个新文件:“file3.csv”,其中“apples”和“bananas”的列出次数分别与“item1”和“item2”相同。

The resulting file3.csv should look like this:生成的 file3.csv 应如下所示:

file3.csv
HEADER1,HEADER2,HEADER3
item1,item3,apples
item1,item3,apples
item1,item3,apples
item2,item3,bananas
item2,item3,bananas
item2,item3,bananas

To add the new header I'm using echo "$(head -n 1 file2.csv),HEADER3 >file3.csv .要添加新标题,我正在使用echo "$(head -n 1 file2.csv),HEADER3 >file3.csv

To copy and paste the lines I've tried to do it with: awk -F, -v OFS=, 'NR==FNR {a[$1]=$1; next} {print $0,a[$1]}' file1.csv file2.csv要复制和粘贴我尝试使用的行: awk -F, -v OFS=, 'NR==FNR {a[$1]=$1; next} {print $0,a[$1]}' file1.csv file2.csv awk -F, -v OFS=, 'NR==FNR {a[$1]=$1; next} {print $0,a[$1]}' file1.csv file2.csv but it doesn't work as I intend. awk -F, -v OFS=, 'NR==FNR {a[$1]=$1; next} {print $0,a[$1]}' file1.csv file2.csv但它没有按我的意图工作。

If an awk expert could help me a little, I would be eternally thankful.如果 awk 专家能帮我一点忙,我将永远感激不尽。

Thanks!谢谢!

golfed version高尔夫球版

$ awk -F, -v OFS=, 'NR==FNR{a[NR+1]=$1;a[1]="HEADER3";next} $3=a[b[$1]=c+=!b[$1]]' file{1,2}.csv

HEADER1,HEADER2,HEADER3
item1,item3,apples
item1,item3,apples
item1,item3,apples
item2,item3,bananas
item2,item3,bananas
item2,item3,bananas
$ cat tst.awk
BEGIN { FS=OFS="," }
NR == FNR {
    idx2fruit[FNR] = $0
    next
}
!seen[$1]++ {
    item2idx[$1] = cnt++
}
{
    idx = item2idx[$1]
    fruit = (FNR > 1 ? idx2fruit[idx] : "HEADER3")
    print $0, fruit
}

$ awk -f tst.awk file1.csv file2.csv
HEADER1,HEADER2,HEADER3
item1,item3,apples
item1,item3,apples
item1,item3,apples
item2,item3,bananas
item2,item3,bananas
item2,item3,bananas

This might work for you (GNU sed):这可能对你有用(GNU sed):

sed -E '1{x;s/^/cat file1/e;s/$/\n/;x;s/$/,HEADER3/;b}
        2H;:a;G;/^([^,]*,).*\n\1/{s/\n([^\n]*).*/,\1/;b}
        x;s/^[^\n]*\n(.*)\n.*/\1/;x;s/\n.*//;H;ba' file2

Overview:概述:

Keep file1 lines in the hold space and append them to lines from file2 .file1行保留在保留空间中,并将它们附加到file2行。 When the key changes in file2 pop off a line from file1 in the hold space and repeat.file2的键更改时,从保留空间中的file1中弹出一行并重复。

NB The hold space doubles as a repository for lines from file1 and also stores the previous lines key from file2 . NB 保留空间兼作file1的存储库,还存储file2以前的行键。

Prime the hold space with records from file1 and append the third header.使用来自file1记录file1保留空间并附加第三个标题。

Compare the current line with the previous and if the keys are the same, append the first line in the hold space of file1将当前行与前一行进行比较,如果key相同,则在file1的hold space中追加第一行

Otherwise, delete the first line in the hold space of file1 and replace the key by the current lines key, remove the appended lines from the hold space on the the current line, then jump back and execute the previous instructions as to when the keys matched.否则,删除file1的hold space中的第一行,用当前行key替换key,删除当前行hold space中的附加行,然后跳回执行前面的指令,key何时匹配.

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

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