【问题标题】:Extract tokens per line in TSV file提取 TSV 文件中的每行标记
【发布时间】:2021-06-29 09:41:41
【问题描述】:

我不是使用 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)}

它应该返回这个数字:

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    {}

它应该返回:

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)}

它返回49.5,但应该返回49

【问题讨论】:

    标签: bash windows-subsystem-for-linux


    【解决方案1】:

    另一个使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多