从其他CSV文件中查找匹配值,然后将匹配值追加为csv文件中的新列

时间:2015-08-20 08:58:43

标签: bash shell csv

我有2个CSV文件,其中包含超过500行

1包含产品信息

ID, names, url, color, size
p01, abc, uploads/abc.jpg, red, M
P02, abz, uploads/abz.jpg, yellow, S
p03, xyz, uploads/xyz.jpg, green, L
p04, qwe, uploads/qwe.jpg, blue, XL

另一个包含与该产品相关的图像位置,一个产品有许多imgs。

ID, product_id, url
01, p01, uploads/item/asa.jpg
02, p01, uploads/item/aza.jpg
03, p01, uploads/item/dna.jpg
04, p01, uploads/item/adn.jpg
05, p02, uploads/item/bka.jpg
06, p02, uploads/item/bda.jpg
07, p04, uploads/item/cde.jpg

这样的结果

ID, names, url, color, size, thumb1, thumb2, thumb3, thumb4, ....
p01, abc, uploads/abc.jpg, red, M, uploads/item/asa.jpg, uploads/item/aza.jpg, uploads/item/dna.jpg, uploads/item/adn.jpg
P02, abz, uploads/abz.jpg, yellow, S, uploads/item/bka.jpg, uploads/item/bda.jpg
p03, xyz, uploads/xyz.jpg, green, L, 
p04, qwe, uploads/qwe.jpg, blue, XL, uploads/item/cde.jpg

那么我可以在终端中使用哪些实用程序?

1 个答案:

答案 0 :(得分:1)

您可以使用awk:

public double getPredictionStdErr(double x) {
    if( !hasIntercept ){
        return Double.NaN;
    }
    return FastMath.sqrt(
         getMeanSquareError() * ((1d / n) + ((xbar - x) * (xbar - x)) / sumXX));
}

<强>输出:

awk 'BEGIN{FS=OFS=", "}
     NR==1{print "ID, names, url, color, size, thumbnails"; next}
     FNR==NR{$2=tolower($2); a[$2] = (a[$2]?a[$2] ", " $3:$3); next}
     FNR>1{print $0, a[tolower($1)]}' file2 file1