On Windows, I am trying to do an in-place edit of simple JSON files that are malformed. My files look like this, with first object duplicated.
{
"hello":"there"
}
{
"hello":"there"
}
My goal is to have only one object, and discard the second one. I should note that the files have linefeeds, as shown here.
End file should look like
{
"hello":"there"
}
I can match the first group using a regexp like ^({.*?}).*
. This is fairly simple.
Seems like a perfect job for sed in-place editing. But apparently no matter what combination of escaping I do in sed, I can’t match the brackets. I am using (GNU sed) 4.9
patch by Michael M. Builov.
Some results I get:
# as per original regexp
sed.exe -E "s,^({.*?}).*,d,g" double.json
-> -e expression #1, char 16: Invalid preceding regular expression
# trying to escape paranthesis
sed.exe -E "s,^({.*?}).*,d,g" double.json
-> -e expression #1, char 18: Invalid content of {}
# escaping curly brackets
sed.exe -E "s,^({.*?}).*,d,g" double.json
-> Works, but original file is returned (no match)
Is it possible at all on Windows ? According to this and this comment it seems that Windows for some reason does not like curly brackets with sed.
Note: tested in WSL/Ubuntu, and got same result.
2
Answers
You can try this GNU
sed
jq
command line tool is handy to use for JSON values, even on Windows. You can download by this link as an example.Then save the command
into a file called
double.jq
, and then call from command line by[Windows key]+ cmd
if there are more than two independent objects within the file
double.json
such asand want to pick the first one only, then convert code of the
double.jq
to