Awk 将该栏汇总并放在该栏旁边

Awk 将该栏汇总并放在该栏旁边,awk,Awk,我有一个像这样的文件 2291, 382718.00 2291, 19338.00 2291, 9073.00 2292, 4707.00 2293, 495847.00 2293, 157310.00 2293, 63582.00 2293, 100059.00 2293, 2843.00 2293, 58597.00 2293, 14836.00 2293, 24204.00 我想对第二列求和,并将结果放在最后一条记录旁

我有一个像这样的文件

2291,   382718.00
2291,    19338.00
2291,     9073.00
2292,     4707.00
2293,   495847.00
2293,   157310.00
2293,    63582.00
2293,   100059.00
2293,     2843.00
2293,    58597.00
2293,    14836.00
2293,    24204.00
我想对第二列求和,并将结果放在最后一条记录旁边 每当第一列值更改时

id,             amount,              total
2291,   382718.00 ,   
2291,    19338.00 ,   
2291,     9073.00 ,   411129.00 
2292,     4707.00 ,     4707.00
2293,   495847.00 ,   
2293,   157310.00 ,   
2293,    63582.00 ,   
2293,   100059.00 ,   
2293,     2843.00 ,   
2293,    58597.00 ,   
2293,    14836.00 ,   
2293,    24204.00 ,   917278.00
我的程序正在添加所有第二列值并给出结果 最后。如何根据第一列的值分解总计

   awk -F, '{ 
      a[NR] = $0
      sum  += $2
    }
    END {
      for (x = 1; x <= NR-1; x++) {
        printf"%s\n", a[x]
      }
      printf"%s %s\n", a[NR],sum
    }'
awk-F'{
a[NR]=$0
总和+=$2
}
结束{
对于(x=1;x第一种解决方案:请尝试以下内容

awk '
BEGIN{
  FS=", +"
  OFS=", "
}
prev!="" && prev!=$1{
  print prev_line,sum
  sum=""
}
prev==$1{
  print prev_line
}
{
  sum+=$2
  prev=$1
  prev_line=$0
}
END{
  if(sum){
    print prev_line,sum
  }
}
'  Input_file
awk '
BEGIN{
  FS=", +"
  OFS=", "
}
FNR==1{
  print
  next
}
prev!="" && prev!=$1{
  print prev_line,sum
  sum=""
}
prev==$1{
  print prev_line
}
{
  sum+=$2
  prev=$1
  prev_line=$0
}
END{
  if(sum){
    print prev_line,sum
  }
}
'  Input_file
解释:在此处添加上述代码的详细解释:

awk '                       ##Starting awk program from here.
BEGIN{                      ##Starting BEGIN section from here.
  FS=", +"                  ##Setting FS(field separator) as comma and space.
  OFS=", "                  ##Setting OFS(output field separator) as comma and space.
}                           ##Closing BLOCK for BEGIN section of this program.
prev!="" && prev!=$1{       ##Checking condition if prev is NOT NULL and prev is NOT equal to $1 of current line.
  print prev_line,sum       ##Printing prev_line and sum variable here.
  sum=""                    ##Nullifying variable sum here.
}                           ##Closing above condition BLOCK here.
prev==$1{                   ##Checking condition if variable prev is equal to $1 then do following.
  print prev_line           ##Printing variable prev_line here.
}                           ##Closing BLOCK for above condition.
{
  sum+=$2                   ##Creating variable sum and keep adding $2 value in its value.
  prev=$1                   ##Setting $1 to variable prev here.
  prev_line=$0              ##Setting current line value to variable prev_line here.
}
END{                        ##END BLOCK for this section is starting here.
  if(sum){                  ##Checking if variable sum is NOT NULL then do following.
    print prev_line,sum     ##Printing prev_line and sum variable here.
  }                         ##Closing BLOCK for above condition here.
}                           ##Closing END BLOCK of this program here.
'  Input_file               ##Mentioning Input_file name here.


第二种解决方案:如果您有任何标题值,请执行以下操作

awk '
BEGIN{
  FS=", +"
  OFS=", "
}
prev!="" && prev!=$1{
  print prev_line,sum
  sum=""
}
prev==$1{
  print prev_line
}
{
  sum+=$2
  prev=$1
  prev_line=$0
}
END{
  if(sum){
    print prev_line,sum
  }
}
'  Input_file
awk '
BEGIN{
  FS=", +"
  OFS=", "
}
FNR==1{
  print
  next
}
prev!="" && prev!=$1{
  print prev_line,sum
  sum=""
}
prev==$1{
  print prev_line
}
{
  sum+=$2
  prev=$1
  prev_line=$0
}
END{
  if(sum){
    print prev_line,sum
  }
}
'  Input_file
第一种解决方案:请尝试以下方法

awk '
BEGIN{
  FS=", +"
  OFS=", "
}
prev!="" && prev!=$1{
  print prev_line,sum
  sum=""
}
prev==$1{
  print prev_line
}
{
  sum+=$2
  prev=$1
  prev_line=$0
}
END{
  if(sum){
    print prev_line,sum
  }
}
'  Input_file
awk '
BEGIN{
  FS=", +"
  OFS=", "
}
FNR==1{
  print
  next
}
prev!="" && prev!=$1{
  print prev_line,sum
  sum=""
}
prev==$1{
  print prev_line
}
{
  sum+=$2
  prev=$1
  prev_line=$0
}
END{
  if(sum){
    print prev_line,sum
  }
}
'  Input_file
解释:在此处添加上述代码的详细解释:

awk '                       ##Starting awk program from here.
BEGIN{                      ##Starting BEGIN section from here.
  FS=", +"                  ##Setting FS(field separator) as comma and space.
  OFS=", "                  ##Setting OFS(output field separator) as comma and space.
}                           ##Closing BLOCK for BEGIN section of this program.
prev!="" && prev!=$1{       ##Checking condition if prev is NOT NULL and prev is NOT equal to $1 of current line.
  print prev_line,sum       ##Printing prev_line and sum variable here.
  sum=""                    ##Nullifying variable sum here.
}                           ##Closing above condition BLOCK here.
prev==$1{                   ##Checking condition if variable prev is equal to $1 then do following.
  print prev_line           ##Printing variable prev_line here.
}                           ##Closing BLOCK for above condition.
{
  sum+=$2                   ##Creating variable sum and keep adding $2 value in its value.
  prev=$1                   ##Setting $1 to variable prev here.
  prev_line=$0              ##Setting current line value to variable prev_line here.
}
END{                        ##END BLOCK for this section is starting here.
  if(sum){                  ##Checking if variable sum is NOT NULL then do following.
    print prev_line,sum     ##Printing prev_line and sum variable here.
  }                         ##Closing BLOCK for above condition here.
}                           ##Closing END BLOCK of this program here.
'  Input_file               ##Mentioning Input_file name here.


第二种解决方案:如果您有任何标题值,请执行以下操作

awk '
BEGIN{
  FS=", +"
  OFS=", "
}
prev!="" && prev!=$1{
  print prev_line,sum
  sum=""
}
prev==$1{
  print prev_line
}
{
  sum+=$2
  prev=$1
  prev_line=$0
}
END{
  if(sum){
    print prev_line,sum
  }
}
'  Input_file
awk '
BEGIN{
  FS=", +"
  OFS=", "
}
FNR==1{
  print
  next
}
prev!="" && prev!=$1{
  print prev_line,sum
  sum=""
}
prev==$1{
  print prev_line
}
{
  sum+=$2
  prev=$1
  prev_line=$0
}
END{
  if(sum){
    print prev_line,sum
  }
}
'  Input_file

另一个awk,使用
printf
进行漂亮的打印:

$ awk '
NR>1 {                          # no output for the first record
    printf "%s,",q              # output the previous record
    if(p==$1)                   # if $1 remains the same
        print ""                # end line
    else {
        printf "%12.2f\n",s     # otherwise print the sum
        s=""                    # reset the sum
    }
}
{                               # storing vars for next round
    p=$1                        # first field of previous record
    q=$0                        # previous record
    s+=$2                       # the sum
}
END {
    printf "%s,%12.2f\n", p, s  # in the end, flush em all
}' file
输出:

2291,   382718.00,
2291,    19338.00,
2291,     9073.00,   411129.00
2292,     4707.00,     4707.00
2293,   495847.00,
...
2293,    24204.00,   917278.00

另一个awk,使用
printf
进行漂亮的打印:

$ awk '
NR>1 {                          # no output for the first record
    printf "%s,",q              # output the previous record
    if(p==$1)                   # if $1 remains the same
        print ""                # end line
    else {
        printf "%12.2f\n",s     # otherwise print the sum
        s=""                    # reset the sum
    }
}
{                               # storing vars for next round
    p=$1                        # first field of previous record
    q=$0                        # previous record
    s+=$2                       # the sum
}
END {
    printf "%s,%12.2f\n", p, s  # in the end, flush em all
}' file
输出:

2291,   382718.00,
2291,    19338.00,
2291,     9073.00,   411129.00
2292,     4707.00,     4707.00
2293,   495847.00,
...
2293,    24204.00,   917278.00

谢谢。但这并不是打印所有行。只要在第一列的值发生变化时汇总并打印最后一条记录。@var,好的,请检查一下,让我知道这是否对您有帮助?@var,不客气,您真的应该一次看到这个链接。一旦得到答案,您能做什么cheers@RavinderSingh13只是rt给他们带图片的版本:谢谢。但这并不是打印所有行。只要在第一列的值发生变化时汇总并打印最后一条记录。@var,好的,修复了,请检查一下,让我知道这是否对您有帮助?@var,欢迎您,您真的应该一次看到这个链接。一旦得到ans,您可以做什么我们就这样cheers@RavinderSingh13只要开始给他们带图片的版本: