【问题标题】:Shell script to read a list of words and compute their counts in a corpus.用于读取单词列表并计算它们在语料库中的计数的 Shell 脚本。
【发布时间】:2013-04-05 10:08:17
【问题描述】:

我需要在 linux 中编写一个命令行脚本来执行以下操作:

  • 从文本文件中读取单词列表(每行一个单词)。说w_i

  • 为每个 w_i 计算不同文本文件中的字数。

  • 对这些计数求和

这里的一些帮助将不胜感激!

【问题讨论】:

    标签: linux shell command-line wc


    【解决方案1】:

    这条 grep 行可能适合你,试试看:

     grep -oFwf wordlist textfile|wc -l
    

    我刚刚做了这个小测试,它看起来像你预期的那样工作。

    (PS,我用vim在file2中插入了这些词,所以我知道我插入了多少)

    kent$  head file1 file2
    ==> file1 <==
    foo
    bar
    baz
    hello
    world
    
    ==> file2 <==
     foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar
     hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world 
    blah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo bablah bbbb fo ba 
    
    kent$  grep -oFwf file1 file2|wc -l
    66
    

    【讨论】:

    • 这听起来很对,但我的总计数总是为零。 kamituel 和 sudo_O 解决方案也是如此。我认为这与寻找字数有关,例如foo\s+ 而不仅仅是 file2 中的 foo。另外,在 file1 中,我还出现了像“很多”这样的二元组。
    • 我认为这与您在两个文件中的数据有关。例如foo 将计入 foo 但不计入 foowhatever
    • 嗯,这在我的分析中并没有真正引起任何问题。您对解决二元或三元问题有什么建议吗?例如,file1 的某些条目如下所示:foo foo、world、a lot 等。
    【解决方案2】:

    这里使用awk 打印字数和总数:

    awk 'NR==FNR{w[$1];next}{for(i=1;i<=NF;i++)if($i in w)w[$i]++}END{for(k in w){print k,w[k];s+=w[k]}print "Total",s}' file1 file2
    hello 13
    foo 20
    world 13
    baz
    bar 20
    Total 66
    

    注意:使用Kents 示例输入。

    更易读的脚本版本:

    BEGIN {
        OFS="\t"                              # Space the output with a tab 
    }
    NR==FNR {                                 # Only true in file1
        word_count[$1]                        # Build keys for all words           
        next                                  # Get next line
    }
    {                                         # In file2 here
        for(i=1;i<=NF;i++)                    # For each word on the current line
            if($i in word_count)              # If the word has a key in the array
                word_count[$i]++              # Increment the count
    }
    END {                                     # After all files have been read
        for (word in word_count) {            # For each word in the array
            print word,int(word_count[word])  # Print the word and the count
            sum+=word_count[word]             # Sum the values
        }
        print "Total",sum                     # Print the total
    }
    

    另存为script.awk 并运行如下:

    $ awk -f script.awk file1 file2
    hello   13
    foo     20
    world   13
    baz     0
    bar     20
    Total   66
    

    【讨论】:

    • 你能用这个file1试试吗(下一条评论)看看问题出在哪里?我认为无法捕获正确字数的问题与 endofline 字符有关,然后是我在列表中的二元组
    • 很多事后的先验后遗弃弃弃弃弃被绑架者遵守
    • 修复二元组不是问题,在没有相应的file 2 的情况下使用file1 进行毫无意义的测试。您已经将答案标记为接受,您的问题现在解决了吗?
    • 二元组问题仍然存在(我也有三元组,有时甚至是 4 克组)。那我该如何解决呢?
    • 如果您完成您的回答,我将不胜感激,因为虽然它做得很好,但如果不能捕获双/三元组,对我来说毫无用处。
    【解决方案3】:

    假设您有文件words,每个文件包含一个单词,然后您有文件corpus,您可以使用以下命令:

    $ cat file | xargs -I% sh -c '{ echo "%\c"; grep -o "%" corpus | wc -l; }' | \
      tee /dev/tty | awk '{ sum+=$2} END {print "Total " sum}'
    

    例如,file

    car
    plane
    bike
    

    对于corpus

    car is a plane is on a car
    or in the car via a plane
    plane plane
    car    
    

    输出将是:

    $ cat file | xargs -I% sh -c '{ echo "%\c"; grep -o "%" corpus | wc -l; }' | \
      tee /dev/tty | awk '{ sum+=$2} END {print "Total " sum}'
    car       4
    plane       4
    bike       0
    Total 8
    

    【讨论】:

      猜你喜欢
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2015-05-27
      • 2017-11-11
      • 1970-01-01
      相关资源
      最近更新 更多