need code in awk unix or use substr

后端 未结 2 1293
轻奢々
轻奢々 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条回答
  •  佛祖请我去吃肉
    2021-01-26 04:16

    When searching for matches in files grep is the best choice more often than not.

    Using grep with postive lookahead and uniq -c:

    $ grep -Po "(?<=ctr{)[^}]+" file | uniq -c
    1 Mo7afazat
    1 JaishanaIN
    2 ZainElKul
    1 ZainUnlimited
    1 AlBarakehNew
    1 ZainElKulSN
    

    From man uniq:

    Note: 'uniq' does not detect repeated lines unless they are adjacent.

    For files where the duplicates are not adjacent pipe to sort first however the order in which each match is found in the orignal file will be lost:

    grep -Po "(?<=ctr{)[^}]+" file | sort | uniq -c
    1 AlBarakehNew
    1 JaishanaIN
    1 Mo7afazat
    2 ZainElKul
    1 ZainElKulSN
    1 ZainUnlimited
    

提交回复
热议问题