将2个基于datetime的csv文件与shell组合
嗨, 我有3个csv文件,如下所示将2个基于datetime的csv文件与shell组合,shell,csv,awk,Shell,Csv,Awk,嗨, 我有3个csv文件,如下所示 datetime,预测 2016-02-02 00:00:00, 23.34 2016-02-02 00:10:0029.23 时间戳,预测,v1,v2 2016-02-02 00:00:00, 68.56, 012, .23 2016-02-02 00:10:00,23.24,25,32 时间戳,预测[ma],v1 2016-02-02 00:00:00, 56.32, 32 2016-02-02 00:10:0025.2156 我希望我的输出具有 时间,预
datetime,预测
2016-02-02 00:00:00, 23.34
2016-02-02 00:10:0029.23
时间戳,预测,v1,v2
2016-02-02 00:00:00, 68.56, 012, .23
2016-02-02 00:10:00,23.24,25,32
时间戳,预测[ma],v1
2016-02-02 00:00:00, 56.32, 32
2016-02-02 00:10:0025.2156
我希望我的输出具有
时间,预测,预测1,预测2
2016-02-02 00:00:00, 23.34, 68.56, 56.32
2016-02-02 00:10:00,29.23,23.24,25.21
我已经创建了代码,将这些文件在xlsx中与python结合起来。现在我计划用shell进一步处理这些文件,我希望这些文件是csv格式的
我试过这样的代码
join-j2-o1.1,1.2,2.2请您尝试以下内容(这在大多数awk
s中都应该有效)
输出如下
Time, Forecast, forecast1, forecast2
2016-02-02 00:00:00, 23.34, 68.56, 56.32
2016-02-02 00:10:00, 29.23, 23.24, 25.21
解释:现在添加上述代码的详细解释
awk ' ##Starting awk program here.
BEGIN{ ##Mentioning BEGIN section of awk which will execute before Input_file(s) getting read.
FS=OFS=", " ##Setting FS and OFS as ", " read man awk for FS and OFS too.
print "Time, Forecast, forecast1, forecast2" ##Printing headers for output.
} ##Closing BEGIN section here.
FNR==1{ ##Checking condition if this is first line of all Input_file(s).
++count ##Increment variable count with 1 here.
next ##next will skip all further statements from here.
} ##Closing FNR==1 BLOCK here.
count==1{ ##Checking if count==1 then do following.
a[$1]=$2 ##Creating an array a whose index $1 and value is $2.
next ##next will skip all further statements.
} ##Closing count==1 BLOCK here.
count==2{ ##Checking condition if count==2 then do following.
a[$1]=a[$1] OFS $2 ##Concatenate value of a[$1] to its previous value which it got from file1.csv
next ##next will skip all further statements from here.
} ##Closing count==2 BLOCK here.
count==3{ ##Checking condition if count==3 then do following.
print $1,a[$1],$2 ##Printing first field, a[$1] value and $2 of current line for file3.csv
}' file1.csv file2.csv file3.csv ##Mentioning all Input_file(s) names here.
awk ' ##Starting awk program here.
BEGIN{ ##Mentioning BEGIN section of awk which will execute before Input_file(s) getting read.
FS=OFS=", " ##Setting FS and OFS as ", " read man awk for FS and OFS too.
print "Time, Forecast, forecast1, forecast2" ##Printing headers for output.
} ##Closing BEGIN section here.
FNR==1{ ##Checking condition if this is first line of all Input_file(s).
++count ##Increment variable count with 1 here.
next ##next will skip all further statements from here.
} ##Closing FNR==1 BLOCK here.
count==1{ ##Checking if count==1 then do following.
a[$1]=$2 ##Creating an array a whose index $1 and value is $2.
next ##next will skip all further statements.
} ##Closing count==1 BLOCK here.
count==2{ ##Checking condition if count==2 then do following.
a[$1]=a[$1] OFS $2 ##Concatenate value of a[$1] to its previous value which it got from file1.csv
next ##next will skip all further statements from here.
} ##Closing count==2 BLOCK here.
count==3{ ##Checking condition if count==3 then do following.
print $1,a[$1],$2 ##Printing first field, a[$1] value and $2 of current line for file3.csv
}' file1.csv file2.csv file3.csv ##Mentioning all Input_file(s) names here.