【问题标题】:Split column and use first array to headerin awk拆分列并使用第一个数组作为 awk 中的标题
【发布时间】:2016-09-22 14:25:30
【问题描述】:

我的文件看起来像这样:

A=10 B=8 C=12
A=15 B=12 C=5
A=6 B=4 C=9
A=8 B=8 C=9

列更多。我想使用 awk 拆分所有文件并在“=”之前使用字母,例如标题:

A B C
10 8 12
15 12 5
6 4 9
8 8 9

我想做这样的事情:

awk '{split($0,arr0,"="); print arr0[2]}' infile

但还是不知道如何使用arr0[1] like header。

感谢您的任何想法。

【问题讨论】:

标签: bash awk split header


【解决方案1】:
$ awk 'NR==1{h=$0;gsub(/=[^ ]+/,"",h);print h} {gsub(/[^ =]+=/,"")} 1' file
A B C
10 8 12
15 12 5
6 4 9
8 8 9

【讨论】:

    【解决方案2】:

    使用 awk 你可以做到这一点:

    awk -F '[= ]' 'function prnt(start) {
       for (i=start; i<=NF; i+=2)
          printf "%s%s", (i==start?"":OFS), $i
       print ""
    }
    NR==1 {
       prnt(1)
    }
    {
       prnt(2)
    }' file
    
    A B C
    10 8 12
    15 12 5
    6 4 9
    8 8 9
    

    要获得表格格式的输出,请使用:

    awk -F '[= ]' 'function prnt(start) {
       for (i=start; i<=NF; i+=2)
          printf "%s%s", (i==start?"":OFS), $i
       print ""
    }
    NR==1 {
       prnt(1)
    }
    {
       prnt(2)
    }' file | column -t
    
    A   B   C
    10  8   12
    15  12  5
    6   4   9
    8   8   9
    
    【解决方案3】:

    使用 sed

    sed '1{h;s/=[^ ]*//g;p;x};s/.=//g' file
    
    A B C
    10 8 12
    15 12 5
    6 4 9
    8 8 9
    

    【讨论】:

      【解决方案4】:

      Gnu awk:

      split($0,a,"[ =]") && NR==1 {   # split the record from <space> and "="
          print a[1],a[3],a[5]        # first record, print odds
          # for(i=1;i<=NF*2;i+=2)     # you could replace above print with this
          #    printf "%s", a[i] OFS; print ""
      }
      {
          print a[3],a[4],a[6]        # the rest of records, print evens
          # for(i=2;i<=NF*2;i+=2)     # you could replace above print with this
          #     printf "%s", a[i] OFS; print ""
      }
      

      测试一下:

      $ awk foo.awk foo.txt
      A B C
      10 8 12
      15 12 5
      6 4 9
      8 8 9
      

      【讨论】:

        【解决方案5】:

        这是一个精简的awk 实现:

        BEGIN{
          print "A", "B", "C";
        }
        
        {
          split($1, a, /=/);
          split($2, b, /=/);
          split($3, c, /=/);
        
          print a[2], b[2], c[2];
        }
        

        ...和输出:

        $ awk -f /tmp/script.awk </tmp/input
        A B C
        10 8 12
        15 12 5
        6 4 9
        8 8 9
        

        【讨论】:

          【解决方案6】:

          试试这个:

          #!/bin/awk
          
          function print_record( hdr )
          {
              for( i = 1; i <= NF; i++ )
              {
                  split( $i, a, "=" )
                  printf a[ ( hdr == 1 ) ? 1 : 2 ] " "
              }
          
              print ""
          }
          
          BEGIN {
              hdr=1
          }
          
          {
              if( hdr == 1 )
              {
                  print_record( 1 )
                  hdr = 0;
              }
          
              print_record( 0 )
          }
          
          # eof #
          

          测试:

          $ awk -f script.awk -- input.txt
          

          输出:

          A B C 
          10 8 12 
          15 12 5 
          6 4 9 
          8 8 9 
          

          希望对你有帮助!

          【讨论】:

            【解决方案7】:

            perl

            $ cat ip.txt 
            A=10 B=8 C=12
            A=15 B=12 C=5
            A=6 B=4 C=9
            A=8 B=8 C=9
            
            $ # can also use: perl -lpe 'print / ?[^ ]+(?==)/g if $.==1; s/[^ ]+=//g'
            $ perl -pe 'if($. == 1){$a = s/=[^ ]+//rg; print "$a\n"} s/[^ ]+=//g' ip.txt
            A B C
            10 8 12
            15 12 5
            6 4 9
            8 8 9
            
            • if($. == 1){$a = s/=[^ ]+//rg; print "$a\n"} 对于第一行,删除 = 和其右侧的非空格字符。替换结果保存在$a中,无需修改输入行即可打印
            • s/[^ ]+=//g 删除所有行后跟= 的非空格字符
            • -p 选项表示默认情况下在所有修改后打印输入行

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-06-09
              • 2021-09-29
              • 2018-06-09
              • 1970-01-01
              • 1970-01-01
              • 2014-12-16
              • 2019-09-23
              • 1970-01-01
              相关资源
              最近更新 更多