我有一个awk脚本,可将yaml转换为html表,其工作方式如下。
YAML文件内容:
3/10/2019 6:00
AWK脚本内容:
- soft1:
V1.0.1: http://example.com/v1.0.1.zip
V1.0.2: http://example.com/v1.0.2.zip
V1.0.3: http://example.com/v1.0.3.zip
- soft1_beta_ver:
V1.0.1: http://example.com/v1.0.1.zip
V1.0.2: http://example.com/v1.0.2.zip
V1.0.3: http://example.com/v1.0.3.zip
- soft1_alpha_ver:
V1.0.1: http://example.com/v1.0.1.zip
V1.0.2: http://example.com/v1.0.2.zip
V1.0.3: http://example.com/v1.0.3.zip
- soft2:
V1.0.1: http://example.com/v1.0.1.zip
V1.0.2: http://example.com/v1.0.2.zip
V1.0.3: http://example.com/v1.0.3.zip
- soft2_beta_ver:
V1.0.1: http://example.com/v1.0.1.zip
V1.0.2: http://example.com/v1.0.2.zip
V1.0.3: http://example.com/v1.0.3.zip
- soft2_alpha_ver:
V1.0.1: http://example.com/v1.0.1.zip
V1.0.2: http://example.com/v1.0.2.zip
V1.0.3: http://example.com/v1.0.3.zip
< Omit more... >
通过使用#!/usr/bin/env awk
/^-/ {
sub(/:$/,"")
out = type = $NF
sub(/_.*/,"",out)
close(out)
if ( !seen[out]++ ) {
prtBeg()
}
next
}
{
sub(/:$/,"",$1)
prtElt("<tr>")
prtElt("<td>" type "</td>")
prtElt("<td>" $1 "</td>")
prtElt("<td>" $2 "</td>")
prtElt("</tr>")
}
END {
for (out in seen) {
prtEnd()
}
}
function prtElt(str) {
depth[out] += gsub("<[^/<>]+>","&",str)
printf "%*s%s\n", (depth[out]-1)*4, "", str > out".html"
depth[out] -= gsub("</[^<>]+>","&",str)
}
function prtBeg() {
prtElt("<table>")
prtElt("<thead>")
prtElt("<tr>")
prtElt("<th>type</th>")
prtElt("<th>ver</th>")
prtElt("<th>link</th>")
prtElt("</tr>")
prtElt("</thead>")
prtElt("<tbody>")
}
function prtEnd() {
prtElt("</tbody>")
prtElt("</table>")
}
命令,它将分别输出到多个文件,并且内容看起来像这样。
cat xxx.yml | awk -f xxx.awk
我想用自定义字符串替换表中的“ [root@localhost html]# ls
soft1.html soft2.html test.awk test.yml
[root@localhost html]# cat soft1.html
<table>
<thead>
<tr>
<th>type</th>
<th>ver</th>
<th>link</th>
</tr>
</thead>
<tbody>
<tr>
<td>soft1</td>
<td>V1.0.1</td>
<td>http://example.com/v1.0.1.zip</td>
</tr>
<tr>
<td>soft1</td>
<td>V1.0.2</td>
<td>http://example.com/v1.0.2.zip</td>
</tr>
<tr>
<td>soft1</td>
<td>V1.0.3</td>
<td>http://example.com/v1.0.3.zip</td>
</tr>
<tr>
<td>soft1_beta_ver</td>
<td>V1.0.1</td>
<td>http://example.com/v1.0.1.zip</td>
</tr>
<tr>
<td>soft1_beta_ver</td>
<td>V1.0.2</td>
<td>http://example.com/v1.0.2.zip</td>
</tr>
<tr>
<td>soft1_beta_ver</td>
<td>V1.0.3</td>
<td>http://example.com/v1.0.3.zip</td>
</tr>
<tr>
<td>soft1_alpha_ver</td>
<td>V1.0.1</td>
<td>http://example.com/v1.0.1.zip</td>
</tr>
<tr>
<td>soft1_alpha_ver</td>
<td>V1.0.2</td>
<td>http://example.com/v1.0.2.zip</td>
</tr>
<tr>
<td>soft1_alpha_ver</td>
<td>V1.0.3</td>
<td>http://example.com/v1.0.3.zip</td>
</tr>
</tbody>
</table>
”字符串,例如将type
替换为soft1
,将Release version
替换为{{1} },soft1_beta_ver
替换为Beta version
。
我该怎么做?任何帮助都先谢谢您!
答案 0 :(得分:0)
将此添加到顶部:
BEGIN {
map["soft1"] = "Release version"
map["soft1_beta_ver"] = "Beta version"
map["soft1_alpha_ver"] = "Alpha version"
}
并更改这两行:
out = type = $NF
sub(/_.*/,"",out)
收件人:
out = type = $NF
if (type in map) {
type = map[type]
}
sub(/_.*/,"",out)
如果您要进行更多的文本操作,请阅读Arnold Robbins的《有效Awk编程》,第4版,以学习如何使用awk。