Saturday, 15 September 2012

unix - Replace column in one csv file from other csv file -



unix - Replace column in one csv file from other csv file -

i have 1 csv file below data. (, seperated)

c1,c2,c3,c4 1,2,3,4 5,6,7,8

now have other mapping csv file below

d1,d2 1,x 5,y

here want find mapping in sec csv file , update value in first csv file.

expected output (if want update c3 in first csv file against 2 records in sec mapping file)

c1,c2,c3,c4 1,2,x,4 5,6,y,8

try this:

awk -f, 'nr==fnr{if(nr>1)k[$1]=$2;next}fnr==1{print}fnr>1&&$1 in k{$3=k[$1];print}' ofs="," two.csv one.csv

test data:

kent$ head one.csv two.csv ==> one.csv <== c1,c2,c3,c4 1,2,3,4 5,6,7,8 ==> two.csv <== d1,d2 1,x 5,y kent$ awk -f, 'nr==fnr{if(nr>1)k[$1]=$2;next}fnr==1{print}fnr>1&&$1 in k{$3=k[$1];print}' ofs="," two.csv one.csv c1,c2,c3,c4 1,2,x,4 5,6,y,8

unix join sed awk

No comments:

Post a Comment