How do I combine 2 csv files + all content + cygwin/bash/awk/sed/paste

前端 未结 1 403
南笙
南笙 2021-01-26 08:28

How do I combine 2 CSV files(finle1.csv and file2.csv)? I have explored awk/sed/paste but it is beyond me.

file1.csv

Time,Object,Integrity,KPI 1-A Name          


        
相关标签:
1条回答
  • 2021-01-26 09:10

    Using awk:

    awk -F, 'NR==FNR{a[$2]=$0;next}$2 in a{ print a[$2],$4, $5 }' OFS=, file1.csv file2.csv
    Time,Object,Integrity,KPI 1-A Name A unit(unit/s),KPI 2-A Name B unit(unit/s),KPI 1-C Name A unit(unit),KPI 1-D Name A unit(unit)
    2014-06-04 11:00,ObjectA,100%,0.0316,0.0012,0.024,0.0014
    2014-06-04 21:00,ObjectB,100%,40.0332,7.2601,60.6176,29.0913
    

    Using join:

    join -t, -j 2 -o 1.1 1.2 1.3 1.4 1.5 2.4 2.5 file1.csv file2.csv
    Time,Object,Integrity,KPI 1-A Name A unit(unit/s),KPI 2-A Name B unit(unit/s),KPI 1-C Name A unit(unit),KPI 1-D Name A unit(unit)
    2014-06-04 11:00,ObjectA,100%,0.0316,0.0012,0.024,0.0014
    2014-06-04 21:00,ObjectB,100%,40.0332,7.2601,60.6176,29.0913
    

    Update:

    To join on date and object, you can use awk as join only joins on 1 column.

    awk -F, 'NR==FNR{sub(/ .*/,"",$1);map[$1,$2]=$0;next}(($1,$2) in map){print map[$1,$2],$4,$5}' OFS=, f1 f2
    Time,Object,Integrity,KPI 1-A Name A unit(unit/s),KPI 2-A Name B unit(unit/s),KPI 1-C Name A unit(unit),KPI 1-D Name A unit(unit)
    2014-06-04,ObjectA,100%,0.0316,0.0012,0.024,0.0014
    2014-06-04,ObjectB,100%,40.0332,7.2601,60.6176,29.0913
    
    0 讨论(0)
提交回复
热议问题