我把这个放在档案里:
<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>'还有一些其他的转义组合,例如,只对引号进行转义。尝试在引号中定义变量,等等。
对我搞砸的地方有什么想法吗?
‘
发布于 2015-07-07 09:46:58
问题不在于正则表达式,而在于你试图与之匹配的内容。
[[ /tmp/result.html =~ $REGEX_EXPR ]]这意味着字符串/tmp/result.html是匹配的,而不是文件的内容。要逐行匹配,您需要一个循环:
while read line ; do
if [[ "$line" =~ $REGEX ]] ; then
...
fi
done < /tmp/result.htmlhttps://stackoverflow.com/questions/31264751
复制相似问题