将行转换为多列的Shell脚本
输入CSV文件:

Driver Id,Driver Name,Measure Names,Measure Values
123,XYZ,Total Offers,10
123,XYZ,Driver Reject,0
123,XYZ,Driver Accept ,4
123,XYZ,Expired Offers,3
123,XYZ,Total Bookings,6
123,XYZ,Rider Cancels,2
123,XYZ,Driver Cancels,0
123,XYZ,Rider No-Show,0
123,XYZ,Completed Rides,4
124,PQR,Total Offers,2
124,PQR,Driver Reject,0
124,PQR,Driver Accept ,1
124,PQR,Expired Offers,1
124,PQR,Total Bookings,1
124,PQR,Rider Cancels,0
124,PQR,Driver Cancels,0
124,PQR,Rider No-Show,0
124,PQR,Completed Rides,1

所需输出:
驾驶员Id、驾驶员姓名、总优惠、驾驶员拒绝、驾驶员接受、过期优惠、总预订、取消驾乘、取消驾乘、取消驾乘、取消驾乘、完成驾乘
123,XYZ,10,0,4,3,6,2,0,0,4
124,PQR,2,0,1,1,1,0,0,0,1

我用锥子试过了,但结果不正确。
awk -F\, '
    BEGIN{
        P["Total Offers"]="%s;%s;%s;;;;;;;;;\n"
       P["Driver Reject"]="%s;%s;;%s;;;;;;;;\n"
       P["Driver Accept"]="%s;%s;;;%s;;;;;;;\n"
      P["Expired Offers"]="%s;%s;;;;%s;;;;;;\n"
      P["Total Bookings"]="%s;%s;;;;;%s;;;;;\n"
       P["Rider Cancels"]="%s;%s;;;;;;%s;;;;\n"
      P["Driver Cancels"]="%s;%s;;;;;;;%s;;;\n"
       P["Rider No-Show"]="%s;%s;;;;;;;;%s;;\n"
     P["Completed Rides"]="%s;%s;;;;;;;;;%s;\n"
        }
    FNR==1{
        print "Driver Id,Driver Name,Total Offers,Driver Reject,Driver Accept,Expired Offers,Total Bookings,Rider Cancels,Driver Cancels,Rider No-Show,Completed Rides"
        next
        }
    {
        printf(P[$3],$1,$2,$4)
        }
    ' sample1.csv

有人可以帮助我或给我看其他方法来实现这一点吗。
提前谢谢

最佳答案

考虑到您的输入_文件与显示的示例相同,并且如果您不关心输出序列是否应该作为输入,那么下面的内容可能会帮助您实现相同的效果。

awk -F, 'FNR>1{a[$1,$2]=a[$1,$2]?a[$1,$2] FS $NF:$NF} END{for(i in a){print i FS a[i]}}' SUBSEP=","   Input_file

关于linux - LINUX Shell脚本将行转换为多列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47011958/

10-09 13:53