如何重新格式化文本文件 - awk

时间:2017-04-02 17:12:02

标签: bash unix awk

我有一个包含3列的文本文件:

  

打破香蕉192   打破苹果175
  打破鳄梨20
  固定香蕉117
  固定苹果89

我需要以下输出:

  
    

问题,香蕉,苹果,鳄梨
    破门,192,175,20
    固定,117,90,0

  

我是新手,不知道如何得到这个结果。

我感谢任何帮助, 感谢

2 个答案:

答案 0 :(得分:0)

<强>输入

$ cat file
broke banana 192
broke apple 175
fixed banana 117
fixed apple 89 

我不明白你在预期的o / p

中有fixed,117,90的位置

<强>输出

$ awk -v OFS=, '{
      is_fr[$1,$2]=$3

      if(!($1 in i_tmp))issue[++i]=$1; 
      if(!($2 in f_tmp))fruit[++f]=$2;

      i_tmp[$1]; f_tmp[$2]
   }
END{
      printf ("%s","issue"); 
      for(i=1; i in fruit; i++)
           printf("%s%s",OFS,fruit[i]); 
      print ""; 
      for(i=1; i in issue; i++)
      { 
          printf("%s",issue[i]); 
          for(j=1; j  in fruit; j++)
          { 
               printf("%s%s",OFS,(issue[i],fruit[j]) in is_fr ? is_fr[issue[i],fruit[j]]:"")
          } 
          print ""   
      }
    }' file   

issue,banana,apple
broke,192,175
fixed,117,89

如果订单无关紧要,请尝试以下

$ awk -v OFS=, '{
      issue[$1]; 
      fruit[$2]; 
      is_fr[$1,$2]=$3
   }
END{
      printf ("%s","issue"); 
      for(i in fruit)
           printf("%s%s",OFS,i); 
      print ""; 
      for(i in issue)
      { 
          printf("%s",i); 
          for(j in fruit)
          { 
               printf("%s%s",OFS,(i,j) in is_fr ? is_fr[i,j]:"")
          } 
          print ""   
      }   
   }' file
issue,apple,banana
fixed,89,117
broke,175,192

对于OP编辑的新输入

akshay@db-3325:/tmp$ cat f
broke banana 192
broke apple 175
broke avocado 20
fixed banana 117
fixed apple 89 

akshay@db-3325:/tmp$ awk -v OFS=, '{
      is_fr[$1,$2]=$3

      if(!($1 in i_tmp))issue[++i]=$1; 
      if(!($2 in f_tmp))fruit[++f]=$2;

      i_tmp[$1]; f_tmp[$2]
   }
END{
      printf ("%s","issue"); 
      for(i=1; i in fruit; i++)
           printf("%s%s",OFS,fruit[i]); 
      print ""; 
      for(i=1; i in issue; i++)
      { 
          printf("%s",issue[i]); 
          for(j=1; j  in fruit; j++)
          { 
               printf("%s%s",OFS,(issue[i],fruit[j]) in is_fr ? is_fr[issue[i],fruit[j]]+0:0)
          } 
          print ""   
      }
    }' f
issue,banana,apple,avocado
broke,192,175,20
fixed,117,89,0

答案 1 :(得分:0)

$ cat tst.awk
BEGIN { OFS="," }
{
    states[$1]
    fruits[$2]
    count[$1,$2] += $NF
}
END {
    printf "%s", "Issue"
    for (fruit in fruits) {
        printf "%s%s", OFS, fruit
    }
    print ""

    for (state in states) {
        printf "%s", state
        for (fruit in fruits) {
            printf "%s%s", OFS, count[state,fruit]+0
        }
        print ""
    }
}

$ awk -f tst.awk file
Issue,apple,banana,avocado
fixed,89,117,0
broke,175,192,20