I’m trying to parse some JSON and am running into an issue with parsing. My objects are:
{
"foo" : "bar"
}
{
"spam" : "ham"
}
I want to change this in a shell script to:
{
"foo" : "bar"
},
{
"spam" : "ham"
}
I’ve tried:
sed 's/}n{/},n{/g' file.json > file.json.tmp
and solutions like Replace curly braces separated by a newline with sed But without any progress. Any clue what I am missing here?
2
Answers
Using
perl
is easier :Or if you have gnu-sed :
Using GNU sed for
-z
to read the whole input into memory at once as a single multi-line record:or doing the same using any awk in any shell on every Unix box:
The default sed on MacOS is BSD, not GNU, so the above sed command won’t work unless you install GNU sed. The awk command will work on MacOS or anywhere else.
Your sed command in the question couldn’t find
n
in the string it’s working on because sed works on 1n
-separated string at a time by default and so the string it’s working on can’t containn
as it’s separated from the previous and next strings byn
.