bash - How can I count the number of files containing a specific string but not containing another string? -


we run daily selenium tests test our website , extensions. if test fails, output contains string failed (it doesn't matter if contains other strings). if passes, output doesn't contain string failed must contain string ok. if output doesn't contain both strings (usually when it's empty), test failed. our code counting tests failed 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" ];         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 problem is, ignores ok string if both strings not in output (for example when file empty), counts passed test. how can count passed tests , failed tests correctly?

loop on files , perform proper check. searching string outweigh overhead created shell counting.

declare -i fail=0 declare -i success=0  filename in *   if grep -q failed "$filename"       fail+=1   elif grep -q ok "$filename"       success+=1   else     fail+=1   fi done  printf "%d failed, %d succeeded\n" "$fail" "$success" 

a different approach pass file name lists. count how many files in failed not occur, have ok in them:

grep -l ok $(grep -l failed *) | wc -l 

count how many files have failed in them or neither failed nor ok:

(grep -l failed *; egrep -l 'failed|ok' *) | wc -l 

the counting approach have problems strange file names (i. e. whitespace or newlines in file names etc.), can safely assume in special case file name decently.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -