Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何按列值分组到行和列标题中,然后动态求和列_Java_Linux_Shell_Unix_Awk - Fatal编程技术网

Java 如何按列值分组到行和列标题中,然后动态求和列

Java 如何按列值分组到行和列标题中,然后动态求和列,java,linux,shell,unix,awk,Java,Linux,Shell,Unix,Awk,下面是我的输入和输出.txt文件 我想按StatusDate和方法对数据进行分组。 然后根据StatusDate和方法对值求和 Input.txt No,Date,MethodStatus,Key,StatusDate,Hit,CallType,Method,LastMethodType 112,12/15/16,Suceess,Geo,12/15/16,1,Static,GET,12/15/16 113,12/18/16,Suceess,Geo,12/18/16,1,Static,GET,12

下面是我的输入和输出
.txt
文件

我想按
StatusDate
方法对数据进行分组。
然后根据
StatusDate
方法对值求和

Input.txt

No,Date,MethodStatus,Key,StatusDate,Hit,CallType,Method,LastMethodType
112,12/15/16,Suceess,Geo,12/15/16,1,Static,GET,12/15/16
113,12/18/16,Suceess,Geo,12/18/16,1,Static,GET,12/18/16
114,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
115,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
116,12/19/16,Suceess,Geo,12/19/16,1,Static,PUT,12/19/16
117,12/19/16,Suceess,Geo,12/19/16,1,Static,PUT,12/19/16
118,12/19/16,Waiting,Geo,12/19/16,1,Static,GET,12/19/16
119,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
120,12/17/16,Suceess,Geo,12/17/16,1,Static,GET,12/17/16
121,12/17/16,Suceess,Geo,12/17/16,1,Static,GET,12/17/16
130,12/16/16,Suceess,Geo,12/16/16,1,Static,GET,12/16/16
StatusDate,12/15/16,12/16/16,12/17/16,12/17/16,12/18/16,12/19/16,12/19/16,12/19/16,12/19/16,12/19/16,12/19/16,Grand Total
GET,1,1,1,1,1,1,1,1,1,,,9
PUT,,,,,,,,,,1,1,2
Grand Total,1,1,1,1,1,1,1,1,1,1,1,11
Out.txt

No,Date,MethodStatus,Key,StatusDate,Hit,CallType,Method,LastMethodType
112,12/15/16,Suceess,Geo,12/15/16,1,Static,GET,12/15/16
113,12/18/16,Suceess,Geo,12/18/16,1,Static,GET,12/18/16
114,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
115,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
116,12/19/16,Suceess,Geo,12/19/16,1,Static,PUT,12/19/16
117,12/19/16,Suceess,Geo,12/19/16,1,Static,PUT,12/19/16
118,12/19/16,Waiting,Geo,12/19/16,1,Static,GET,12/19/16
119,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
120,12/17/16,Suceess,Geo,12/17/16,1,Static,GET,12/17/16
121,12/17/16,Suceess,Geo,12/17/16,1,Static,GET,12/17/16
130,12/16/16,Suceess,Geo,12/16/16,1,Static,GET,12/16/16
StatusDate,12/15/16,12/16/16,12/17/16,12/17/16,12/18/16,12/19/16,12/19/16,12/19/16,12/19/16,12/19/16,12/19/16,Grand Total
GET,1,1,1,1,1,1,1,1,1,,,9
PUT,,,,,,,,,,1,1,2
Grand Total,1,1,1,1,1,1,1,1,1,1,1,11
我使用
awk
并通过
awk-F,{if($8==“GET”)print}
分割数据,然后计算和值。 由于文件大小很大,因此会有延迟


能一步到位吗?因此文件操作将减少?

您可以使用如下GNU awk脚本:

script.awk

BEGIN { PROCINFO["sorted_in"] = "@ind_str_asc" }

function remember( theDate, mem) {
    mem[   theDate] +=1
    # in Totals the column sum is stored for each possible date (i.e the columns)
    Totals[theDate] += 1
}

# with header 0 or 1 the first line in output is differentiated
# OFS is used, so it is possible to use a commandline option like 
# -v OFS='\t' or  -v OFS=','
function printMem( mem, name, header ) {
    printf("%s%s",name,OFS)
    sum=0
    for( k in Totals ) { 
        if( header) 
            printf("%s%s", k, OFS )
        else { 
            printf("%s%s", mem[k], OFS )
            sum += mem[k]
        }
    }
    if(!header) 
        printf("%s", sum )
    else 
        printf("Grand Total")
    print ""
}

# different methods are stored in different arrays
$8 == "GET" { remember( $2, get ) }
$8 == "PUT" { remember( $2, put ) }

END { # print the stored values
      # the first line header
      printMem( Totals , "StatusDate", 1)
      printMem( get    , "GET", 0)
      printMem( put    , "PUT", 0)
      # the summary line
      printMem( Totals , "Grand Total", 0)
    }
按如下方式运行脚本:
awk-F,-v OFS=','script.awk Input.txt