使用其他.csv列替换.csv中的列

时间:2015-08-13 04:24:07

标签: bash csv awk grep ksh

我有2个带有15 000行的csv文件,如下所示:

Daily.csv

CODE,Images URL
M-1001,http://www.domain.com/overstock-images/M-1001arger.jpg
M-1004,http://www.domain.com/overstock-images/M-1004larger.jpg
W-C-4948,http://www.domain.com/overstock-images/W-C-4948larger.jpg

GoodImages.csv

Output is desired as:

"CODE","BRAND","DESIGNER","SIZE","TYPE","GENDER","SET","DESCRIPTION","IMAGE","COST","WEIGHT","MSRP","UPC"
"M-1001","212","Caroline Her","1.7 oz","EDT Spray","Men","","text.","http://www.domain.com/overstock-images/M-1001larger.jpg","31.08","1","57.00","8411061341704"
"M-1003","1881","Nino Cer","1.7 oz","EDT Spray","Men","","text.","http://www.domain.com/overstock-images/M-1004larger.jpg","24.13","1","36.00","688575003642"
"M-1004","1881","Nino Cer","3.4 oz","EDT Spray","Men",""," text.","http://www.domain.com/products/Men/Final/M-1004large.jpg","37.51","1","50.00","688575003659"

只有当两个字段1匹配时,我才需要将来自Daily.csv的字段9“IMAGE”替换为来自GoodImages.csv的字段2“图像URL”。

var _value = firstCalc(4); //this is undefined!!
setTimeout(function() { console.log(_value); }, 5000);

如果可能,我想从cron作业中运行它。

CENTOS 6.7 x86_64标准

谢谢

2 个答案:

答案 0 :(得分:1)

join -a 1 -o 1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.10,1.11,1.12,1.13,2.2,1.9 -t, <(sort -t, -k 1,1 Daily.csv) <(sed 's/[^,]*/"&"/g' GoodImages.csv | sort -t, -k 1,1) | sed 's/,,/,/'| cut -d , -f -13 | awk -F"," '{OFS = ","; print $1,$2,$3,$4,$5,$6,$7,$8,$13,$9,$10,$11,$12}'

输出:

"CODE","BRAND","DESIGNER","SIZE","TYPE","GENDER","SET","DESCRIPTION","Images URL","COST","WEIGHT","MSRP","UPC"
"M-1001","212","Caroline Her","1.7 oz","EDT Spray","Men","","text.","http://www.domain.com/overstock-images/M-1001arger.jpg","31.08","1","57.00","8411061341704"
"M-1003","1881","Nino Cer","1.7 oz","EDT Spray","Men","","text.","http://www.domain.com/products/Men/Final/M-1003large.jpg","24.13","1","36.00","688575003642"
"M-1004","1881","Nino Cer","3.4 oz","EDT Spray","Men",""," text.","http://www.domain.com/overstock-images/M-1004larger.jpg","37.51","1","50.00","688575003659"

答案 1 :(得分:0)

这可以通过awk

轻松解决
$ awk 'NR==FNR{a["\""$1"\""]=$2;next}{if ($1 in a){$9=a[$1]}}1' FS=',' OFS=',' GoodImages.csv Daily.csv
"CODE","BRAND","DESIGNER","SIZE","TYPE","GENDER","SET","DESCRIPTION",Images URL,"COST","WEIGHT","MSRP","UPC"
"M-1001","212","Caroline Her","1.7 oz","EDT Spray","Men","","text.",http://www.domain.com/overstock-images/M-1001arger.jpg,"31.08","1","57.00","8411061341704"
"M-1003","1881","Nino Cer","1.7 oz","EDT Spray","Men","","text.","http://www.domain.com/products/Men/Final/M-1003large.jpg","24.13","1","36.00","688575003642"
"M-1004","1881","Nino Cer","3.4 oz","EDT Spray","Men",""," text.",http://www.domain.com/overstock-images/M-1004larger.jpg,"37.51","1","50.00","688575003659"