awk脚本返回空白输出

时间:2015-01-08 17:31:59

标签: awk

这是输入数据另存为sample.csv

aNumber|bNumber|startDate|timeZone|duration|currencyType|cost|dicatedAccused|balanceAfter|trafficCase|teleServiceCode|location|dataVolume|numberOfEvents|fafIndicator|netWorkID|serviceProvideID|serviceClass|nAno|nBno|bNumberZnCode|fileNamedID|Destination|Operator|unknown3|MainAmount|ReAnalyse|DEDICATEDACCBALBEF|DEDICATEDACCBALAFT|ACCOUNTGROUPID|SERVICEOFFERINGS|SELECTEDCOMMUNITYID|BALANCEBEFORE
22677512549|778|2014-07-02 10:16:35.000|NULL|NULL|localCurrency|0.00|NULL|11.50|0|4|22676020076|NULL|NULL|NULL|NULL|NULL|34|77512549|778|NULL|1131257|OTHER|Short Code|126244088|0.0000|0|NULL|NULL|NULL|NULL|NULL|11.5000
22675557361|76457227|2014-07-02 10:16:38.000|NULL|NULL|localCurrency|10.00|NULL|1009.10|0|4|22676613028|NULL|NULL|1|NULL|NULL|35|75557361|76457227|NULL|1131257|Airtel|Airtel|4132206314|10.0000|0|NULL|NULL|NULL|NULL|NULL|1019.1000
22677521277|778|2014-07-02 10:16:42.000|NULL|NULL|localCurrency|0.00|NULL|0.00|0|4|22676020078|NULL|NULL|NULL|NULL|NULL|34|77521277|778|NULL|1131257|OTHER|Short Code|130071591|0.0000|0|NULL|NULL|NULL|NULL|NULL|0.0000
22676099496|77250331|2014-07-02 10:16:42.000|NULL|NULL|localCurrency|1.00|9|0.50|0|4|22676613028|NULL|NULL|NULL|NULL|NULL|35|76099496|77250331|NULL|1131257|Airtel|Airtel|4132218551|0.0000|0|4.0000|3.0000|NULL|NULL|NULL|0.5000
22667222160|22667262389|2014-07-02 10:16:43.000|NULL|NULL|localCurrency|10.00|NULL|16070.00|0|4|22676613028|NULL|NULL|NULL|NULL|NULL|35|67222160|67262389|NULL|1131257|Airtel|Airtel|4132222628|10.0000|0|NULL|NULL|NULL|NULL|NULL|16080.0000
22665799922|70110055|2014-07-02 10:16:45.000|NULL|NULL|localCurrency|20.00|6|0.50|0|4|22676020076|NULL|NULL|NULL|NULL|NULL|35|65799922|70110055|NULL|1131257|Telmob|Telmob|126260244|20.0000|0|44.0000|24.0000|NULL|NULL|NULL|0.5000
22676239633|433|2014-07-02 10:16:48.000|NULL|NULL|localCurrency|0.00|NULL|0.20|0|4|22676020027|NULL|NULL|NULL|NULL|NULL|35|76239633|433|NULL|1131257|Airtel_TollFree|Short Code|397224944|0.0000|0|NULL|NULL|NULL|NULL|NULL|0.2000

我必须按日期分组,dicatedAccused,trafficCase和teleserviceCode然后根据这个组的结果我必须总结持续时间,成本,balanceAfter,MainAmount和Balancebefore。我用了一个awk脚本如下:

这是我使用的awk脚本(保存为test.awk):

BEGIN {FS="|"}FNR = 1 {next}{sub(/ .*/,"",$3);key=sprintf("%10s %10s %12s %12s",$3,$8,$10,$11)   duration[key] += $5 cost[key] += $7 bAfter[key] += $9 main[key] += $26 dedAccbBefore[key] += $27 dedAccbAfter[key] += $28 bBefore[key] += $NF}END {printf "%-10s\t\t %10s %12s %12s %10s %10s %10s %10s %12s %12s %10s\n", "date","dAccused","TrafficCase","ServiceCode","Duration","Cost","BalanceAfter","MainAmount","DAcBlBefore","DAcBlAfter","BalanceBefore" 
for (i in duration) { 
printf "%-47s %10s %10s %10s %10s %10s %10s\t %10s\n", i,duration[i],cost[i],bAfter[i],main[i],dedAccbBefore[i],dedAccbAfter[i],bBefore[i] }}

当我运行我的awk脚本时:

$ awk -f test.awk sample.csv

除了标题

之外,我的输出是空白的
date         dAccused  TrafficCase  ServiceCode   Duration       Cost BalanceAfter MainAmount  DAcBlBefore   DAcBlAfter BalanceBefore

我希望输出为:

date       dAccused TrafficCase ServiceCode Duration    Cost    BalanceAfter    MainAmount  DAcBlBefore DAcBlAfter  BalanceBefore   
2014-07-02   6       0           4           0           20      0.5             20             0            44          0.5    
2014-07-02  NULL     0           4           0           10      17090,8         20             0            0          17110,8 
2014-07-02   9       0           4           0            1      0,5             0              0            4           0,5    

有人可以帮我弄清楚我的剧本有什么问题吗?

1 个答案:

答案 0 :(得分:1)

您的规则FNR = 1是一个赋值,而不是一个测试,因此每行的计算结果为true。您应该将其更改为FNR == 1

除了简单的语法错误之外,你当前形式的脚本也会为我产生很多语法错误,因为你在同一行上有很多语句没有任何;分隔他们。如果您打算使用awk脚本,而不是单行,则应添加一些换行符以使脚本更具可读性:

BEGIN { FS = "|" }
FNR == 1 { next }
{ 
    sub(/ .*/,"",$3) 
    key=sprintf("%10s %10s %12s %12s",$3,$8,$10,$11)
    duration[key] += $5 
    cost[key] += $7 
    bAfter[key] += $9 
    main[key] += $26 
    dedAccbBefore[key] += $27 
    dedAccbAfter[key] += $28 
    bBefore[key] += $NF
}
END {
    printf "%-10s\t\t %10s %12s %12s %10s %10s %10s %10s %12s %12s %10s\n", 
        "date","dAccused","TrafficCase","ServiceCode","Duration","Cost","BalanceAfter","MainAmount","DAcBlBefore","DAcBlAfter","BalanceBefore" 
    for (i in duration) { 
        printf "%-47s %10s %10s %10s %10s %10s %10s\t %10s\n", 
            i,duration[i],cost[i],bAfter[i],main[i],dedAccbBefore[i],dedAccbAfter[i],bBefore[i] 
    }
}

这是我得到的输出,在您的文件上运行上述脚本:

$ awk -f test.awk file                                                  
date                       dAccused  TrafficCase  ServiceCode   Duration       Cost BalanceAfter MainAmount  DAcBlBefore   DAcBlAfter BalanceBefore
      NULL          0  22676020027         NULL          0        0.2          4          0          0          0                 0
    0.0000       NULL       0.2000                       0          0          0          0          0          0               0.2
      NULL          0  22676020078         NULL          0          0          4          0          0          0                 0
      NULL          0  22676613028         NULL         21    17079.6         12          4          3          0           17099.6
    0.0000       NULL      11.5000                       0          0          0          0          0          0              11.5
    0.0000       NULL       0.0000                       0          0          0          0          0          0                 0
      NULL          0  22676020076         NULL         20         12          8         44         24          0               0.5
2014-07-02                                               0          0          0          0          0          0             14098