Search code examples
linuxbashsedscriptingcut

Split a JSON to multiple lines using UNIX command line tools


We have a JSON file that needs to be multi-lined instead of being single lined as shown below,

{this is the first block},{this is the second block, really},{this is the third you're kidding:no}

We expected it to be like this so that it can be fed to an external program to read it without issues,

{this is the first block}
{this is the second block, really}
{this is the third you're kidding:no}

I'm not an expert with simple text processing tools like awk, sed, cut but I did try with sed for sometime unsuccessfully.

cat test.json | sed 's/},/\n/g'
{this is the first block
{this is the second block, really
{this is the third you're kidding:no}

What is the best way to do this?


Solution

  • Since it is not JSON and you just want to split:

    input_file

    {this is the first block},{this is the second block, really},{this is the third you're kidding:no}
    
    sed 's/},/}\n/g' input_file
    

    output

    {this is the first block}
    {this is the second block, really}
    {this is the third you're kidding:no}