我如何使用 grep 或 awk 来计算违反 ScanJS 规则的情况,以协助文本文件报告中的安全审查?

How can I use grep or awk to count violations of ScanJS rules to assist with security reviews in a text file report?

我正在尝试使用 grep and/or awk 创建一个脚本来回答下面列出的问题(请参阅图片以供视觉参考)

  1. 标题为“results.txt”的巨型文本文件中有多少行包含文本“scanjs-rules/”?
  2. “results.txt”中有多少行与图 2 中的第一个条目匹配?
  3. 其中有多少行与图 2 中的第二个条目匹配? (继续到 108 条规则列表的末尾)
  4. 我们如何创建图 3 中列出的示例报告?

动机: ESLint 生成的报告解决了每个单独的文件和每个文件的潜在违规行为——但据我所知,我无法生成任何统计数据来帮助某人对此执行安全代码审查。

注意:我使用 ScanJS 的 ESLint 配置文件来生成这些文本文件报告,但我想让它们更易于阅读。

编辑

我对规则进行了格式化,使每个规则看起来像这样: accidental_assignment

而不是:
"scanjs-rules/accidental_assignment": 1

然后我运行这个命令:

while read l; do grep -i "${l//\"/}" results.txt; done < rules.txt

关于此的好处:它按字母顺序打印出每个违反规则的情况,并提供标识符以帮助您查看 results.txt 文件。

结果图像:link

期望的改进:我仍然希望它打印出实际规则,然后打印出计数 - 如图 3 所示。有什么建议吗?

试试这个。

cat results.txt | grep scanjs-rules | wc
  • cat 将显示 results.txt
  • 的内容
  • grep 将 select 那些 包含 "scanjs-rules"
  • 的行
  • wc 会计算字数,第一个数字是你的答案的行数

您应该能够使用此命令计算包含 searched 字符串的行数:

cat results.txt | grep <searched> | wc -l 

一起

for f in `sed -e 's/"\(.*\/\)\(.*\)\(".*\)//' pattern.txt`;
  do printf $f' - '; cat result.txt | grep $f | wc -l;
done

将打印出类似

的结果
scanjs-rules/accidental_assignment - 2
scanjs-rules/assign_to_hostname - 2
scanjs-rules/assign_to_href - 4

哪里

$ cat pattern.txt
"scanjs-rules/accidental_assignment":1,
"scanjs-rules/assign_to_hostname":1,
"scanjs-rules/assign_to_href":1,

和结果文件示例:

$cat result.txt
This is dummy line
312:9 warrning from scanjs-rules/accidental_assignment
Another dummy line
Another dummy line
Another dummy line
312:9 warrning from scanjs-rules/assign_to_hostname
312:9 warrning from scanjs-rules/accidental_assignment
Another dummy line
312:9 warrning from scanjs-rules/assign_to_href
Another dummy line
312:9 warrning from unsafe scanjs-rules/assign_to_hostname
312:9 warrning from scanjs-rules/assign_to_href
312:9 warrning from scanjs-rules/assign_to_href
312:9 warrning from scanjs-rules/assign_to_href