我有一个python脚本,它使用正则表达式从日志文件中取回数据,同时运行脚本,我得到一个错误sre_constants.error: unbalanced parenthesis
。python正则表达式不平衡括号错误
下面是我尝试用户正则表达式的脚本。
#!/grid/common/pkgs/python/v2.7.10/bin/python
import sys
import re
var1 = ''
var2 = ''
Html_file= open("/home/karn/healthCheck_result.html","w")
html_str = """
<table border=1>
<tr>
<th bgcolor=fe9a2e>Hostname</th>
<th bgcolor=fe9a2e>Service</th>
</tr>
"""
Html_file.write(html_str)
fh=open(sys.argv[1],"r")
for line in fh:
pat_match=re.match("^\s+\HostName:\s+(.*?)\.*", line)
pat_match1=re.match("^\s+(.*?\)Service Status:\s+(.*Not.*?)\.*", line)
if pat_match:
Html_file.write("""<TR><TD bgcolor=fe9a2e>""" + pat_match.group(1) + """</TD>\n""")
elif pat_match1:
Html_file.write("""<TR><TD><TD>""" + pat_match1.group(2) + """</TD></TD></TR>\n""")
我的日志文件具有以下示例数据:如果我在寻找的“不”字符串,如果获取然后打印整行与
Service Status
开始。
[analytics1] sudo: /hm/it_script/DC/scripts/mainRun.py
[analytics1] out: sudo password:
[analytics1] out: HostName: analytics1
[analytics1] out: Service Status: NTP Service is Running On the host analytics1
[analytics1] out: Service Status: NSCD Service is Not Running On the host analytics1
[analytics1] out: Service Status: Sendmail Service is Running On the host analytics1
[analytics1] out: Service Status: Altris Service is Running On the host analytics1
[analytics1] out: Service Status: Automount Service is Running On the host analytics1
[analytics1] out: Service Status: Filesystem For Root(/) is more than 90% On the Host analytics1
[analytics1] out: Service Status: Filesystem For /var is more than 90% On the Host analytics1
能否请您指教什么是错在这里..
SpoonMeiser ....感谢错字抓,但我无法得到它只是打印..header所需的数据仅 '的bash-4.1 $更healthCheck_result.html <表边框= 1>
SpoonMeiser,你的答案是正确的.. 我们可以得到主机名左手pannelled只有一次,下面那该主机的服务的服务状态AME。 我有html文件,但我找不到在这里附上。 – krock1516
我认为这里没有回答的范围。也许问一个新问题? – SpoonMeiser