首页 > 解决方案 > 更新 html 文件和两个 JSON 文件 (unix) 中的文本

问题描述

我想想出一种同时更新三个文件的方法。这三个文件在同一个目录中。输入将是一个 id(键)、一个英文翻译和一个西班牙文翻译。

假设我们的三个文件看起来像这样

索引.html

<div id="foo">goodbye</div>

英语.json

{ "foo": "goodbye" }

西班牙语.json

{ "foo": "adios" }

假设我们希望这三个文件改为说“你好”。我们输入 'foo' 'hello' 'hola' 这就是文件的外观:

索引.html

<div id="foo">hello</div>

英语.json

{ "foo": "hello" }

西班牙语.json

{ "foo": "hola" }

也许 sed 是正确的方法?我从未使用过它,但我愿意学习如何使用它来解决这个问题。理想情况下,这应该以某种方式从命令行完成。

谢谢!

标签: shellunixsedscriptingcommand-line-interface

解决方案


#!/bin/sh
sed -i "s/\(.*<div id=\"$1\">\)[^<]*\(<.*\)/\1$2\2/g" index.html
sed -i "s/\(.*\"$1\":\).*/\1 \"$2\" }/g" english.json
sed -i "s/\(.*\"$1\":\).*/\1 \"$3\" }/g" spanish.json

用法:脚本名 KEY ENGLISH SPANISH


推荐阅读