Update: unexpectedly grep
is sadly able to grep over multiple lines. See some other answers. And jq
is realy tje right tool for the job.
Nonetheless, here is an awk
solution :
$ awk '/]/{p=0}p{print}/test1/{p=1}' test
"test_a",
"test_b",
"test_c"
Or a bit more generic
$ awk 'BEGIN{RS="\"test1\": \\[\n|\n[[:blank:]]*\\]"}(RT~/]/){print}' test
"test_a",
"test_b",
"test_c"
The first solution searches for test1
and sets a marker to print (p=1
). If it finds a ]
it will set the print marker to zero.
The second solution defines a record separator to be or \"test1\": \\[\n
or \n[[:blank:]]*\\]
. It will check the found record separator, if this is the correct one, it will print.