Home > Mobile >  add comma between opening and closing braces in bash/python
add comma between opening and closing braces in bash/python

Time:11-04

I need to add comma between the two curly braces except last one.

Below is the input:

{"id": "1", "eventName": "Pause", "eventTime": "2022-11-02T21:00:42Z"}{"id": "2", "eventName": "Resume", "eventTime": "2022-11-03T14:18:47Z"}{"id": "7", "eventName": "Pause", "eventTime": "2022-11-02T18:15:48Z"}{"id": "3", "eventName": "Resume", "eventTime": "2022-11-03T08:01:19Z"}{"id": "4", "eventName": "Resume", "eventTime": "2022-11-03T06:34:42Z"}{"id": "6", "eventName": "Pause", "eventTime": "2022-11-02T23:01:07Z"}{"id": "5", "eventName": "Resume", "eventTime": "2022-11-03T03:01:05Z"}

I tried sed like sed 's/"\[\[}{\]\]\\ "/","/g' but no luck

expected output:

{"id": "1", "eventName": "Pause", "eventTime": "2022-11-02T21:00:42Z"},{"id": "2", "eventName": "Resume", "eventTime": "2022-11-03T14:18:47Z"},{"id": "7", "eventName": "Pause", "eventTime": "2022-11-02T18:15:48Z"},{"id": "3", "eventName": "Resume", "eventTime": "2022-11-03T08:01:19Z"},{"id": "4", "eventName": "Resume", "eventTime": "2022-11-03T06:34:42Z"},{"id": "6", "eventName": "Pause", "eventTime": "2022-11-02T23:01:07Z"},{"id": "5", "eventName": "Resume", "eventTime": "2022-11-03T03:01:05Z"}

CodePudding user response:

You should use string.replace() method

input_string = """
{"id": "1", "eventName": "Pause", "eventTime": "2022-11-02T21:00:42Z"}{"id": "2", "eventName": "Resume", "eventTime": "2022-11-03T14:18:47Z"}{"id": "7", "eventName": "Pause", "eventTime": "2022-11-02T18:15:48Z"}{"id": "3", "eventName": "Resume", "eventTime": "2022-11-03T08:01:19Z"}{"id": "4", "eventName": "Resume", "eventTime": "2022-11-03T06:34:42Z"}{"id": "6", "eventName": "Pause", "eventTime": "2022-11-02T23:01:07Z"}{"id": "5", "eventName": "Resume", "eventTime": "2022-11-03T03:01:05Z"}
"""

print(x)
output_string = x.replace("}{", "},{")
print(output_string)

CodePudding user response:

You seem to be over-complicating it. It's simply:

sed 's/}{/},{/g'
  • Related