bashformatcontinuation

Code formatting with bash script


I would like to search through a file and find all instances where the last non-blank character is a comma and move the line below that up one. Essentially, undoing line continuations like

private static final double SOME_NUMBERS[][] = {
    {1.0, -6.032174644509064E-23},
    {-0.25, -0.25},
    {-0.16624879837036133, -2.6033824355191673E-8}
};

and transforming that to

private static final double SOME_NUMBERS[][] = {
    {1.0, -6.032174644509064E-23}, {-0.25, -0.25}, {-0.16624879837036133, -2.6033824355191673E-8}
};

Is there a good way to do this?


Solution

  • As mjswartz suggests in the comments, we need a sed substitution command like s/,\n/ /g. That, however, does not work by itself because, by default, sed reads in only one line at a time. We can fix that by reading in the whole file first and then doing the substitution:

    $ sed 'H;1h;$!d;x; s/,[[:blank:]]*\n[[:blank:]]*/, /g;' file
    private static final double SOME_NUMBERS[][] = {
        {1.0, -6.032174644509064E-23}, {-0.25, -0.25}, {-0.16624879837036133, -2.6033824355191673E-8}
    };
    

    Because this reads in the whole file at once, this is not a good approach for huge files.

    The above was tested with GNU sed.

    How it works