【问题标题】:Updating text in an html file and two JSON files (unix)更新 html 文件和两个 JSON 文件 (unix) 中的文本
【发布时间】:2019-08-07 02:08:30
【问题描述】:

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

假设我们的三个文件如下所示

index.html

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

english.json

{ "foo": "goodbye" }

西班牙文.json

{ "foo": "adios" }

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

index.html

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

english.json

{ "foo": "hello" }

西班牙文.json

{ "foo": "hola" }

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

谢谢!

【问题讨论】:

  • grymoire.com/Unix/Sed.html |也许 sed 是正确的方法? - 什么是“正确”或“错误”是基于意见的问题之王。可以使用三个 sed 命令。

标签: shell unix sed scripting command-line-interface


【解决方案1】:
#!/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

【讨论】:

  • 谢谢!我运行它时遇到这些错误,有什么想法吗?我是 sed 的新手 sed: 1: "public/index.html": extra characters at the end of p command sed: 1: "english.json": invalid command code e sed: 1: "spanish.json": unterminated substitute pattern
猜你喜欢
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多