Count occurrences of character per line/field on Unix

后端 未结 10 2105
难免孤独
难免孤独 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:03

    awk '{gsub("[^t]",""); print length($0),NR;}' stores.dat
    

    The call to gsub() deletes everything in the line that is not a t, then just print the length of what remains, and the current line number.

    Want to do it just for column 2?

    awk 'BEGIN{FS="|"} {gsub("[^t]","",$2); print NR,length($2);}' stores.dat
    

提交回复
热议问题