#!/bin/sed -nf # this will only work on a file that doesn't have any of the following tokens: # THISONE NEWLINE { } #this will mark what each ) is closing :openclose s/( "\([^"]*\)"\([^()]*\))/{ "\1"\2}"\1"/ t openclose s/{/(/g s/}/)/g # this line adds newlines where we want them, after each leaf node s/( "\([^ ]*\)"\([^(]*)[^(]*\)/( "\1"\2\n/g :lp /(/ { s/(/THISONE/ h s/.*THISONE "\([^"]*\)".*/\1_/ x # replace all newlines after the ( with NEWLINE :lpnl s/\(THISONE.*\)\n/\1NEWLINE/ tlpnl :lps x s/[^ ]/ / tlpsc x blpsd :lpsc x s/NEWLINE/NEWLINE /g tlps ; blps :lpsd s/NEWLINE/\n/g s/THISONE/{/ blp } :rp /)/ { s/)/THISONE/ h s/.*THISONE"\([^"]*\)".*/\1_/ x # replace all newlines after the ) with NEWLINE :rpnl s/\(THISONE.*\)\n/\1NEWLINE/ trpnl :rps x s/[^ ]/ / trpsc x brpsd :rpsc x s/NEWLINE /NEWLINE/g trps ; brps :rpsd s/NEWLINE/\n/g s/THISONE"[^"]*"/}/ brp } s/"//g s/{ //g s/ }//g p