Count occurrences of character per line/field on Unix

后端 未结 10 2128
难免孤独
难免孤独 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 16:55

    grep -n -o "t" stores.dat | sort -n | uniq -c | cut -d : -f 1
    

    gives almost exactly the output you want:

      4 1
      3 2
      6 3
    

    Thanks to @raghav-bhushan for the grep -o hint, what a useful flag. The -n flag includes the line number as well.

提交回复
热议问题