Tricky SED matching and replace pattern for reformatting source codes brackets - Unix

This is a discussion on Tricky SED matching and replace pattern for reformatting source codes brackets - Unix ; Ok, I know, the recommended bracket style for source codes (e.g. in java) is like: if (.....) { command statements } But all the curly brackets at the beginning is for me rather bugging and makes me nervous. I prefer ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Tricky SED matching and replace pattern for reformatting source codes brackets

  1. Tricky SED matching and replace pattern for reformatting source codes brackets

    Ok, I know, the recommended bracket style for source codes (e.g. in java) is like:

    if (.....)
    {
    command statements
    }

    But all the curly brackets at the beginning is for me rather bugging and makes me nervous.
    I prefer a style like

    if (.....) {
    command statements }

    where different nesting levels are visualized by different indention.
    I don't want to discuss this style at this place.

    However I need a good sed search and replace pattern which converts a source code file with old
    curly bracketing into my personal style.

    This should be theoretically rather easy. I only need a pattern which replaces

    blank-blank-curlybracket

    or

    controlreturn-blank-curlybracket

    or

    controlreturn-curlybracket

    into

    blank-curlybracket

    iteratively as often as necessary for the whole file.
    Notice that before a closing curly bracket there should be exactly one blank.

    I feel that this is somehow feasible with sed but I cannot find the magic pattern.

    Can someone help me?

    Matt


  2. Re: Tricky SED matching and replace pattern for reformatting sourcecodes brackets

    mbens@hotmail.com (Matt Benson) writes:

    > Ok, I know, the recommended bracket style for source codes (e.g. in java) is like:
    >
    > if (.....)
    > {
    > command statements
    > }
    >
    > But all the curly brackets at the beginning is for me rather bugging and makes me nervous.
    > I prefer a style like
    >
    > if (.....) {
    > command statements }


    I don't think you are going to find sed up to the task.
    Handling newlines is tricky.

    GNU indent with the -br flag will do the job.

  3. Re: Tricky SED matching and replace pattern for reformatting source codes brackets

    In message of Thu, 12 Aug 2004
    12:13:17 in comp.unix.misc, Dan Espen
    writes
    >mbens@hotmail.com (Matt Benson) writes:

    Why do you consider you are topical in 5 newsgroups? The thread has
    disintegrated as correspondents have selected different subsets of the
    original groups. I stick to your original set.

    >
    >> Ok, I know, the recommended bracket style for source codes (e.g. in
    >>java) is like:
    >>
    >> if (.....)
    >> {
    >> command statements
    >> }
    >>
    >> But all the curly brackets at the beginning is for me rather bugging
    >>and makes me nervous.
    >> I prefer a style like
    >>
    >> if (.....) {
    >> command statements }

    >
    >I don't think you are going to find sed up to the task.

    I disagree with that advice but doubt the OP will want to spend enough
    time to develop a solution. I threw the following together to transform
    the supplied data:- "N;s/) *\n *{/) {/;s/ *\n *}/ }/"

    >Handling newlines is tricky.
    >
    >GNU indent with the -br flag will do the job.

    I agree that ANY tool beats rolling your own. If a tool does most of the
    work, do the rest with sed.
    --
    Walter Briscoe

+ Reply to Thread