Replacing a specific whitespace pattern in sed with a newline, when it doesn't have a preceding colon?










0















I am trying to parse the following line using sed to replace a whitespace with a newline only when the whitespace doesn't precede a colon.



For example, I'm using the following input to be processed:



label1: output label2: output2 label3: "output3" label4: output4 label5: output5 label6: output6 label7: output7 label8: output8 label9: output9 label10: output10


I'd like regex to replace any whitespace that doesn't have a colon before it with a newline, so the output would be something like this:



label1: output
label2: output2
label3: "output3"
label4: output4

label5: output5
label6: output6

label7: output7
{


label8: output8
label9: output9


label10: output10


When I try using the following regex in cat file | sed 's/[^:A-Za-z0-9"] /%/g' | tr '%' 'n', it results in the output below, which is close but not achieving the goal:



 label1: output label2: output2 label3: "output3" label4: output4
label5: output5 label6: output6
label7: output7


label8: output8
label9: output9

label10: output10


I've also tried this cat file | sed 's/[^:A-Za-z0-9"] /%/g' | tr '%' 'n', and it results in



label1: outpu
label2: output
label3: "output3
label4: output

label5: output
label6: output

label7: output



label8: output

label9: output


label10: output10


Which looks like the regex also includes replacing every other character that's not a : with a newline.










share|improve this question






















  • You want to avoid the useless use of cat

    – tripleee
    Nov 16 '18 at 6:04











  • The regular expression for "not a colon (or a newline)" is [^:]; it's not clear from your question if you also want newlines followed by space to be replaced.

    – tripleee
    Nov 16 '18 at 6:06















0















I am trying to parse the following line using sed to replace a whitespace with a newline only when the whitespace doesn't precede a colon.



For example, I'm using the following input to be processed:



label1: output label2: output2 label3: "output3" label4: output4 label5: output5 label6: output6 label7: output7 label8: output8 label9: output9 label10: output10


I'd like regex to replace any whitespace that doesn't have a colon before it with a newline, so the output would be something like this:



label1: output
label2: output2
label3: "output3"
label4: output4

label5: output5
label6: output6

label7: output7
{


label8: output8
label9: output9


label10: output10


When I try using the following regex in cat file | sed 's/[^:A-Za-z0-9"] /%/g' | tr '%' 'n', it results in the output below, which is close but not achieving the goal:



 label1: output label2: output2 label3: "output3" label4: output4
label5: output5 label6: output6
label7: output7


label8: output8
label9: output9

label10: output10


I've also tried this cat file | sed 's/[^:A-Za-z0-9"] /%/g' | tr '%' 'n', and it results in



label1: outpu
label2: output
label3: "output3
label4: output

label5: output
label6: output

label7: output



label8: output

label9: output


label10: output10


Which looks like the regex also includes replacing every other character that's not a : with a newline.










share|improve this question






















  • You want to avoid the useless use of cat

    – tripleee
    Nov 16 '18 at 6:04











  • The regular expression for "not a colon (or a newline)" is [^:]; it's not clear from your question if you also want newlines followed by space to be replaced.

    – tripleee
    Nov 16 '18 at 6:06













0












0








0








I am trying to parse the following line using sed to replace a whitespace with a newline only when the whitespace doesn't precede a colon.



For example, I'm using the following input to be processed:



label1: output label2: output2 label3: "output3" label4: output4 label5: output5 label6: output6 label7: output7 label8: output8 label9: output9 label10: output10


I'd like regex to replace any whitespace that doesn't have a colon before it with a newline, so the output would be something like this:



label1: output
label2: output2
label3: "output3"
label4: output4

label5: output5
label6: output6

label7: output7
{


label8: output8
label9: output9


label10: output10


When I try using the following regex in cat file | sed 's/[^:A-Za-z0-9"] /%/g' | tr '%' 'n', it results in the output below, which is close but not achieving the goal:



 label1: output label2: output2 label3: "output3" label4: output4
label5: output5 label6: output6
label7: output7


label8: output8
label9: output9

label10: output10


I've also tried this cat file | sed 's/[^:A-Za-z0-9"] /%/g' | tr '%' 'n', and it results in



label1: outpu
label2: output
label3: "output3
label4: output

label5: output
label6: output

label7: output



label8: output

label9: output


label10: output10


Which looks like the regex also includes replacing every other character that's not a : with a newline.










share|improve this question














I am trying to parse the following line using sed to replace a whitespace with a newline only when the whitespace doesn't precede a colon.



For example, I'm using the following input to be processed:



label1: output label2: output2 label3: "output3" label4: output4 label5: output5 label6: output6 label7: output7 label8: output8 label9: output9 label10: output10


I'd like regex to replace any whitespace that doesn't have a colon before it with a newline, so the output would be something like this:



label1: output
label2: output2
label3: "output3"
label4: output4

label5: output5
label6: output6

label7: output7
{


label8: output8
label9: output9


label10: output10


When I try using the following regex in cat file | sed 's/[^:A-Za-z0-9"] /%/g' | tr '%' 'n', it results in the output below, which is close but not achieving the goal:



 label1: output label2: output2 label3: "output3" label4: output4
label5: output5 label6: output6
label7: output7


label8: output8
label9: output9

label10: output10


I've also tried this cat file | sed 's/[^:A-Za-z0-9"] /%/g' | tr '%' 'n', and it results in



label1: outpu
label2: output
label3: "output3
label4: output

label5: output
label6: output

label7: output



label8: output

label9: output


label10: output10


Which looks like the regex also includes replacing every other character that's not a : with a newline.







regex sed






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 0:58









Anthony_R0Anthony_R0

1




1












  • You want to avoid the useless use of cat

    – tripleee
    Nov 16 '18 at 6:04











  • The regular expression for "not a colon (or a newline)" is [^:]; it's not clear from your question if you also want newlines followed by space to be replaced.

    – tripleee
    Nov 16 '18 at 6:06

















  • You want to avoid the useless use of cat

    – tripleee
    Nov 16 '18 at 6:04











  • The regular expression for "not a colon (or a newline)" is [^:]; it's not clear from your question if you also want newlines followed by space to be replaced.

    – tripleee
    Nov 16 '18 at 6:06
















You want to avoid the useless use of cat

– tripleee
Nov 16 '18 at 6:04





You want to avoid the useless use of cat

– tripleee
Nov 16 '18 at 6:04













The regular expression for "not a colon (or a newline)" is [^:]; it's not clear from your question if you also want newlines followed by space to be replaced.

– tripleee
Nov 16 '18 at 6:06





The regular expression for "not a colon (or a newline)" is [^:]; it's not clear from your question if you also want newlines followed by space to be replaced.

– tripleee
Nov 16 '18 at 6:06












2 Answers
2






active

oldest

votes


















0














This should do it:



sed -E 's/([^:]) /1n/g' file


Output:



label1: output
label2: output2
label3: "output3"
label4: output4

label5: output5
label6: output6

label7: output7



label8: output8

label9: output9


label10: output10


Cheers!






share|improve this answer

























  • Welcome to StackOverflow! Your answer need more explanation about how the code works to be a good answer.

    – Foo
    Nov 16 '18 at 5:56











  • The -r option is not portable; not all sed variants support the escape n for literal newline.

    – tripleee
    Nov 16 '18 at 6:08











  • Changed the -r option to -E. Not sure how to address your comment about n

    – Amol
    Nov 16 '18 at 6:21












  • -E is also not portable; both of these are outside of the POSIX spec. There is a way to embed a literal newline in a sed script but how exactly to do that isn't completely portable, either (typically, backslash-newline works most places).

    – tripleee
    Nov 16 '18 at 6:33



















0














This might work for you (GNU sed):



sed 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


Append a newline to the current line using the G command which by default appends an empty hold space to the pattern space. Using pattern matching and back references, iterate throughout the current line replacing a non-space/non-colon character followed by a space, by the appended newline. When there are no further matches, remove the newline artefact and print the line.



The same solution can be viewed more easily using the -r option (GNU sed only) which removes many of back slashes:



sed -r 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


Also as pointed out, the optimal solution would be:



sed 's/([^: ]) /1n/g' file





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329973%2freplacing-a-specific-whitespace-pattern-in-sed-with-a-newline-when-it-doesnt-h%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This should do it:



    sed -E 's/([^:]) /1n/g' file


    Output:



    label1: output
    label2: output2
    label3: "output3"
    label4: output4

    label5: output5
    label6: output6

    label7: output7



    label8: output8

    label9: output9


    label10: output10


    Cheers!






    share|improve this answer

























    • Welcome to StackOverflow! Your answer need more explanation about how the code works to be a good answer.

      – Foo
      Nov 16 '18 at 5:56











    • The -r option is not portable; not all sed variants support the escape n for literal newline.

      – tripleee
      Nov 16 '18 at 6:08











    • Changed the -r option to -E. Not sure how to address your comment about n

      – Amol
      Nov 16 '18 at 6:21












    • -E is also not portable; both of these are outside of the POSIX spec. There is a way to embed a literal newline in a sed script but how exactly to do that isn't completely portable, either (typically, backslash-newline works most places).

      – tripleee
      Nov 16 '18 at 6:33
















    0














    This should do it:



    sed -E 's/([^:]) /1n/g' file


    Output:



    label1: output
    label2: output2
    label3: "output3"
    label4: output4

    label5: output5
    label6: output6

    label7: output7



    label8: output8

    label9: output9


    label10: output10


    Cheers!






    share|improve this answer

























    • Welcome to StackOverflow! Your answer need more explanation about how the code works to be a good answer.

      – Foo
      Nov 16 '18 at 5:56











    • The -r option is not portable; not all sed variants support the escape n for literal newline.

      – tripleee
      Nov 16 '18 at 6:08











    • Changed the -r option to -E. Not sure how to address your comment about n

      – Amol
      Nov 16 '18 at 6:21












    • -E is also not portable; both of these are outside of the POSIX spec. There is a way to embed a literal newline in a sed script but how exactly to do that isn't completely portable, either (typically, backslash-newline works most places).

      – tripleee
      Nov 16 '18 at 6:33














    0












    0








    0







    This should do it:



    sed -E 's/([^:]) /1n/g' file


    Output:



    label1: output
    label2: output2
    label3: "output3"
    label4: output4

    label5: output5
    label6: output6

    label7: output7



    label8: output8

    label9: output9


    label10: output10


    Cheers!






    share|improve this answer















    This should do it:



    sed -E 's/([^:]) /1n/g' file


    Output:



    label1: output
    label2: output2
    label3: "output3"
    label4: output4

    label5: output5
    label6: output6

    label7: output7



    label8: output8

    label9: output9


    label10: output10


    Cheers!







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 16 '18 at 6:21

























    answered Nov 16 '18 at 5:52









    AmolAmol

    522214




    522214












    • Welcome to StackOverflow! Your answer need more explanation about how the code works to be a good answer.

      – Foo
      Nov 16 '18 at 5:56











    • The -r option is not portable; not all sed variants support the escape n for literal newline.

      – tripleee
      Nov 16 '18 at 6:08











    • Changed the -r option to -E. Not sure how to address your comment about n

      – Amol
      Nov 16 '18 at 6:21












    • -E is also not portable; both of these are outside of the POSIX spec. There is a way to embed a literal newline in a sed script but how exactly to do that isn't completely portable, either (typically, backslash-newline works most places).

      – tripleee
      Nov 16 '18 at 6:33


















    • Welcome to StackOverflow! Your answer need more explanation about how the code works to be a good answer.

      – Foo
      Nov 16 '18 at 5:56











    • The -r option is not portable; not all sed variants support the escape n for literal newline.

      – tripleee
      Nov 16 '18 at 6:08











    • Changed the -r option to -E. Not sure how to address your comment about n

      – Amol
      Nov 16 '18 at 6:21












    • -E is also not portable; both of these are outside of the POSIX spec. There is a way to embed a literal newline in a sed script but how exactly to do that isn't completely portable, either (typically, backslash-newline works most places).

      – tripleee
      Nov 16 '18 at 6:33

















    Welcome to StackOverflow! Your answer need more explanation about how the code works to be a good answer.

    – Foo
    Nov 16 '18 at 5:56





    Welcome to StackOverflow! Your answer need more explanation about how the code works to be a good answer.

    – Foo
    Nov 16 '18 at 5:56













    The -r option is not portable; not all sed variants support the escape n for literal newline.

    – tripleee
    Nov 16 '18 at 6:08





    The -r option is not portable; not all sed variants support the escape n for literal newline.

    – tripleee
    Nov 16 '18 at 6:08













    Changed the -r option to -E. Not sure how to address your comment about n

    – Amol
    Nov 16 '18 at 6:21






    Changed the -r option to -E. Not sure how to address your comment about n

    – Amol
    Nov 16 '18 at 6:21














    -E is also not portable; both of these are outside of the POSIX spec. There is a way to embed a literal newline in a sed script but how exactly to do that isn't completely portable, either (typically, backslash-newline works most places).

    – tripleee
    Nov 16 '18 at 6:33






    -E is also not portable; both of these are outside of the POSIX spec. There is a way to embed a literal newline in a sed script but how exactly to do that isn't completely portable, either (typically, backslash-newline works most places).

    – tripleee
    Nov 16 '18 at 6:33














    0














    This might work for you (GNU sed):



    sed 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


    Append a newline to the current line using the G command which by default appends an empty hold space to the pattern space. Using pattern matching and back references, iterate throughout the current line replacing a non-space/non-colon character followed by a space, by the appended newline. When there are no further matches, remove the newline artefact and print the line.



    The same solution can be viewed more easily using the -r option (GNU sed only) which removes many of back slashes:



    sed -r 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


    Also as pointed out, the optimal solution would be:



    sed 's/([^: ]) /1n/g' file





    share|improve this answer



























      0














      This might work for you (GNU sed):



      sed 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


      Append a newline to the current line using the G command which by default appends an empty hold space to the pattern space. Using pattern matching and back references, iterate throughout the current line replacing a non-space/non-colon character followed by a space, by the appended newline. When there are no further matches, remove the newline artefact and print the line.



      The same solution can be viewed more easily using the -r option (GNU sed only) which removes many of back slashes:



      sed -r 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


      Also as pointed out, the optimal solution would be:



      sed 's/([^: ]) /1n/g' file





      share|improve this answer

























        0












        0








        0







        This might work for you (GNU sed):



        sed 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


        Append a newline to the current line using the G command which by default appends an empty hold space to the pattern space. Using pattern matching and back references, iterate throughout the current line replacing a non-space/non-colon character followed by a space, by the appended newline. When there are no further matches, remove the newline artefact and print the line.



        The same solution can be viewed more easily using the -r option (GNU sed only) which removes many of back slashes:



        sed -r 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


        Also as pointed out, the optimal solution would be:



        sed 's/([^: ]) /1n/g' file





        share|improve this answer













        This might work for you (GNU sed):



        sed 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


        Append a newline to the current line using the G command which by default appends an empty hold space to the pattern space. Using pattern matching and back references, iterate throughout the current line replacing a non-space/non-colon character followed by a space, by the appended newline. When there are no further matches, remove the newline artefact and print the line.



        The same solution can be viewed more easily using the -r option (GNU sed only) which removes many of back slashes:



        sed -r 'G;:a;s/([^: ]) (.*(.))/132/;ta;s/.$//' file


        Also as pointed out, the optimal solution would be:



        sed 's/([^: ]) /1n/g' file






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 8:32









        potongpotong

        36.1k43062




        36.1k43062



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329973%2freplacing-a-specific-whitespace-pattern-in-sed-with-a-newline-when-it-doesnt-h%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric