I have a multiline string containing some text followed by a JSON, so it has the following format:
Some random text
It spans across multiple lines and contains a JSON that does not start at the beginning of the line:
MY_JSON: {
"foo": [
{
"bar": "baz",
(...) // more content here
}
]
}
I want to extract the JSON using sed by removing the text before, so everything until (and including) MY_JSON: (note the trailing space).
My current solution:
# $str contains above multiline string
$ echo $str | sed '/MY_JSON: /d'
I get the following output:
Some random text
It spans across multiple lines and contains a JSON that does not start at the beginning of the line:
"foo": [
{
"bar": "baz",
(...) // more content here
}
]
}
But I want the following output:
{
"foo": [
{
"bar": "baz",
(...) // more content here
}
]
}
So the idea is to select everything until the first occurrence of { and delete it. But that doesn't work. It doesn't delete the first n lines until the line where the pattern matches. It also deletes the whole line instead of just the part until the {.
How can I achive best with sed what I want to do?