Count occurrences of character per line/field on Unix

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

    perl -e 'while(<>) { $count = tr/t//; print "$count ".++$x."\n"; }' stores.dat
    

    Another perl answer yay! The tr/t// function returns the count of the number of times the translation occurred on that line, in other words the number of times tr found the character 't'. ++$x maintains the line number count.

提交回复
热议问题