【问题标题】:Check if any file in a directory contains words in a constant file检查目录中的任何文件是否包含常量文件中的单词
【发布时间】:2016-10-14 18:38:38
【问题描述】:

我有一个包含 n 个文本文件的目录。现在,我想检查这些文件中的任何一个是否包含一个(或多个)常量文件的单词。 这些文件都是单词量不同的字典。常量文件是一个密码列表,我想在其中检查这些单词。正确命中的数量应保存在变量中。这个词也应该保存在一个变量中(我认为是一个数组)。

例如:file1 包含This is my dictionaryfile2 包含And another one,我的密码列表包含this is a test for the dictionary and we have no other one。 来自file1 的点击是This is dictionaryn1=3 字)和来自file2 and onen2=2 字)。

我现在的密码是

#!/bin/bash
# program_call passwordlist.txt *.txt
passwordlist="$1"
dictionarys="$*"
for comparison in  $dictionarys; do
  cat $passwordlist $comparison| sort | uniq -d >${comparison}.compare
done

她最大的问题之一是,我的字典数量不同。也许是 2,也许是 200。没关系,所有这些都必须根据密码列表进行检查,并且结果(正确单词的数量和正确的单词本身)必须保存在他的 OWN 变量中。所以我认为每个字典有两个变量。

【问题讨论】:

  • 您的代码有什么问题?您真的需要将结果保存在 shell 变量中,而不是文件中吗?如果你想要匹配的数量,只需使用wc -l $comparison.compare
  • 您也可以使用fgrep -x -f $passwordlist $comparison 来获取文件之间匹配的行。
  • 我的问题是,我的输出总是只有一个包含所有这些结果的文件。当我只得到包含我的结果的文件时,这也可以。我想,通常每一个 $dictionary 都会保存比较,所以我的输出是例如 file1.compare、file2.compare、file3.compare 等等。但我的输出只是 .comapare 与所有结果。我不明白。另外,我只对文本文件感兴趣。所以我尝试了for comparison in $1dictionarys*.txt; do ,但我遇到了同样的问题。只有一个输出,但在这种情况下它调用 *.txt.compare。
  • 一个问题是你在$dictionarys中有密码文件,因为你在分配$*之前忘记了shift
  • 我认为变量不包含您认为应该包含的内容。将set -x 放在脚本的开头,这样您就可以看到所有变量都展开的执行跟踪。

标签: linux bash variables dictionary compare


【解决方案1】:

另一种方式

$ for f in file{1,2}; 
  do echo -n $f": "; 
     grep -iow -f <(tr ' ' '\n' <cons) $f | 
     wc -l; 
  done

file1: 3
file2: 2

将常量文件每行转换一个单词,检查字典文件中的单词匹配忽略大小写并计算匹配的出现次数。

【讨论】:

    【解决方案2】:

    我的解决方案:

    #!/bin/bash 
    # program_call_is /dictionarys/*.txt passwordlist.txt
    dictionarys="$1"
    shift
    passwordlist="$*"
    for comparison in  $dictionarys; do
    fgrep -x -f $passwordlist $comparison >${comparison}.compare
    done
    

    【讨论】:

      猜你喜欢
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2011-10-28
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多