使用Sed查找和替换json字段

时间:2021-09-09 16:48:20

I have set of json files where after last key value pair i have comma which needs to be replaced.

我有一组json文件,其中在最后一个键值对后我有逗号需要被替换。

{
    "RepetitionTime": 0.72,
    "TaskName":"WM",
    "Manufacturer": "Siemens",
    "ManufacturerModelName": "Skyra",
    "MagneticFieldStrength": 3.0,
    "EchoTime":"0.033",
}

It should look like:

它应该看起来像:

{
    "RepetitionTime": 0.72,
    "TaskName":"WM",
    "Manufacturer": "Siemens",
    "ManufacturerModelName": "Skyra",
    "MagneticFieldStrength": 3.0,
    "EchoTime": 0.033
}

How can i achive this using sed. Edit: Changed output - There should not be any "" around 0.033.

我怎样才能使用sed实现这个目标。编辑:更改输出 - 应该没有任何“”大约0.033。

sed -i  \'7i'\\t'\"EchoTime\": \0.033\' sub-285345_task-WM_acq-RL_bold.json

is not helping me. I have tried few other options but no success..

没有帮助我。我尝试了其他一些选择,但没有成功..

I trioed using simplejson and json package in python too. But given that the files are incorrct json, json.loads(file) throws errors..

我也在python中使用simplejson和json包进行了三重奏。但鉴于文件是json,json.loads(文件)抛出错误..

I would prefer sed over python for now..

我现在更喜欢sed over python ..

3 个解决方案

#1


1  

 sed -Ei.bak 's/^([[:blank:]]*"EchoTime[^"]*":)"([^"]*)",$/\1\2/' file.json

will do it

会做的

Sample Output

{
    "RepetitionTime": 0.72,
    "TaskName":"WM",
    "Manufacturer": "Siemens",
    "ManufacturerModelName": "Skyra",
    "MagneticFieldStrength": 3.0,
    "EchoTime":0.033
}

Notes

  • E to enable extended regular expressions.
  • E启用扩展正则表达式。

  • i to enable inplace editing, a backup file with .bak extension is created.
  • 我要启用就地编辑,创建一个扩展名为.bak的备份文件。

#2


0  

Please try the following command.

请尝试以下命令。

sed -i 's#\(.*\)EchoTime":"\(.*\)",$#\1EchoTime":\2#' sub-285345_task-WM_acq-RL_bold.json

#3


0  

In case you are not limited to sed and open for awk , then following can be used :

如果您不限于sed并打开awk,则可以使用以下内容:

awk ' BEGIN{FS=OFS=":"}/EchoTime/ {gsub(/\"|\,/,"",$2)}1'  file.json
{
    "RepetitionTime": 0.72,
    "TaskName":"WM",
    "Manufacturer": "Siemens",
    "ManufacturerModelName": "Skyra",
    "MagneticFieldStrength": 3.0,
    "EchoTime":0.033
}

Explanation:

FS=OFS=":" : This will set input and o/p field separator as ":"

FS = OFS =“:”:这会将输入和o / p字段分隔符设置为“:”

/EchoTime/ : Search for the line containing EchoTime.

/ EchoTime /:搜索包含EchoTime的行。

/EchoTime/{gsub(/\"|\,/,"",$2)}: Once echo time is found use global sub to replace , double quotes and comma in second field of that line.

/ EchoTime / {gsub(/ \“| \,/,”“,$ 2)}:一旦找到回显时间,使用global sub替换,双引号和逗号在该行的第二个字段中。

1 : awk's default action is to print.

1:awk的默认动作是打印。

For making changes in original file:

要在原始文件中进行更改:

awk ' BEGIN{FS=OFS=":"}/EchoTime/ {gsub(/\"|\,/,"",$2)}1'  file.json >json.tmp && mv json.tmp file.json

#1


1  

 sed -Ei.bak 's/^([[:blank:]]*"EchoTime[^"]*":)"([^"]*)",$/\1\2/' file.json

will do it

会做的

Sample Output

{
    "RepetitionTime": 0.72,
    "TaskName":"WM",
    "Manufacturer": "Siemens",
    "ManufacturerModelName": "Skyra",
    "MagneticFieldStrength": 3.0,
    "EchoTime":0.033
}

Notes

  • E to enable extended regular expressions.
  • E启用扩展正则表达式。

  • i to enable inplace editing, a backup file with .bak extension is created.
  • 我要启用就地编辑,创建一个扩展名为.bak的备份文件。

#2


0  

Please try the following command.

请尝试以下命令。

sed -i 's#\(.*\)EchoTime":"\(.*\)",$#\1EchoTime":\2#' sub-285345_task-WM_acq-RL_bold.json

#3


0  

In case you are not limited to sed and open for awk , then following can be used :

如果您不限于sed并打开awk,则可以使用以下内容:

awk ' BEGIN{FS=OFS=":"}/EchoTime/ {gsub(/\"|\,/,"",$2)}1'  file.json
{
    "RepetitionTime": 0.72,
    "TaskName":"WM",
    "Manufacturer": "Siemens",
    "ManufacturerModelName": "Skyra",
    "MagneticFieldStrength": 3.0,
    "EchoTime":0.033
}

Explanation:

FS=OFS=":" : This will set input and o/p field separator as ":"

FS = OFS =“:”:这会将输入和o / p字段分隔符设置为“:”

/EchoTime/ : Search for the line containing EchoTime.

/ EchoTime /:搜索包含EchoTime的行。

/EchoTime/{gsub(/\"|\,/,"",$2)}: Once echo time is found use global sub to replace , double quotes and comma in second field of that line.

/ EchoTime / {gsub(/ \“| \,/,”“,$ 2)}:一旦找到回显时间,使用global sub替换,双引号和逗号在该行的第二个字段中。

1 : awk's default action is to print.

1:awk的默认动作是打印。

For making changes in original file:

要在原始文件中进行更改:

awk ' BEGIN{FS=OFS=":"}/EchoTime/ {gsub(/\"|\,/,"",$2)}1'  file.json >json.tmp && mv json.tmp file.json