awk/sed 或任何其他方式

awk/sed 或任何其他方式

我有一个文件(超过 1000 行),需要以某种格式获取输出。我需要看看每个人有多少行thr。在下面的情况下,我有 3 thrs,5、4、2 分别是每行的计数thr

输入:

19608250477[thr=22321]: Res90 at
1: 0x00007f1fb38d5089 
2: 0x00007f1fb5565c79 
3: 0x00007f1fbb097775 
4: 0x00007f1fbb034a69 
5: 0x00007f1fbb035467 
19601889333[thr=19068]: Res87 at
1: 0x00007f1fc15f86c0 
2: 0x00007f1fc1a27d7c 
3: 0x00007f1fc1d0f312 
4: 0x00007f1fc1caf054 
16236545786[thr=55528]: Res67 at
1: 0x00007f1fb4959a90 
2: 0x00007f1fb557ad94 

我需要如下的输出

thr=22321 ; Count 5 # Count number of lines for each thr ; Each content will start with number (1: and finish with some numebr  
thr=19068 ; Count 4
thr=55528 ; Count 2

答案1

awk -F'[]=[]' '
    function print_count() {
        printf "thr=%d ; Count %d\n", key, count
    }
    $2 == "thr" {if (key) print_count(); key = $3; count = 0; next}
    {count++}
    END {print_count()}
' file

这使用]or=[作为字段分隔符。

答案2

另一个 awk :

awk -F'[][]' '
  function p_c () {
    print a" ; Count "b
  }
  NF == 3 {
    if ( f )
      p_c()
    a = $2
    f = 1
    next
  }
  {
    b = $1
    sub ( ":.*" , "" , b )
  }
  END {
    p_c()
  }
' infile

答案3

另一种awk解决方案利用定义记录和字段分隔符:

$ awk -v RS="[0-9]+[[]" -v FS="\n" -v OFS=";" '$0 {gsub(/[]].*/, "", $1); print $1,"Count "NF-2}' input

相关内容