我怎么能算含有的特定字符串的文件的数量,但不包含另一个字符串? [英] How can I count the number of files containing a specific string but not containing another string?

查看:121
本文介绍了我怎么能算含有的特定字符串的文件的数量,但不包含另一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们每天运行Selenium测试,以测试我们的网站和扩展。如果测试失败,输出包含失败的字符串(如果它包含其他字符串也没关系)。如果通过,则输出不包含FAILED的字符串,但必须包含字符串确定。如果输出中不包含两个字符串(通常当它是空的),测试失败。我们code用于计算其未能通过测试是这样的:

We run daily Selenium tests to test our website and extensions. If the test fails, the output contains the string FAILED (it doesn't matter if it contains other strings). If it passes, the output doesn't contain the string FAILED but must contain the string OK. If the output doesn't contain both strings (usually when it's empty), the test failed. Our code for counting the tests which failed is like this:

today=`TZ='Asia/Tel_Aviv' date +"%Y-%m-%d"`
yesterday=`TZ='Asia/Tel_Aviv' date +"%Y-%m-%d" -d "yesterday"`

print_test_results()
{
    log_suffix="_${file_name}.log"
    yesterday_logs="${log_prefix}${yesterday}_[1,2]*${log_suffix}"
    today_logs="${log_prefix}${today}_0*${log_suffix}"
    passed_tests=`fgrep -L FAILED $yesterday_logs $today_logs 2>/dev/null | wc -l 2>/dev/null`
    failed_tests=`fgrep -l FAILED $yesterday_logs $today_logs 2>/dev/null | wc -l 2>/dev/null`
    total_tests=`ls -1 $yesterday_logs $today_logs 2>/dev/null | wc -l 2>/dev/null`
    echo "<tr>"
    echo "<td>$test_name - $today</td>"
    if [ $passed_tests = "0" ]; then
        echo "<td>$passed_tests passed</td>"
        echo "<td><span style=\"color: red;\">$failed_tests failed</span></td>"
    else
        echo "<td><span style=\"color: green;\">$passed_tests passed</span></td>"
        echo "<td>$failed_tests failed</td>"
    fi
    echo "<td>$total_tests tests total</td>"
    echo "</tr>"
}

但问题在于,它忽视了如果两个字符串不输出(例如,当该文件是空的)确定的字符串,它计数一次通过考试。我们如何才能正确计算通过试验和失败的测试?

But the problem is, it ignores the OK string if both strings are not in the output (for example when the file is empty), it counts a passed test. How can we count passed tests and failed tests correctly?

推荐答案

遍历所有的文件,并执行正确的检查。搜索字符串将超过由外壳计数产生的任何费用。

Loop over the files and perform a proper check. Searching the string will outweigh any overhead created by the shell counting.

declare -i fail=0
declare -i success=0

for fileName in *
do
  if grep -q FAILED "$fileName"
  then
    fail+=1
  elif grep -q OK "$fileName"
  then
    success+=1
  else
    fail+=1
  fi
done

printf "%d failed, %d succeeded\n" "$fail" "$success"

一个不同的方法是通过文件名列表。伯爵在在他们的失败。并的的发生,有确定多少个文件:

A different approach would be to pass file name lists. Count how many files in which FAILED does not occur, have OK in them:

grep -l OK $(grep -L FAILED *) | wc -l

数有多少个文件失败在其中或既不失败也不确定

(grep -l FAILED *; egrep -L 'FAILED|OK' *) | wc -l

计数方法将有奇怪的文件名(即空格或换行符的文件名等)的问题,但可能是你可以安全地假设你的特殊情况的文件名是体面的。

The counting approach will have problems with strange file names (i. e. whitespace or newlines in the file names etc.), but probably you can safely assume that in your special case the file name are decently.

这篇关于我怎么能算含有的特定字符串的文件的数量,但不包含另一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆