How to replace spaces with newlines/enter in a text-file?












12















I have simple text file named "example".



Reading with terminal command: cat example



Output:



abc cdef ghi jk lmnopq rst uv wxyz


I want to convert (transform) into following form: (expected output from cat example)



abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz


How can I do this via the command-line?



(This is only an example file, I want to convert word's position in vertical-column)










share|improve this question

























  • Related: unix.stackexchange.com/q/169995/85039

    – Sergiy Kolodyazhnyy
    Jan 16 '17 at 13:48
















12















I have simple text file named "example".



Reading with terminal command: cat example



Output:



abc cdef ghi jk lmnopq rst uv wxyz


I want to convert (transform) into following form: (expected output from cat example)



abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz


How can I do this via the command-line?



(This is only an example file, I want to convert word's position in vertical-column)










share|improve this question

























  • Related: unix.stackexchange.com/q/169995/85039

    – Sergiy Kolodyazhnyy
    Jan 16 '17 at 13:48














12












12








12


6






I have simple text file named "example".



Reading with terminal command: cat example



Output:



abc cdef ghi jk lmnopq rst uv wxyz


I want to convert (transform) into following form: (expected output from cat example)



abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz


How can I do this via the command-line?



(This is only an example file, I want to convert word's position in vertical-column)










share|improve this question
















I have simple text file named "example".



Reading with terminal command: cat example



Output:



abc cdef ghi jk lmnopq rst uv wxyz


I want to convert (transform) into following form: (expected output from cat example)



abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz


How can I do this via the command-line?



(This is only an example file, I want to convert word's position in vertical-column)







command-line text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 8 '14 at 10:24







Pandya

















asked May 5 '14 at 14:34









PandyaPandya

20k2794157




20k2794157













  • Related: unix.stackexchange.com/q/169995/85039

    – Sergiy Kolodyazhnyy
    Jan 16 '17 at 13:48



















  • Related: unix.stackexchange.com/q/169995/85039

    – Sergiy Kolodyazhnyy
    Jan 16 '17 at 13:48

















Related: unix.stackexchange.com/q/169995/85039

– Sergiy Kolodyazhnyy
Jan 16 '17 at 13:48





Related: unix.stackexchange.com/q/169995/85039

– Sergiy Kolodyazhnyy
Jan 16 '17 at 13:48










6 Answers
6






active

oldest

votes


















27














A few choices:





  1. The classic, use tr:



    tr ' ' 'n' < example



  2. Use cut



    cut -d ' ' --output-delimiter=$'n' -f 1- example



  3. Use sed



    sed 's/ /n/g' example



  4. Use perl



    perl -pe 's/ /n/g' example



  5. Use the shell



    foo=$(cat example); echo -e ${foo// /\n}







share|improve this answer

































    16














    Try the below command



    awk -v RS=" " '{print}' file


    OR



    awk -v RS='[n ]' '{print}' file


    Example:



    $ awk -v RS=" " '{print}' example
    abc
    cdef
    ghi
    jk
    lmnopq
    rst
    uv
    wxyz


    Explanation:



    RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.



    In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.






    share|improve this answer

































      7














      You can use xargs,



      cat example | xargs -n 1


      or, better



      xargs -n 1 < example





      share|improve this answer





















      • 5





        xargs -n 1 < example saves you 1 kitten

        – Rinzwind
        May 5 '14 at 14:48



















      2














      Using a perl oneliner:



      perl -p -i -e 's/s/n/g' example


      It will replace spaces and tabs with "ENTER" (aka n)






      share|improve this answer































        1














        No one posted python, so here's that:



        python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt 


        We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.






        share|improve this answer































          0














          Similar to the 'tr' above but with the additions:




          • Also works for tabs



          • Converts multiple spaces or tabs to 1 newline



            tr -s '[:space:]' 'n' < example







          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            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%2faskubuntu.com%2fquestions%2f461144%2fhow-to-replace-spaces-with-newlines-enter-in-a-text-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            27














            A few choices:





            1. The classic, use tr:



              tr ' ' 'n' < example



            2. Use cut



              cut -d ' ' --output-delimiter=$'n' -f 1- example



            3. Use sed



              sed 's/ /n/g' example



            4. Use perl



              perl -pe 's/ /n/g' example



            5. Use the shell



              foo=$(cat example); echo -e ${foo// /\n}







            share|improve this answer






























              27














              A few choices:





              1. The classic, use tr:



                tr ' ' 'n' < example



              2. Use cut



                cut -d ' ' --output-delimiter=$'n' -f 1- example



              3. Use sed



                sed 's/ /n/g' example



              4. Use perl



                perl -pe 's/ /n/g' example



              5. Use the shell



                foo=$(cat example); echo -e ${foo// /\n}







              share|improve this answer




























                27












                27








                27







                A few choices:





                1. The classic, use tr:



                  tr ' ' 'n' < example



                2. Use cut



                  cut -d ' ' --output-delimiter=$'n' -f 1- example



                3. Use sed



                  sed 's/ /n/g' example



                4. Use perl



                  perl -pe 's/ /n/g' example



                5. Use the shell



                  foo=$(cat example); echo -e ${foo// /\n}







                share|improve this answer















                A few choices:





                1. The classic, use tr:



                  tr ' ' 'n' < example



                2. Use cut



                  cut -d ' ' --output-delimiter=$'n' -f 1- example



                3. Use sed



                  sed 's/ /n/g' example



                4. Use perl



                  perl -pe 's/ /n/g' example



                5. Use the shell



                  foo=$(cat example); echo -e ${foo// /\n}








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 6 '14 at 10:57

























                answered May 5 '14 at 14:42









                terdonterdon

                65.9k12138221




                65.9k12138221

























                    16














                    Try the below command



                    awk -v RS=" " '{print}' file


                    OR



                    awk -v RS='[n ]' '{print}' file


                    Example:



                    $ awk -v RS=" " '{print}' example
                    abc
                    cdef
                    ghi
                    jk
                    lmnopq
                    rst
                    uv
                    wxyz


                    Explanation:



                    RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.



                    In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.






                    share|improve this answer






























                      16














                      Try the below command



                      awk -v RS=" " '{print}' file


                      OR



                      awk -v RS='[n ]' '{print}' file


                      Example:



                      $ awk -v RS=" " '{print}' example
                      abc
                      cdef
                      ghi
                      jk
                      lmnopq
                      rst
                      uv
                      wxyz


                      Explanation:



                      RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.



                      In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.






                      share|improve this answer




























                        16












                        16








                        16







                        Try the below command



                        awk -v RS=" " '{print}' file


                        OR



                        awk -v RS='[n ]' '{print}' file


                        Example:



                        $ awk -v RS=" " '{print}' example
                        abc
                        cdef
                        ghi
                        jk
                        lmnopq
                        rst
                        uv
                        wxyz


                        Explanation:



                        RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.



                        In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.






                        share|improve this answer















                        Try the below command



                        awk -v RS=" " '{print}' file


                        OR



                        awk -v RS='[n ]' '{print}' file


                        Example:



                        $ awk -v RS=" " '{print}' example
                        abc
                        cdef
                        ghi
                        jk
                        lmnopq
                        rst
                        uv
                        wxyz


                        Explanation:



                        RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.



                        In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 5 '14 at 15:00

























                        answered May 5 '14 at 14:37









                        Avinash RajAvinash Raj

                        51.8k41168218




                        51.8k41168218























                            7














                            You can use xargs,



                            cat example | xargs -n 1


                            or, better



                            xargs -n 1 < example





                            share|improve this answer





















                            • 5





                              xargs -n 1 < example saves you 1 kitten

                              – Rinzwind
                              May 5 '14 at 14:48
















                            7














                            You can use xargs,



                            cat example | xargs -n 1


                            or, better



                            xargs -n 1 < example





                            share|improve this answer





















                            • 5





                              xargs -n 1 < example saves you 1 kitten

                              – Rinzwind
                              May 5 '14 at 14:48














                            7












                            7








                            7







                            You can use xargs,



                            cat example | xargs -n 1


                            or, better



                            xargs -n 1 < example





                            share|improve this answer















                            You can use xargs,



                            cat example | xargs -n 1


                            or, better



                            xargs -n 1 < example






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 5 '14 at 14:50

























                            answered May 5 '14 at 14:42









                            souravcsouravc

                            27k1376105




                            27k1376105








                            • 5





                              xargs -n 1 < example saves you 1 kitten

                              – Rinzwind
                              May 5 '14 at 14:48














                            • 5





                              xargs -n 1 < example saves you 1 kitten

                              – Rinzwind
                              May 5 '14 at 14:48








                            5




                            5





                            xargs -n 1 < example saves you 1 kitten

                            – Rinzwind
                            May 5 '14 at 14:48





                            xargs -n 1 < example saves you 1 kitten

                            – Rinzwind
                            May 5 '14 at 14:48











                            2














                            Using a perl oneliner:



                            perl -p -i -e 's/s/n/g' example


                            It will replace spaces and tabs with "ENTER" (aka n)






                            share|improve this answer




























                              2














                              Using a perl oneliner:



                              perl -p -i -e 's/s/n/g' example


                              It will replace spaces and tabs with "ENTER" (aka n)






                              share|improve this answer


























                                2












                                2








                                2







                                Using a perl oneliner:



                                perl -p -i -e 's/s/n/g' example


                                It will replace spaces and tabs with "ENTER" (aka n)






                                share|improve this answer













                                Using a perl oneliner:



                                perl -p -i -e 's/s/n/g' example


                                It will replace spaces and tabs with "ENTER" (aka n)







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 5 '14 at 14:42









                                Sylvain PineauSylvain Pineau

                                48.9k16107150




                                48.9k16107150























                                    1














                                    No one posted python, so here's that:



                                    python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt 


                                    We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.






                                    share|improve this answer




























                                      1














                                      No one posted python, so here's that:



                                      python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt 


                                      We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.






                                      share|improve this answer


























                                        1












                                        1








                                        1







                                        No one posted python, so here's that:



                                        python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt 


                                        We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.






                                        share|improve this answer













                                        No one posted python, so here's that:



                                        python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt 


                                        We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jan 16 '17 at 13:52









                                        Sergiy KolodyazhnyySergiy Kolodyazhnyy

                                        71.9k9148314




                                        71.9k9148314























                                            0














                                            Similar to the 'tr' above but with the additions:




                                            • Also works for tabs



                                            • Converts multiple spaces or tabs to 1 newline



                                              tr -s '[:space:]' 'n' < example







                                            share|improve this answer




























                                              0














                                              Similar to the 'tr' above but with the additions:




                                              • Also works for tabs



                                              • Converts multiple spaces or tabs to 1 newline



                                                tr -s '[:space:]' 'n' < example







                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                Similar to the 'tr' above but with the additions:




                                                • Also works for tabs



                                                • Converts multiple spaces or tabs to 1 newline



                                                  tr -s '[:space:]' 'n' < example







                                                share|improve this answer













                                                Similar to the 'tr' above but with the additions:




                                                • Also works for tabs



                                                • Converts multiple spaces or tabs to 1 newline



                                                  tr -s '[:space:]' 'n' < example








                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jan 22 at 10:55









                                                AstroTomAstroTom

                                                1




                                                1






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Ask Ubuntu!


                                                    • 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%2faskubuntu.com%2fquestions%2f461144%2fhow-to-replace-spaces-with-newlines-enter-in-a-text-file%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

                                                    How to make a Squid Proxy server?

                                                    第一次世界大戦

                                                    Touch on Surface Book