need code in awk unix or use substr

后端 未结 2 1290
轻奢々
轻奢々 2021-01-26 03:54

I want to print words between \"ctr{words}\" and count the same words in a file.

I tried:

sed -n \'s/.*ctr{\\(.[^}]*\\).*/\\1/p\' file

2条回答
  •  梦毁少年i
    2021-01-26 04:36

    It looks like you are missing the counts. The easiest way to do this is to pipe your output through uniq -c:

    $ sed -n 's/.*ctr{\(.[^}]*\).*/\1/p' file | sort | uniq -c
          1 **Mo7afazat**
          1 **JaishanaIN**
          2 **ZainElKul**
          1 ZainUnlimited
          1 **AlBarakehNew**
          1 **ZainElKulSN**
    

    Another way, only using awk:

    $ awk 'match($0,".*ctr{([^}]*)}.*",m){a[m[1]]++}END{for(i in a) print i,a[i]}' file
    ZainUnlimited 1
    **ZainElKulSN** 1
    **Mo7afazat** 1
    **ZainElKul** 2
    **JaishanaIN** 1
    **AlBarakehNew** 1
    

提交回复
热议问题