1

我试图摆脱 JSON 文件中的大量内容,以便使用 BBEdit 为翻译做好准备。

一条线看起来像这样:

"ribbonText1" : "The text that needs to be translated",

我想删除所有东西,所以它最终是这样的:

The text that needs to be translated

非常感谢任何帮助!!!

4

2 回答 2

0

首先正则表达式选择一个字符串,正则表达式sub删除除文本之外的所有内容。

$ cat r.sh
awk '
/"ribbonText1"[ \t]*:/ {
    sub(/^[^:]*:/, "")
    sub(/^[^"]*"/, "")
    sub(/"[^"]*$/, "")
    print
}
' "$@"

用法

$ sh r.sh file
The text that needs to be translated 
于 2018-02-06T18:17:44.530 回答
0

假设显示的示例是 JSON 对象的一部分,并且键名会有所不同,就像这样......

{
  "ribbonText1" : "The text that needs to be translated",
  "ribbonText2" : "The text that needs to be translated2",
  "ribbonText3" : "The text that needs to be translated3",
  "ribbonText4" : "The text that needs to be translated4",
  "ribbonText5" : "The text that needs to be translated5",
  "ribbonText6" : "The text that needs to be translated6"
}

BBEdit 中的这个正则表达式应该可以解决问题:

查找:^\s?[^:]+:\s?"([^"]+)",?$ 替换:\1

正如 Twometer 在评论中指出的那样,这很脆弱。我倾向于为此使用jq

cat foo.json | jq '.[]'

于 2018-02-08T19:45:33.263 回答