Linux – How to add a line break before a set of values going into a bash script

awkbashlinuxscriptingsed

I've got a set of data piping into a bash script. Here's an example of what that data looks like:

"foo1": "Miscellaneous text",
    "foo2": "More text",
    "foo3": "blah blah blah",
      "foo4": 1635.0,
      "foo5": 0.0,
"foo1": "Miscellaneous text that is different",
    "foo2": "More text1231231",
    "foo3": "blah blah blah234234",
      "foo4": 1633425.0,
      "foo5": 0.0,
"foo1": "Miscellaneous text abc123",
    "foo2": "More text122121",
    "foo3": "blah blah blah414124",
      "foo4": 163235.0,
      "foo5": 1.0,
"foo1": "More Miscellaneous text",
    "foo2": "asdfasdfaMore text",
    "foo3": "blah blahadsfasdf blah",
      "foo4": 1635232.0,
      "foo5": 0.0,

I want to add a line break character (\n) to precede "foo1". In other words, change the data to read:

    \n"foo1": "Miscellaneous text",
        "foo2": "More text",
        "foo3": "blah blah blah",
          "foo4": 1635.0,
          "foo5": 0.0,
    \n"foo1": "Miscellaneous text that is different",
        "foo2": "More text1231231",
        "foo3": "blah blah blah234234",
          "foo4": 1633425.0,
          "foo5": 0.0,
    \n"foo1": "Miscellaneous text abc123",
        "foo2": "More text122121",
        "foo3": "blah blah blah414124",
          "foo4": 163235.0,
          "foo5": 1.0,
    \n"foo1": "More Miscellaneous text",
        "foo2": "asdfasdfaMore text",
        "foo3": "blah blahadsfasdf blah",
          "foo4": 1635232.0,
          "foo5": 0.0,

I thought it would be possible to do with sed/awk but I'm not so sure as the values for foo1 can/will change.

Any ideas?

Best Answer

This will add a '\n' for every line starting with "foo":

sed 's/^"foo1":/\n&/'