V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
VKMEPR
V2EX  ›  Linux

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

  •  
  •   VKMEPR · 2019-09-24 21:56:20 +08:00 · 4694 次点击
    这是一个创建于 1668 天前的主题,其中的信息可能已经有所发展或是发生改变。

    服务器上有多个 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>
    27 条回复    2019-10-03 11:03:51 +08:00
    VKMEPR
        1
    VKMEPR  
    OP
       2019-09-24 21:59:22 +08:00
    小白没有 shell 基础,网上搜索看了几页结果看得云里雾里,所以请教下各位大佬,任何帮助提前致谢!
    tao14
        2
    tao14  
       2019-09-24 22:01:22 +08:00 via Android
    awk
    VKMEPR
        3
    VKMEPR  
    OP
       2019-09-24 22:07:29 +08:00
    @tao14 可以麻烦写个示例用法吗?为这个问题折腾了一下午。。。
    Cooky
        4
    Cooky  
       2019-09-24 22:13:28 +08:00 via Android
    Python 省事
    sker101
        5
    sker101  
       2019-09-24 22:17:22 +08:00
    1. 为什么要只用 shell
    2. 就算找到可用方法大部分都是只是用 shell 跑别人写好的各种 cli
    3. 装个可以解 yaml 的语言 nodejs 或 python 甚至 php 自己写个自己操作不好?
    EscYezi
        6
    EscYezi  
       2019-09-24 22:17:27 +08:00 via iPad
    python+1 现在发行版都自带 python 环境的,直接用 python 吧
    upczww
        7
    upczww  
       2019-09-24 22:18:48 +08:00 via Android
    用 Python,从第二行开始读,split,把链接读到一个 list 里面,然后构造你要的格式。
    VKMEPR
        8
    VKMEPR  
    OP
       2019-09-24 22:19:08 +08:00
    @Cooky 有可以处理这种需求的脚本推荐吗?
    ChristopherWu
        9
    ChristopherWu  
       2019-09-24 22:19:59 +08:00   ❤️ 1
    可以。。太简单了。 关键词:bash read file,awk,echo, >
    ChristopherWu
        10
    ChristopherWu  
       2019-09-24 22:20:32 +08:00
    @VKMEPR #3 你这个问的太伸手党了,没有人会写给你的。
    VKMEPR
        11
    VKMEPR  
    OP
       2019-09-24 22:23:16 +08:00
    @ChristopherWu 可以付费解决吗?如果可以,麻烦报个价格,也不想做伸手党。。
    ChristopherWu
        12
    ChristopherWu  
       2019-09-24 22:34:08 +08:00
    @VKMEPR #11 这个问题不难,你先用 Google 或者必应,搜索一下我说的关键词,去先学习一下 shell 相关的东西吧。

    估计一天就学的不错了,然后最多 30 分钟不到写完。
    aheadlead
        13
    aheadlead  
       2019-09-24 22:42:24 +08:00
    算了 我来写一个吧
    aheadlead
        14
    aheadlead  
       2019-09-24 22:47:36 +08:00
    ant2017
        15
    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
        16
    5long  
       2019-09-24 23:05:21 +08:00   ❤️ 1
    解析结构化的数据,建议用能正确解析这种格式的库,不建议用取巧的办法。在这个例子里要解析 YAML,那么就找个能解析 YAML 的库。比如用 Python 装上 pyyaml https://pyyaml.org/ ,或者用 Ruby 标准库里的 yaml https://ruby-doc.org/stdlib-2.6.4/libdoc/yaml/rdoc/index.html

    至于说输出 HTML 的步骤,用模板语言或者自己拼字符串,都随意了。
    defunct9
        17
    defunct9  
       2019-09-25 01:29:23 +08:00 via iPhone
    yq
    ysc3839
        18
    ysc3839  
       2019-09-25 01:33:16 +08:00 via Android   ❤️ 1
    这样的数据让我选的话我还是选择用 Python 写。
    downdowndown30
        19
    downdowndown30  
       2019-09-25 01:41:29 +08:00 via Android
    @ChristopherWu 下面好多人给了😂
    UnknownR
        20
    UnknownR  
       2019-09-25 02:06:16 +08:00
    像 python,powershell 这种都有对应的库,比如直接导入成 yml 数据类型,然后转换成容易修改、读取的格式,比如 object,再选取想要的 value 和 name 用对应的库转换成 html。或者你就强行用 awk 或者其他脚本语言的 split 参数把想要的数据分离出来
    oneisall8955
        21
    oneisall8955  
       2019-09-25 06:43:19 +08:00 via Android
    略简单,正则匹配,分 2 组完事了
    Nitroethane
        22
    Nitroethane  
       2019-09-25 07:37:26 +08:00 via Android
    用 Python 解析出来后根据自己的需求拼接 markdown 字符串,然后渲染成 HTML
    msg7086
        23
    msg7086  
       2019-09-25 08:00:23 +08:00 via Android
    想起我司傻逼代码用 shell 调 awk 解析 XML。
    特别想打死那个写代码的人。
    VKMEPR
        24
    VKMEPR  
    OP
       2019-09-25 08:22:50 +08:00
    @aheadlead 真心感谢!
    VKMEPR
        25
    VKMEPR  
    OP
       2019-09-25 08:23:34 +08:00
    @ant2017 感谢大佬!
    VKMEPR
        26
    VKMEPR  
    OP
       2019-09-25 08:33:27 +08:00
    @ChristopherWu 感谢建议!会结合大佬们给建议和示例去理解用法
    OceanSea
        27
    OceanSea  
       2019-10-03 11:03:51 +08:00
    vim 都可以 录制宏
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2838 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:33 · PVG 21:33 · LAX 06:33 · JFK 09:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.