【问题标题】:Count unique string occurences in each colum and print results by colum计算每列中出现的唯一字符串并按列打印结果
【发布时间】:2021-12-27 21:05:05
【问题描述】:

我有一个包含大约 2000 万行和 100 列的文本文件。我想输出每列中每个字符串出现次数的汇总统计信息。这是文件的一部分,包括作为示例名称的标题。

 LP605  LP606   LP607   LP608   LP609
0/0 0/0 0/0 0/0 0/0
0/0 1/1 1/1 0/0 0/0
0/0 0/0 0/0 0/0 0/0
0/0 0/0 0/0 0/0 0/0
0/1 0/1 0/1 0/1 0/0
1/1 0/0 0/1 0/0 0/0
1/1 1/1 ./. 0/0 ./.
0/0 0/0 ./. 0/0 ./.
0/1 0/1 0/0 0/0 0/1

汇总统计的期望输出

Summary LP605   LP606   LP607   LP608   LP609
0/0 4   4   8   8   6
0/1 2   2   1   1   1
1/1 2   1   1   0   0
./. 0   2   2   0   2

谢谢

【问题讨论】:

    标签: string sum unique


    【解决方案1】:

    这可以解决问题,使用 awk

      NR==31 {                  # File header is on line 31
        print "GT", $0          # Print "GT" followed by header
        n=NF                    # Record the number of columns
        next                    # Stop processing
      }
      {                         # For every line
        for(i=1; i<=NF; i++) {  # & for every column ("i")
          A[i,$i]++             # Use an array A to store the the number of occurrences of the value ($i) in column i
          V[$i]                 # Record all values of ($i)
        }
      } END {
        for(j in V) {           # For all values of ($i)
          $1=j                  # Assign the value to field 1
          for(i=1; i<=n; i++)   # For all columns            
            $(i+1)=A[i,j]+0     # Assign the number of occurrences of value "j" to the appropriate column
          print                 # Print the line
        }
      } 
      OFS='\t' file             # Use tab output field separator
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      相关资源
      最近更新 更多