shell - 更新 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 是正确的方法?我从未使用过它,但我愿意学习如何使用它来解决这个问题。理想情况下,这应该以某种方式从命令行完成。
谢谢!
解决方案
#!/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
推荐阅读
- python - 从 .py 转换为 .ipynb
- angular - AppModule 的 NgModule.imports 中位置 5 的值不是参考(构建错误 Angular2)
- qliksense - 是否可以生成 Anychart Qlik 扩展图表对象?
- typo3 - TYPO3 V10 - EXT:news 自定义类型和持久性映射
- cuda - 如何在 Google Colab 中添加头文件
- python - value_counts 在 DataFrame 中返回浮点数
- docker - 在运行 docker 的多台裸机机器上时 KSQLDB 集群失败
- reactjs - 如何将填充颜色传递给 recharts Pie 组件
- linux - 使用 Cron 和 Pgrep 重新启动服务会产生许多 pgrep 进程
- java - 获取目录java中的资源