可以用 shell 脚本实现这个功能吗?

2019-09-24 21:56:20 +08:00
 VKMEPR

服务器上有多个 yml 数据文件,想转换为 HTML 表格文件。原数据不是很复杂,有两列数据,具体如下:

yml 文件内容

date:
  1.0.1: http://example.com/1.0.1.tgz
  1.0.2: http://example.com/1.0.2.tgz
  1.0.3: http://example.com/1.0.3.tgz
  1.0.4: http://example.com/1.0.4.tgz
  1.0.5: http://example.com/1.0.5.tgz
  1.0.6: http://example.com/1.0.6.tgz
  1.0.7: http://example.com/1.0.7.tgz
  1.0.8: http://example.com/1.0.8.tgz
  1.0.9: http://example.com/1.0.9.tgz
  1.0.10: http://example.com/1.0.10.tgz

需要转换表格格式,有几个地方需要操作:1. 忽略第一行 date:字符串,2. 剩下的版本号和下载链接分两列,3. 网址添加超链接标签。

<table>
	<thead>
		<tr>
			<th>ver</th>
			<th>link</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>1.0.1</td>
			<td><a href="http://example.com/1.0.1.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.2</td>
			<td><a href="http://example.com/1.0.2.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.3</td>
			<td><a href="http://example.com/1.0.3.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.4</td>
			<td><a href="http://example.com/1.0.4.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.5</td>
			<td><a href="http://example.com/1.0.5.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.6</td>
			<td><a href="http://example.com/1.0.6.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.7</td>
			<td><a href="http://example.com/1.0.7.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.8</td>
			<td><a href="http://example.com/1.0.8.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.9</td>
			<td><a href="http://example.com/1.0.9.tgz">file</a></td>
		</tr>
		<tr>
			<td>1.0.10</td>
			<td><a href="http://example.com/1.0.10.tgz">file</a></td>
		</tr>
	</tbody>
</table>
4767 次点击
所在节点    Linux
27 条回复
VKMEPR
2019-09-24 21:59:22 +08:00
小白没有 shell 基础,网上搜索看了几页结果看得云里雾里,所以请教下各位大佬,任何帮助提前致谢!
tao14
2019-09-24 22:01:22 +08:00
awk
VKMEPR
2019-09-24 22:07:29 +08:00
@tao14 可以麻烦写个示例用法吗?为这个问题折腾了一下午。。。
Cooky
2019-09-24 22:13:28 +08:00
Python 省事
sker101
2019-09-24 22:17:22 +08:00
1. 为什么要只用 shell
2. 就算找到可用方法大部分都是只是用 shell 跑别人写好的各种 cli
3. 装个可以解 yaml 的语言 nodejs 或 python 甚至 php 自己写个自己操作不好?
EscYezi
2019-09-24 22:17:27 +08:00
python+1 现在发行版都自带 python 环境的,直接用 python 吧
upczww
2019-09-24 22:18:48 +08:00
用 Python,从第二行开始读,split,把链接读到一个 list 里面,然后构造你要的格式。
VKMEPR
2019-09-24 22:19:08 +08:00
@Cooky 有可以处理这种需求的脚本推荐吗?
ChristopherWu
2019-09-24 22:19:59 +08:00
可以。。太简单了。 关键词:bash read file,awk,echo, >
ChristopherWu
2019-09-24 22:20:32 +08:00
@VKMEPR #3 你这个问的太伸手党了,没有人会写给你的。
VKMEPR
2019-09-24 22:23:16 +08:00
@ChristopherWu 可以付费解决吗?如果可以,麻烦报个价格,也不想做伸手党。。
ChristopherWu
2019-09-24 22:34:08 +08:00
@VKMEPR #11 这个问题不难,你先用 Google 或者必应,搜索一下我说的关键词,去先学习一下 shell 相关的东西吧。

估计一天就学的不错了,然后最多 30 分钟不到写完。
aheadlead
2019-09-24 22:42:24 +08:00
算了 我来写一个吧
aheadlead
2019-09-24 22:47:36 +08:00
ant2017
2019-09-24 22:54:54 +08:00
awk -F ': ' '$2 ~ /tgz$/ {print "<tr>\n<td>"$1"</td>\n<td><a href=\""$2"\">file</a></td>\n</tr>"}' filename
5long
2019-09-24 23:05:21 +08:00
解析结构化的数据,建议用能正确解析这种格式的库,不建议用取巧的办法。在这个例子里要解析 YAML,那么就找个能解析 YAML 的库。比如用 Python 装上 pyyaml https://pyyaml.org/ ,或者用 Ruby 标准库里的 yaml https://ruby-doc.org/stdlib-2.6.4/libdoc/yaml/rdoc/index.html

至于说输出 HTML 的步骤,用模板语言或者自己拼字符串,都随意了。
defunct9
2019-09-25 01:29:23 +08:00
yq
ysc3839
2019-09-25 01:33:16 +08:00
这样的数据让我选的话我还是选择用 Python 写。
downdowndown30
2019-09-25 01:41:29 +08:00
@ChristopherWu 下面好多人给了😂
UnknownR
2019-09-25 02:06:16 +08:00
像 python,powershell 这种都有对应的库,比如直接导入成 yml 数据类型,然后转换成容易修改、读取的格式,比如 object,再选取想要的 value 和 name 用对应的库转换成 html。或者你就强行用 awk 或者其他脚本语言的 split 参数把想要的数据分离出来

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/603853

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX