在 TSV 文件中每行提取标记

Extract tokens per line in TSV file

我不是使用 bash 的专家,我不知道如何计算此 TSV 文件中每行的标记数:

http://es.wikipedia.org/wiki/%22Superstar%22_Billy_Graham   {(que,9),(luchador,7),(del,7),(graham,7)}
http://es.wikipedia.org/wiki/%22Weird_Al%22_Yankovic    {(que,42),(weird,20),(yankovic,20),(del,17),(cancion,15),(vide,12)}
http://es.wikipedia.org/wiki/..._I_Lose_Myself  {(escrib,5),(que,4),(episodi,2),(los,2),(myself,2),(mencion,2),(shiv,2),(calif,2),(vez,2)}
http://es.wikipedia.org/wiki/...And_Out_Come_the_Wolves {(band,12),(ranc,10),(the,8),(lanz,7)}

应该return这个数字:

4 
6 
9 
4

我用 while loop 做了类似的事情,但我想要更快的事情:

while read line;
        do
            NUMBER_TOKENS=$(echo $line | cut -f 2 -d { | sed -e s/}//g| sed -e s/\)/\n/g  | wc -l)
        done < $TOKENCOUNTS_FILE

也许 awkcat | cut 可以找到一个好的解决方案。

希望你能帮助我。提前致谢。

这一行:

http://es.wikipedia.org/wiki/.cd    {}

应该return:

0

在这一行中:

http://es.wikipedia.org/wiki/.270_Winchester    {(fusil,13),(winchest,12),(.30,9),(springfield,9),(magnum,8),(cartuch,7),(del,7),(maus,6),(remington,6),(que,5),(.300,5),(cac,4),(.270,4),(wsm,4),(98k,3),(modific,3),(nuev,3),(emple,3),(model,3),(wssm,3),(m1903,3),(kar,2),(estos,2),(civil,2),(aleman,2),(les,2),(guerr,2),(general,2),(equip,2),(especial,2),(otros,2),(asi,2),(recalibr,2),(armer,2),(6,5,2),(.308,2),(.22,2),(.223,2),(.243,2),(savag,2),(.25,2),(7mm,2),(mas,2),(cort,2),(dispar,2),(mecan,2),(este,2),(recam,2),(coleccion,2)}

它returns 49.5但应该return49

另一个使用gsub()来计算([^)]*)s的awk:

$ awk '{print gsub(/\([^)]*\)/,"&")}' file

最后输出http://es.wikipedia.org/wiki/.cd {}http://es.wikipedia.org/wiki/.270_Winchester...

4
6
9
4
0
49