我在文件中有这个:
<tr class="LightRow Center" style="height:auto;">
<td class="SmallText resultbadB" title="Non-Compliant/Vulnerable/Unpatched" style="width:20%">0</td>
<td class="SmallText resultgoodB" title="Compliant/Non-Vulnerable/Patched" style="width:20%">1</td>
<td class="SmallText errorB" title="Error" style="width:20%">0</td>
<td class="SmallText unknownB" title="Unknown" style="width:20%">0</td>
<td class="SmallText otherB" title="Inventory/Miscellaneous class, or Not Applicable/Not Evaluated result" style="width:20%">0</td>
</tr>
</table>
我想从这一行得到文字:
<td class="SmallText resultbadB" title="Non-Compliant/Vulnerable/Unpatched" style="width:20%">0</td>
这是一个shell脚本,我正在尝试使用bash正则表达式。
我试过这个shell脚本
#!/bin/bash
set -x
REGEX_EXPR='\<td\ class=\"SmallText\ resultbadB\"\ title=\"Non-Compliant\/Vulnerable\/Unpatched\"\ style=\"width\:20\%\"\>\(.*\)\</td\>'
[[ /tmp/result.html =~ $REGEX_EXPR ]]
echo "output $?"
echo ${BASH_REMATCH[0]}
echo ${BASH_REMATCH[1]}
但是我在echo "output $?"
上得到了一个不匹配的回复(1)我也试过了以下的正则表达式。
REGEX_EXPR='<td class="SmallText resultbadB" title="Non-Compliant/Vulnerable/Unpatched" style="width:20%">\(.*\)</td>'
REGEX_EXPR='<td class="SmallText resultbadB" title="Non-Compliant/Vulnerable/Unpatched" style="width:20%">(.*)</td>'
其他一些逃避组合,例如,仅仅引用了逃脱。试图用引号定义变量等等。
关于我搞砸的地方的任何想法?
答案 0 :(得分:1)
问题不在正则表达式中,而在于您尝试将其与之匹配。
[[ /tmp/result.html =~ $REGEX_EXPR ]]
这意味着匹配字符串/tmp/result.html
,而不是文件的内容。要逐行匹配,您需要一个循环:
while read line ; do
if [[ "$line" =~ $REGEX ]] ; then
...
fi
done < /tmp/result.html