How can I replace a newline with its escape sequence?












2















Using sed to make text that contains certain characters suitable for use in string literals is straightforward:



sed "s/\\/\\\\/g"
sed "s/\"/\\\"/g"


But how can I do something similar with a text file containing newline characters?










share|improve this question























  • sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.

    – barlop
    Jan 18 '18 at 4:08
















2















Using sed to make text that contains certain characters suitable for use in string literals is straightforward:



sed "s/\\/\\\\/g"
sed "s/\"/\\\"/g"


But how can I do something similar with a text file containing newline characters?










share|improve this question























  • sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.

    – barlop
    Jan 18 '18 at 4:08














2












2








2








Using sed to make text that contains certain characters suitable for use in string literals is straightforward:



sed "s/\\/\\\\/g"
sed "s/\"/\\\"/g"


But how can I do something similar with a text file containing newline characters?










share|improve this question














Using sed to make text that contains certain characters suitable for use in string literals is straightforward:



sed "s/\\/\\\\/g"
sed "s/\"/\\\"/g"


But how can I do something similar with a text file containing newline characters?







sed awk






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 12 '15 at 13:34









MelabMelab

3371921




3371921













  • sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.

    – barlop
    Jan 18 '18 at 4:08



















  • sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.

    – barlop
    Jan 18 '18 at 4:08

















sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.

– barlop
Jan 18 '18 at 4:08





sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.

– barlop
Jan 18 '18 at 4:08










4 Answers
4






active

oldest

votes


















3














Use:



sed ':a;N;$!ba;s/n/\n/g'


Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.






share|improve this answer

































    0














    sed is line based, and this can cause issues when trying to replace newline characters.



    the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.



    I would suggest that 'tr' would probably be a better fit here.



    as an example, to replace newline characters with spaces:



    tr 'n' ' ' < inputfile





    share|improve this answer
























    • Won't [rn] be cleaner?

      – theoden
      Aug 12 '15 at 14:03











    • I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.

      – David Moylan
      Aug 12 '15 at 14:10













    • I tried using tr "n" "\n" before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.

      – Melab
      Aug 12 '15 at 20:12













    • @Melab correct. the 'tr' command will only do a single character replacement.

      – David Moylan
      Aug 15 '15 at 0:04











    • So then how does it help at all?

      – Melab
      Aug 17 '15 at 0:41



















    0














    I'd like to extend David Moytan's solution:



    cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'





    share|improve this answer

































      0














      I had this problem too.



      You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.



      echo -n $(sed 'a
      \n');


      Stick whatever you're sed-ing in the parentheses before the sed command.






      share|improve this answer

























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "3"
        };
        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%2fsuperuser.com%2fquestions%2f955935%2fhow-can-i-replace-a-newline-with-its-escape-sequence%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        Use:



        sed ':a;N;$!ba;s/n/\n/g'


        Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.






        share|improve this answer






























          3














          Use:



          sed ':a;N;$!ba;s/n/\n/g'


          Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.






          share|improve this answer




























            3












            3








            3







            Use:



            sed ':a;N;$!ba;s/n/\n/g'


            Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.






            share|improve this answer















            Use:



            sed ':a;N;$!ba;s/n/\n/g'


            Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 7 at 20:59









            Mikey T.K.

            2,17731941




            2,17731941










            answered Feb 19 '18 at 3:19









            vossad01vossad01

            1314




            1314

























                0














                sed is line based, and this can cause issues when trying to replace newline characters.



                the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.



                I would suggest that 'tr' would probably be a better fit here.



                as an example, to replace newline characters with spaces:



                tr 'n' ' ' < inputfile





                share|improve this answer
























                • Won't [rn] be cleaner?

                  – theoden
                  Aug 12 '15 at 14:03











                • I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.

                  – David Moylan
                  Aug 12 '15 at 14:10













                • I tried using tr "n" "\n" before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.

                  – Melab
                  Aug 12 '15 at 20:12













                • @Melab correct. the 'tr' command will only do a single character replacement.

                  – David Moylan
                  Aug 15 '15 at 0:04











                • So then how does it help at all?

                  – Melab
                  Aug 17 '15 at 0:41
















                0














                sed is line based, and this can cause issues when trying to replace newline characters.



                the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.



                I would suggest that 'tr' would probably be a better fit here.



                as an example, to replace newline characters with spaces:



                tr 'n' ' ' < inputfile





                share|improve this answer
























                • Won't [rn] be cleaner?

                  – theoden
                  Aug 12 '15 at 14:03











                • I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.

                  – David Moylan
                  Aug 12 '15 at 14:10













                • I tried using tr "n" "\n" before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.

                  – Melab
                  Aug 12 '15 at 20:12













                • @Melab correct. the 'tr' command will only do a single character replacement.

                  – David Moylan
                  Aug 15 '15 at 0:04











                • So then how does it help at all?

                  – Melab
                  Aug 17 '15 at 0:41














                0












                0








                0







                sed is line based, and this can cause issues when trying to replace newline characters.



                the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.



                I would suggest that 'tr' would probably be a better fit here.



                as an example, to replace newline characters with spaces:



                tr 'n' ' ' < inputfile





                share|improve this answer













                sed is line based, and this can cause issues when trying to replace newline characters.



                the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.



                I would suggest that 'tr' would probably be a better fit here.



                as an example, to replace newline characters with spaces:



                tr 'n' ' ' < inputfile






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 12 '15 at 14:01









                David MoylanDavid Moylan

                973




                973













                • Won't [rn] be cleaner?

                  – theoden
                  Aug 12 '15 at 14:03











                • I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.

                  – David Moylan
                  Aug 12 '15 at 14:10













                • I tried using tr "n" "\n" before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.

                  – Melab
                  Aug 12 '15 at 20:12













                • @Melab correct. the 'tr' command will only do a single character replacement.

                  – David Moylan
                  Aug 15 '15 at 0:04











                • So then how does it help at all?

                  – Melab
                  Aug 17 '15 at 0:41



















                • Won't [rn] be cleaner?

                  – theoden
                  Aug 12 '15 at 14:03











                • I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.

                  – David Moylan
                  Aug 12 '15 at 14:10













                • I tried using tr "n" "\n" before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.

                  – Melab
                  Aug 12 '15 at 20:12













                • @Melab correct. the 'tr' command will only do a single character replacement.

                  – David Moylan
                  Aug 15 '15 at 0:04











                • So then how does it help at all?

                  – Melab
                  Aug 17 '15 at 0:41

















                Won't [rn] be cleaner?

                – theoden
                Aug 12 '15 at 14:03





                Won't [rn] be cleaner?

                – theoden
                Aug 12 '15 at 14:03













                I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.

                – David Moylan
                Aug 12 '15 at 14:10







                I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.

                – David Moylan
                Aug 12 '15 at 14:10















                I tried using tr "n" "\n" before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.

                – Melab
                Aug 12 '15 at 20:12







                I tried using tr "n" "\n" before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.

                – Melab
                Aug 12 '15 at 20:12















                @Melab correct. the 'tr' command will only do a single character replacement.

                – David Moylan
                Aug 15 '15 at 0:04





                @Melab correct. the 'tr' command will only do a single character replacement.

                – David Moylan
                Aug 15 '15 at 0:04













                So then how does it help at all?

                – Melab
                Aug 17 '15 at 0:41





                So then how does it help at all?

                – Melab
                Aug 17 '15 at 0:41











                0














                I'd like to extend David Moytan's solution:



                cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'





                share|improve this answer






























                  0














                  I'd like to extend David Moytan's solution:



                  cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'





                  share|improve this answer




























                    0












                    0








                    0







                    I'd like to extend David Moytan's solution:



                    cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'





                    share|improve this answer















                    I'd like to extend David Moytan's solution:



                    cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 20 '17 at 10:17









                    Community

                    1




                    1










                    answered Aug 12 '15 at 14:11









                    theodentheoden

                    529519




                    529519























                        0














                        I had this problem too.



                        You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.



                        echo -n $(sed 'a
                        \n');


                        Stick whatever you're sed-ing in the parentheses before the sed command.






                        share|improve this answer






























                          0














                          I had this problem too.



                          You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.



                          echo -n $(sed 'a
                          \n');


                          Stick whatever you're sed-ing in the parentheses before the sed command.






                          share|improve this answer




























                            0












                            0








                            0







                            I had this problem too.



                            You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.



                            echo -n $(sed 'a
                            \n');


                            Stick whatever you're sed-ing in the parentheses before the sed command.






                            share|improve this answer















                            I had this problem too.



                            You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.



                            echo -n $(sed 'a
                            \n');


                            Stick whatever you're sed-ing in the parentheses before the sed command.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 18 '18 at 1:31









                            RockPaperLizard

                            3,184133671




                            3,184133671










                            answered Jan 18 '18 at 1:10









                            Kaeden WileKaeden Wile

                            12




                            12






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Super User!


                                • 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%2fsuperuser.com%2fquestions%2f955935%2fhow-can-i-replace-a-newline-with-its-escape-sequence%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

                                flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                Mangá

                                 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕