This can't be done with grep
and shouldn't be done with bash
but it's quite simple if you have GNU awk
just define RS
as },?\n
:
# find a record containing 1545
$ awk '/1545/' RS='},?\n' ORS='}\n' file
{
Item 1
lots of stuff
more stuff 1545
even more
}
This method won't separate multiple records with a ,
like JSON
should but you could define ORS
as },
and remove the last ,
if you need valid JSON
as the result.
Alternatively you could also use RT
instead of ORS
to display the separator that matched the RS
regexp:
$ awk '/1545/{printf "%s",$0RT}' RS='},?\n' file
{
Item 1
lots of stuff
more stuff 1545
even more
},
But depending on whether the last record matched the given pattern you might still need to remove the trailing ,
. A simple sed
command would do the trick sed '$s/,$//'
.
I'd probably just use a proper JSON
passer however.