如何组合多个awk命令并打印然后用空格



我正在使用cURL捕获URL内容,它以HTML格式提供输出。使用awk我正在捕获传感器名称及其状态。

(curl <MY URL> | awk -F"Sensor</th><td>" '{print $2}' | awk -F"</td></tr>" '{print $1}'; 
curl <my URL> | awk -F"Status</th><td><strong>" '{print $2}' | awk -F"</strong>" '{printf $1}' 
) | tr -d 'n' >> output

cURL输入,如

<html><head><title>Sensor status for NumberOfThreadsSensor-NumberOfThreads</title></head><body>
<h1>Sensor status for NumberOfThreadsSensor-NumberOfThreads</h1>
<table>
<tr><th>Plugin</th><td>NumberOfThreadsSensor</td></tr><tr><th>Sensor</th><td>NumberOfThreads</td></tr><tr><th>Status</th><td>Ok</td></tr><tr><th>Created</th><td>Fri Aug 14 09:03:10 UTC 2020 (13 seconds ago)</td></tr><tr><th>TTL</th><td>30 seconds</td></tr><tr><th>Short message</th><td>1;14;28</td></tr><tr><th>Long message</th><td>1 [interval: 1 min];14 [interval: 30 min];28 [interval: 60 min]</td></tr></table>
<h2>Formats</h2><p>The status shown on this page is also available in the following machine-friendly formats:</p>
<ul>
<li><a href="/admin/monitoring/NumberOfThreadsSensor-NumberOfThreads/status">A simple status string</a>, Possible values: OK, WARNING, CRITICAL, UNKNOWN.</li>
<li><a href="/admin/monitoring/NumberOfThreadsSensor-NumberOfThreads/nagios">Nagios plugin output</a>, output formatted for easy integration with Nagios.</li>
<li><a href="/admin/monitoring/NumberOfThreadsSensor-NumberOfThreads/xml">Full xml</a> all available data in xml for easy parsing by ad-hoc monitoring tools.</li>
<li><a href="/admin/monitoring/NumberOfThreadsSensor-NumberOfThreads/prometheus">Prometheus output</a>, all available data in prometheus format</li>
</ul>
<p>Please do not rely on the output of this page for automated monitoring, use one of the formats above.</p>
</body></html>

当前输出记分处理器警告

预期输出ScoreProcessor警告

请帮我简化我的shell脚本,我正处于学习阶段。感谢的帮助

输入保存在/tmp/input.txt:中

<h1>Sensor status for EventProcessorStatus-ScoreProcessor</h1>
<table>
<tr><th>Plugin</th><td>EventProcessorStatus</td></tr><tr><th>Sensor</th><td>ScoreProcessor</td></tr><tr><th>Status</th><td><strong>Warning</strong></td></tr><tr><th>Created</th><td>Fri Aug 10 00:16:23 UTC 2020 (0 seconds ago)</td></tr><tr><th>TTL</th><td>30 seconds</td></tr><tr><th>Short message</th><td>Endpoint is running, but has errors</td></tr><tr><th>Long message</th><td>Endpoint is running, but has errors<br/>
Number of errors in background process (xxxx) logs: 4<br/>
</td></tr></table>
<h2>Performance data</h2><table>

由于我对xmllint的了解非常有限,我最终得到了:

# Extract only table, get text from all tales
xmllint --html --xpath '//table//tr//text()' /tmp/input.txt |
# Because we know table has two rows, join two lines together
sed 'N;s/n/t/' |
# Filter Sensor and status only
sed -n '/Sensort/{s///;h}; /Statust/{s///;x;G;p}' |
# Read the sensor and status to bash
{ IFS= read -r name; IFS= read -r status; echo "name=$name status=$status" ;}

输出:

name=ScoreProcessor status=Warning

相关内容

  • 没有找到相关文章