1

I have a large json table and I need to add comm between json objects on it. I can not do this manually. The file structure looks like:

[{"host"
}
{"host"
}
{"host"
}
]

I need to modify it using sed to be like:

[{"host"
},
{"host"
},
{"host"
}
]

I executed the following command. I did not get any error. But nothing changed in the file:

sed -i 's/}{"host"/},{"host"/g' result.json 

I suspect that I should consider that }{ are separated by new line? I tried to add \n in the command but also did not work.

user9371654
  • 2,160
  • 16
  • 45
  • 78
  • 1
    I think it'd be better to have whatever creates that first file do so as valid json in the first place... – Shawn Aug 19 '18 at 19:24
  • @Shawn but I'm not the author of the tool. – user9371654 Aug 19 '18 at 19:26
  • The proposed modification is still insufficient to produce valid JSON. – peak Aug 20 '18 at 03:19
  • Possible duplicate of [Match a string that contains a newline using sed](https://stackoverflow.com/q/23850789/608639), [sed to match pattern across a newline](https://stackoverflow.com/q/35278680/608639), etc. – jww Aug 20 '18 at 03:20

2 Answers2

1

With GNU awk for multi-char RS:

$ awk -v RS='^$' -v ORS= '{gsub(/}\n{/,"},\n{")}1' file
[{"host"
},
{"host"
},
{"host"
}
]

or if you prefer:

$ awk -v RS='}\n{' '{ORS=(RT ? "},\n{" : "")} 1' file
[{"host"
},
{"host"
},
{"host"
}
]

or with GNU sed for -z and to recognize \n as a newline:

$ sed -z 's/}\n{/},\n{/g' file
[{"host"
},
{"host"
},
{"host"
}
]
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • `sed -z 's/}\n{/},\n{/g' file` works in a moc example. But when I tried the real file .json it does not work. What do you mean by GNU ? I use Ubuntu 18.04 – user9371654 Aug 24 '18 at 14:28
  • GNU - https://www.gnu.org/. I expect you can have GNU tools (e,.g. GNU sed, GNU awk, GNU sort, etc.) on Ubuntu just like you can every other UNIX-like platform. Run `sed --version` and the output will tell you if it's GNU sed or not - the script might work with other seds too, I just know it does work with GNU sed but won't work with all seds. If the script works on the example you provided but not on your real data then that means the script works as intended but your example isn't representative of your real data. – Ed Morton Aug 24 '18 at 14:37
  • I copied the first two records from the real file in a small file. I run the command and it works. But when I run it in the real data file (it is a json file), the comma is not added. I can not post the real file it is too long. I copied the first two records in a separate file and it works. What can be the problem? – user9371654 Aug 24 '18 at 14:39
  • Could be anything. Do the usual divide-and-conquer debugging steps (`WHILE (command is failing) DO cut the file in half, choose a half for the next iteration DONE`) to find a small section of your real input file that the command doesn't work for and then think about in what way that's different from a part that it does work for. – Ed Morton Aug 24 '18 at 15:57
0

Instead of trying to insert the comma before the newline, you could easily insert it after:

sed 's/^{"host"/,&/'

The result looks a bit odd, but a conformant JSON-reader shouldn't care.

Michael Dyck
  • 2,153
  • 1
  • 14
  • 18