我正在尝试将YAML文件转换为HTML表,它涉及多个复杂条件,我知道可以使用Shell脚本来完成此操作,但是我在实现过程中遇到了一些问题,因此请向社区寻求帮助。
YAML内容格式如下。
- 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... >
它记录了多个软件的历史版本,我需要将其转换为HTML表代码并将其分别输出到文件中。
例如,输出soft1
,soft1_beta_ver
和soft1_alpha_ver
到同一文件(文件名使用soft1
),将soft2输出到另一个文件。
需要转换的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>
这是我正在尝试的shell脚本,我不知道如何将输出分成多个文件,以及如何获取软件类型的变量。
#!/usr/bin/env bash
cat << EOF
<table>
<thead>
<tr>
<th>type</th>
<th>ver</th>
<th>link</th>
</tr>
</thead>
<tbody>
EOF
while IFS=": " read -r softver softlink
do
cat << EOF
<tr>
<td>$softver</td>
<td></td>
<td><a href="$softlink">download</a></td>
</tr>
EOF
done
cat << EOF
</tbody>
</table>
EOF
对此的任何帮助或建议都将非常有帮助,也将不胜感激。
只需从原始输入文件生成所需的HTML:
$ cat ../tst.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
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>")
}
。
$ ls
$
$ awk -f ../tst.awk ../file
$
$ ls
soft1 soft2
。
$ cat soft1
<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>
。
$ cat soft2
<table>
<thead>
<tr>
<th>type</th>
<th>ver</th>
<th>link</th>
</tr>
</thead>
<tbody>
<tr>
<td>soft2</td>
<td>V1.0.1</td>
<td>http://example.com/v1.0.1.zip</td>
</tr>
<tr>
<td>soft2</td>
<td>V1.0.2</td>
<td>http://example.com/v1.0.2.zip</td>
</tr>
<tr>
<td>soft2</td>
<td>V1.0.3</td>
<td>http://example.com/v1.0.3.zip</td>
</tr>
<tr>
<td>soft2_beta_ver</td>
<td>V1.0.1</td>
<td>http://example.com/v1.0.1.zip</td>
</tr>
<tr>
<td>soft2_beta_ver</td>
<td>V1.0.2</td>
<td>http://example.com/v1.0.2.zip</td>
</tr>
<tr>
<td>soft2_beta_ver</td>
<td>V1.0.3</td>
<td>http://example.com/v1.0.3.zip</td>
</tr>
<tr>
<td>soft2_alpha_ver</td>
<td>V1.0.1</td>
<td>http://example.com/v1.0.1.zip</td>
</tr>
<tr>
<td>soft2_alpha_ver</td>
<td>V1.0.2</td>
<td>http://example.com/v1.0.2.zip</td>
</tr>
<tr>
<td>soft2_alpha_ver</td>
<td>V1.0.3</td>
<td>http://example.com/v1.0.3.zip</td>
</tr>
</tbody>
</table>
上面是针对此输入文件运行的:
$ cat ../file
- 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句