简体   繁体   English

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

[英]How to combine multiple awk command and to print then with space

I'm capturing URL content using cURL which gives output in HTML format.我正在使用 cURL 捕获 URL 内容,它给出 output 格式为 Z4C4AD5FCA2E03A3F74ZDBBAACED。 Using awk I'm capturing sensor name and its status.使用 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 input like, 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>

Current output ScoreProcessorWarning当前 output ScoreProcessorWarning

expected output ScoreProcessor Warning预期 output ScoreProcessor 警告

Please help me to simplify my shell script and I'm in learning phase.请帮我简化我的 shell 脚本,我正处于学习阶段。 Thanks for help感谢帮助

With the input presented saved in /tmp/input.txt :输入保存在/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>

with my very limited knowledge of xmllint I ended with:由于我对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 '/Sensor\t/{s///;h}; /Status\t/{s///;x;G;p}' |
# Read the sensor and status to bash
{ IFS= read -r name; IFS= read -r status; echo "name=$name status=$status" ;}

which outputs:输出:

name=ScoreProcessor status=Warning

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM