Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用sed编辑JSON文件_Regex_Json_Bash_Sed_Jq_Tee_Perl - Fatal编程技术网

Regex 使用sed编辑JSON文件

Regex 使用sed编辑JSON文件,regex,json,bash,sed,jq,tee,perl,Regex,Json,Bash,Sed,Jq,Tee,Perl,我需要使用sed编辑一个JSON文件,向文件中添加一些数据。JSON如下所示: { 'name': // more attributes that are already filled in } 我已经编写了这个sed命令来尝试这样做: sed "s/(\'name\':)/\1\"hello\"\,/g" /path/to/file.json 然而,我不断地发现这个错误: sed: \1 not defined in the RE 预期结果如下: { 'name': "he

我需要使用
sed
编辑一个JSON文件,向文件中添加一些数据。JSON如下所示:

{
  'name':
  // more attributes that are already filled in 
}
我已经编写了这个
sed
命令来尝试这样做:

sed "s/(\'name\':)/\1\"hello\"\,/g" /path/to/file.json
然而,我不断地发现这个错误:

sed: \1 not defined in the RE
预期结果如下:

{
  'name': "hello",
  // more attributes here, left untouched
}

我知道这是一种不好的方法,但我不认为我能够使用这样的工具,因为文件将在服务器上编辑,而我无法在服务器上安装
jq
。如果有人有更好的解决方案,我会非常有兴趣听到的。谢谢大家!

你的反斜杠放错地方了
不需要转义,但括号需要转义。这项工作:

sed "s/\('name':\)/\1\"hello\"\,/g" /path/to/file.json

注意:我假设file.json不是一个json文件,而是一个精心编制的模板,否则这种替换就没有任何意义(结果不是json)。使用sed处理JSON文件肯定是一个坏主意,因此如果您试图修改JSON而不是生成它,请查看
jq
左右。

如您所述,
sed
不是正确的工具,请使用适当的JSON解析器:

输入json 使用: (使用小文件的最新版本)

或使用: 如果要在不在位编辑文件的情况下测试命令,请删除
-i
开关

输出json
使用python,在大多数POSIX系统上更容易实现:

#!/usr/bin/python
import json
jsonObject = json.load(filename)
jsonObject['name'] = "hello"
json.dump(filename, jsonObject)
然后运行它:

python parse_json.py
编辑:要在bash设置中使用此选项,请使用here文档:

#!/bin/sh

# ...

# set the name variable to something
name=VALUE

# ...

# and use it in a here document
parse_json_script=$(mktemp parse_json.XXXX.py)
cat > $parse_json_script << SCRIPT
#!/usr/bin/env python
import json
jsonObject = json.load(filename)
# You can use bash variables in here documents.
# the line below uses the bash variable `name` and inserts it into the python script
jsonObject['name'] = "$name"
print(jsonObject['name'])
json.dump(filename, jsonObject)
SCRIPT
python $parse_json_script && rm $parse_json_script
在python的
sh
(命令执行)、
os
(文件操作)、
shutil
(递归文件操作)和
re
(regex)模块之间,我发现bash的功能通常会被完全取代

sed "s/'name':/& \"hello\",/" File

几乎与Wintermutes的答案相似,但这里我们避免使用
(分组)
,因此我们使用
&(模式匹配)
,而不是
\1
。此外,如果每行中只出现一个
'name':
,则不需要使用
全局替换(g)
。不过,我不确定sed是否适合这项工作。我还不知道JSON:-)

对于级联语言(如JSON),更好的方法是从不使用正则表达式,而是使用上下文无关语法,甚至更好的方法是:使用特定的语言工具。特别是如果你希望你的工具是可靠的,这真的是一种糟糕的做法:最终它总会以这样或那样的方式出错。正如你所说的,这是一种糟糕的做法<代码>jq可以按原样下载并在没有依赖项的情况下执行。是否需要退出冒号?可能值得一试。同意上面关于不使用sed的观点,但并非所有的
sed
s都以相同的方式工作。您可能需要使用
's/\(\'name\':\)/\1….'
。祝你好运。不要使用正则表达式来解析JSON!如果file.json是json文件,则生成的文件将不是json。我认为他正在开发一个特定于用例的模板,这个模板是精心制作的,以使其正常运行。谢谢Wintermute,这太棒了。对反斜杠有点困惑@斯普特尼克,我理解这些担心,我也有点不安,但这不是一个重要的文件,如果出了什么问题,这不是一个大问题。我看过jq,但在这种情况下使用它是不实际的。答案不仅适用于你,问题/答案有自己的生活,如果不好的做法被接受,使用搜索引擎搜索的人会选择不好的做法。我添加了一些东西来说明这一点。同样,如果输出文件是有效的JSON,那么输入文件就不能是有效的JSON,因此“使用JSON工具”对这个问题不是一个有用的答案。感谢您的回答,sputnick将尝试在我的系统中实现这一点,它看起来比使用
sed
要好得多,我只是不确定替代方法。一直在探索这个选项,它似乎工作得很好,但是,我需要在一个更大的Bash脚本中运行它,
“hello”
值实际上是一个Bash变量。如何将Bash和Python结合起来?我在答案中添加了您要求的内容。感谢您的添加,它工作得很好:)我不得不稍微编辑一下您的Python,使其在我的系统上工作:
导入json
并将open('package.json',r+')作为f:
数据=json.load(f)
数据[“name”]=“$newstring”
f.seek(0)
json.dump(数据,f,缩进=4)
#!/usr/bin/python
import json
jsonObject = json.load(filename)
jsonObject['name'] = "hello"
json.dump(filename, jsonObject)
python parse_json.py
#!/bin/sh

# ...

# set the name variable to something
name=VALUE

# ...

# and use it in a here document
parse_json_script=$(mktemp parse_json.XXXX.py)
cat > $parse_json_script << SCRIPT
#!/usr/bin/env python
import json
jsonObject = json.load(filename)
# You can use bash variables in here documents.
# the line below uses the bash variable `name` and inserts it into the python script
jsonObject['name'] = "$name"
print(jsonObject['name'])
json.dump(filename, jsonObject)
SCRIPT
python $parse_json_script && rm $parse_json_script
#!/usr/bin/python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals 
import sh

# now you can call any command as if it were a python function
sh.ls("-lah") # `ls -lah`, but maybe you use python's `os` module instead
sh.rsync("-avHAX","foo","bar")
sed "s/'name':/& \"hello\",/" File