Unix - need to remove line break from record spanning multiple lines

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












I have a file as below



"IN001~24Apr16~Hi,
what a way?
oh no!~
not here~"
"IN003~29Apr16~
what a way?
~oh no!
say again.
not again~"


and I want the output in below format



"IN001~24Apr16~Hi,what a way?oh no!~not here~"
"IN003~29Apr16~what a way?~oh no!say again.not again~"


Any solution using awk or sed is much appreciated.







share|improve this question






















  • Your input and output looks similar, can you format it correctly and paste at least two lines for input ...
    – Bharat
    Apr 11 at 17:32














up vote
-1
down vote

favorite












I have a file as below



"IN001~24Apr16~Hi,
what a way?
oh no!~
not here~"
"IN003~29Apr16~
what a way?
~oh no!
say again.
not again~"


and I want the output in below format



"IN001~24Apr16~Hi,what a way?oh no!~not here~"
"IN003~29Apr16~what a way?~oh no!say again.not again~"


Any solution using awk or sed is much appreciated.







share|improve this question






















  • Your input and output looks similar, can you format it correctly and paste at least two lines for input ...
    – Bharat
    Apr 11 at 17:32












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a file as below



"IN001~24Apr16~Hi,
what a way?
oh no!~
not here~"
"IN003~29Apr16~
what a way?
~oh no!
say again.
not again~"


and I want the output in below format



"IN001~24Apr16~Hi,what a way?oh no!~not here~"
"IN003~29Apr16~what a way?~oh no!say again.not again~"


Any solution using awk or sed is much appreciated.







share|improve this question














I have a file as below



"IN001~24Apr16~Hi,
what a way?
oh no!~
not here~"
"IN003~29Apr16~
what a way?
~oh no!
say again.
not again~"


and I want the output in below format



"IN001~24Apr16~Hi,what a way?oh no!~not here~"
"IN003~29Apr16~what a way?~oh no!say again.not again~"


Any solution using awk or sed is much appreciated.









share|improve this question













share|improve this question




share|improve this question








edited Apr 11 at 18:05









Hauke Laging

53.2k1282130




53.2k1282130










asked Apr 11 at 16:34









Sundeep

4




4











  • Your input and output looks similar, can you format it correctly and paste at least two lines for input ...
    – Bharat
    Apr 11 at 17:32
















  • Your input and output looks similar, can you format it correctly and paste at least two lines for input ...
    – Bharat
    Apr 11 at 17:32















Your input and output looks similar, can you format it correctly and paste at least two lines for input ...
– Bharat
Apr 11 at 17:32




Your input and output looks similar, can you format it correctly and paste at least two lines for input ...
– Bharat
Apr 11 at 17:32










2 Answers
2






active

oldest

votes

















up vote
0
down vote













awk ' printf "%s",$0 ; /"$/ print ""; ;' inputfile


There shall be a newline in the output only if the input line ends with double quotes. Thus this awk code outputs all input lines without a newline. After that it checks whether the input line ends in a " and if so then a newline is output.






share|improve this answer






















  • I really should ask you to qualify this with an explanation, but given my (first) comment underneath the question itself I'm going to OK this from the review queue as is.
    – roaima
    Apr 11 at 22:16











  • @roaima You seem to imply that correctly working, unique code could be a "very low quality" answer; especially when the question is about a "solution" and not about an explanation. That would be a strange attitude.
    – Hauke Laging
    Apr 11 at 22:53










  • Not at all. It was flagged in the LQ review queue, possibly because it was an unadorned single line of code. Sufficiently clever code that others could really benefit from an explanation.
    – roaima
    Apr 11 at 23:01


















up vote
0
down vote













$ sed -n -e '/^"/h;n;' -e H -e '/"$/g;s/n//g;p;' file
"IN001~24Apr16~Hi,what a way?oh no!~not here~"
"IN003~29Apr16~what a way?~oh no!say again.not again~"


The sed command will...



  • /^"/h;n;: copy all lines that start with " to the hold space (h), overwriting whatever was there, and immediately move on to the next line of input (n).


  • H: Other lines are appended to the hold space.


  • /"$/g;s/n//g;p;: If a line ends with ", the hold space will be fetched (g) and newlines removed from it (these are added when appending lines to the hold space), and the resulting string is printed.


The command line can be compressed a bit:



$ sed -n '/^"/h;n;;H;/"$/g;s/n//g;p;' file





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437079%2funix-need-to-remove-line-break-from-record-spanning-multiple-lines%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    awk ' printf "%s",$0 ; /"$/ print ""; ;' inputfile


    There shall be a newline in the output only if the input line ends with double quotes. Thus this awk code outputs all input lines without a newline. After that it checks whether the input line ends in a " and if so then a newline is output.






    share|improve this answer






















    • I really should ask you to qualify this with an explanation, but given my (first) comment underneath the question itself I'm going to OK this from the review queue as is.
      – roaima
      Apr 11 at 22:16











    • @roaima You seem to imply that correctly working, unique code could be a "very low quality" answer; especially when the question is about a "solution" and not about an explanation. That would be a strange attitude.
      – Hauke Laging
      Apr 11 at 22:53










    • Not at all. It was flagged in the LQ review queue, possibly because it was an unadorned single line of code. Sufficiently clever code that others could really benefit from an explanation.
      – roaima
      Apr 11 at 23:01















    up vote
    0
    down vote













    awk ' printf "%s",$0 ; /"$/ print ""; ;' inputfile


    There shall be a newline in the output only if the input line ends with double quotes. Thus this awk code outputs all input lines without a newline. After that it checks whether the input line ends in a " and if so then a newline is output.






    share|improve this answer






















    • I really should ask you to qualify this with an explanation, but given my (first) comment underneath the question itself I'm going to OK this from the review queue as is.
      – roaima
      Apr 11 at 22:16











    • @roaima You seem to imply that correctly working, unique code could be a "very low quality" answer; especially when the question is about a "solution" and not about an explanation. That would be a strange attitude.
      – Hauke Laging
      Apr 11 at 22:53










    • Not at all. It was flagged in the LQ review queue, possibly because it was an unadorned single line of code. Sufficiently clever code that others could really benefit from an explanation.
      – roaima
      Apr 11 at 23:01













    up vote
    0
    down vote










    up vote
    0
    down vote









    awk ' printf "%s",$0 ; /"$/ print ""; ;' inputfile


    There shall be a newline in the output only if the input line ends with double quotes. Thus this awk code outputs all input lines without a newline. After that it checks whether the input line ends in a " and if so then a newline is output.






    share|improve this answer














    awk ' printf "%s",$0 ; /"$/ print ""; ;' inputfile


    There shall be a newline in the output only if the input line ends with double quotes. Thus this awk code outputs all input lines without a newline. After that it checks whether the input line ends in a " and if so then a newline is output.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 12 at 18:13

























    answered Apr 11 at 18:08









    Hauke Laging

    53.2k1282130




    53.2k1282130











    • I really should ask you to qualify this with an explanation, but given my (first) comment underneath the question itself I'm going to OK this from the review queue as is.
      – roaima
      Apr 11 at 22:16











    • @roaima You seem to imply that correctly working, unique code could be a "very low quality" answer; especially when the question is about a "solution" and not about an explanation. That would be a strange attitude.
      – Hauke Laging
      Apr 11 at 22:53










    • Not at all. It was flagged in the LQ review queue, possibly because it was an unadorned single line of code. Sufficiently clever code that others could really benefit from an explanation.
      – roaima
      Apr 11 at 23:01

















    • I really should ask you to qualify this with an explanation, but given my (first) comment underneath the question itself I'm going to OK this from the review queue as is.
      – roaima
      Apr 11 at 22:16











    • @roaima You seem to imply that correctly working, unique code could be a "very low quality" answer; especially when the question is about a "solution" and not about an explanation. That would be a strange attitude.
      – Hauke Laging
      Apr 11 at 22:53










    • Not at all. It was flagged in the LQ review queue, possibly because it was an unadorned single line of code. Sufficiently clever code that others could really benefit from an explanation.
      – roaima
      Apr 11 at 23:01
















    I really should ask you to qualify this with an explanation, but given my (first) comment underneath the question itself I'm going to OK this from the review queue as is.
    – roaima
    Apr 11 at 22:16





    I really should ask you to qualify this with an explanation, but given my (first) comment underneath the question itself I'm going to OK this from the review queue as is.
    – roaima
    Apr 11 at 22:16













    @roaima You seem to imply that correctly working, unique code could be a "very low quality" answer; especially when the question is about a "solution" and not about an explanation. That would be a strange attitude.
    – Hauke Laging
    Apr 11 at 22:53




    @roaima You seem to imply that correctly working, unique code could be a "very low quality" answer; especially when the question is about a "solution" and not about an explanation. That would be a strange attitude.
    – Hauke Laging
    Apr 11 at 22:53












    Not at all. It was flagged in the LQ review queue, possibly because it was an unadorned single line of code. Sufficiently clever code that others could really benefit from an explanation.
    – roaima
    Apr 11 at 23:01





    Not at all. It was flagged in the LQ review queue, possibly because it was an unadorned single line of code. Sufficiently clever code that others could really benefit from an explanation.
    – roaima
    Apr 11 at 23:01













    up vote
    0
    down vote













    $ sed -n -e '/^"/h;n;' -e H -e '/"$/g;s/n//g;p;' file
    "IN001~24Apr16~Hi,what a way?oh no!~not here~"
    "IN003~29Apr16~what a way?~oh no!say again.not again~"


    The sed command will...



    • /^"/h;n;: copy all lines that start with " to the hold space (h), overwriting whatever was there, and immediately move on to the next line of input (n).


    • H: Other lines are appended to the hold space.


    • /"$/g;s/n//g;p;: If a line ends with ", the hold space will be fetched (g) and newlines removed from it (these are added when appending lines to the hold space), and the resulting string is printed.


    The command line can be compressed a bit:



    $ sed -n '/^"/h;n;;H;/"$/g;s/n//g;p;' file





    share|improve this answer


























      up vote
      0
      down vote













      $ sed -n -e '/^"/h;n;' -e H -e '/"$/g;s/n//g;p;' file
      "IN001~24Apr16~Hi,what a way?oh no!~not here~"
      "IN003~29Apr16~what a way?~oh no!say again.not again~"


      The sed command will...



      • /^"/h;n;: copy all lines that start with " to the hold space (h), overwriting whatever was there, and immediately move on to the next line of input (n).


      • H: Other lines are appended to the hold space.


      • /"$/g;s/n//g;p;: If a line ends with ", the hold space will be fetched (g) and newlines removed from it (these are added when appending lines to the hold space), and the resulting string is printed.


      The command line can be compressed a bit:



      $ sed -n '/^"/h;n;;H;/"$/g;s/n//g;p;' file





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        $ sed -n -e '/^"/h;n;' -e H -e '/"$/g;s/n//g;p;' file
        "IN001~24Apr16~Hi,what a way?oh no!~not here~"
        "IN003~29Apr16~what a way?~oh no!say again.not again~"


        The sed command will...



        • /^"/h;n;: copy all lines that start with " to the hold space (h), overwriting whatever was there, and immediately move on to the next line of input (n).


        • H: Other lines are appended to the hold space.


        • /"$/g;s/n//g;p;: If a line ends with ", the hold space will be fetched (g) and newlines removed from it (these are added when appending lines to the hold space), and the resulting string is printed.


        The command line can be compressed a bit:



        $ sed -n '/^"/h;n;;H;/"$/g;s/n//g;p;' file





        share|improve this answer














        $ sed -n -e '/^"/h;n;' -e H -e '/"$/g;s/n//g;p;' file
        "IN001~24Apr16~Hi,what a way?oh no!~not here~"
        "IN003~29Apr16~what a way?~oh no!say again.not again~"


        The sed command will...



        • /^"/h;n;: copy all lines that start with " to the hold space (h), overwriting whatever was there, and immediately move on to the next line of input (n).


        • H: Other lines are appended to the hold space.


        • /"$/g;s/n//g;p;: If a line ends with ", the hold space will be fetched (g) and newlines removed from it (these are added when appending lines to the hold space), and the resulting string is printed.


        The command line can be compressed a bit:



        $ sed -n '/^"/h;n;;H;/"$/g;s/n//g;p;' file






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 12 at 18:31

























        answered Apr 11 at 20:12









        Kusalananda

        102k13199316




        102k13199316






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437079%2funix-need-to-remove-line-break-from-record-spanning-multiple-lines%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)