Count occurrences of character per line/field on Unix

后端 未结 10 2131
难免孤独
难免孤独 2020-12-23 16:19

Given a file with data like this (ie stores.dat file)

sid|storeNo|latitude|longitude
2tt|1|-28.0372000t0|153.42921670
9|2t|-33tt.85t09t0000|15t1.03274200
         


        
10条回答
  •  情书的邮戳
    2020-12-23 17:09

    To count occurences of a character per line:

    $ awk -F 't' '{print NF-1, NR}'  input.txt
    4 1
    3 2
    6 3
    

    this sets field separator to the character that needs to be counted, then uses the fact that number of fields is one greater than number of separators.

    To count occurences in a particular column cut out that column first:

    $ cut -d '|' -f 2 input.txt | awk -F 't' '{print NF-1, NR}'
    1 1
    0 2
    1 3
    
    $ cut -d '|' -f 3 input.txt | awk -F 't' '{print NF-1, NR}'
    2 1
    1 2
    4 3
    

提交回复
热议问题