How to reverse a text file on Windows?











up vote
3
down vote

favorite
1












How do I reverse the contents of a .txt file whilst preserving empty lines?



For example



text one

text two
text three

text four


text five


text six


would become



text six


text five


text four

text three
text two

text one


Preferably with the Windows console or also source code to compile from to an application. I have Windows 7.










share|improve this question
























  • (Replying to my own post since I can't post an answer with <10 reputation. Thanks for your replies!) I found something: tac, which is part of CoreUtils for Windows. I just tested it and it's working fine for me.
    – A. D.
    May 1 '14 at 12:44












  • Possible duplicate of How to reverse text file in Windows
    – and31415
    May 1 '14 at 12:50






  • 1




    I even checked twice that I am not on http://codegolf.stackexchange.com/
    – VL-80
    May 1 '14 at 12:50










  • The tags were removed from your question for a reason. These tags are not useful and add nothing of value to your question.
    – Der Hochstapler
    Jul 28 '14 at 11:04















up vote
3
down vote

favorite
1












How do I reverse the contents of a .txt file whilst preserving empty lines?



For example



text one

text two
text three

text four


text five


text six


would become



text six


text five


text four

text three
text two

text one


Preferably with the Windows console or also source code to compile from to an application. I have Windows 7.










share|improve this question
























  • (Replying to my own post since I can't post an answer with <10 reputation. Thanks for your replies!) I found something: tac, which is part of CoreUtils for Windows. I just tested it and it's working fine for me.
    – A. D.
    May 1 '14 at 12:44












  • Possible duplicate of How to reverse text file in Windows
    – and31415
    May 1 '14 at 12:50






  • 1




    I even checked twice that I am not on http://codegolf.stackexchange.com/
    – VL-80
    May 1 '14 at 12:50










  • The tags were removed from your question for a reason. These tags are not useful and add nothing of value to your question.
    – Der Hochstapler
    Jul 28 '14 at 11:04













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





How do I reverse the contents of a .txt file whilst preserving empty lines?



For example



text one

text two
text three

text four


text five


text six


would become



text six


text five


text four

text three
text two

text one


Preferably with the Windows console or also source code to compile from to an application. I have Windows 7.










share|improve this question















How do I reverse the contents of a .txt file whilst preserving empty lines?



For example



text one

text two
text three

text four


text five


text six


would become



text six


text five


text four

text three
text two

text one


Preferably with the Windows console or also source code to compile from to an application. I have Windows 7.







windows sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 17:32









Twisty Impersonator

17.3k126293




17.3k126293










asked May 1 '14 at 11:44









A. D.

16626




16626












  • (Replying to my own post since I can't post an answer with <10 reputation. Thanks for your replies!) I found something: tac, which is part of CoreUtils for Windows. I just tested it and it's working fine for me.
    – A. D.
    May 1 '14 at 12:44












  • Possible duplicate of How to reverse text file in Windows
    – and31415
    May 1 '14 at 12:50






  • 1




    I even checked twice that I am not on http://codegolf.stackexchange.com/
    – VL-80
    May 1 '14 at 12:50










  • The tags were removed from your question for a reason. These tags are not useful and add nothing of value to your question.
    – Der Hochstapler
    Jul 28 '14 at 11:04


















  • (Replying to my own post since I can't post an answer with <10 reputation. Thanks for your replies!) I found something: tac, which is part of CoreUtils for Windows. I just tested it and it's working fine for me.
    – A. D.
    May 1 '14 at 12:44












  • Possible duplicate of How to reverse text file in Windows
    – and31415
    May 1 '14 at 12:50






  • 1




    I even checked twice that I am not on http://codegolf.stackexchange.com/
    – VL-80
    May 1 '14 at 12:50










  • The tags were removed from your question for a reason. These tags are not useful and add nothing of value to your question.
    – Der Hochstapler
    Jul 28 '14 at 11:04
















(Replying to my own post since I can't post an answer with <10 reputation. Thanks for your replies!) I found something: tac, which is part of CoreUtils for Windows. I just tested it and it's working fine for me.
– A. D.
May 1 '14 at 12:44






(Replying to my own post since I can't post an answer with <10 reputation. Thanks for your replies!) I found something: tac, which is part of CoreUtils for Windows. I just tested it and it's working fine for me.
– A. D.
May 1 '14 at 12:44














Possible duplicate of How to reverse text file in Windows
– and31415
May 1 '14 at 12:50




Possible duplicate of How to reverse text file in Windows
– and31415
May 1 '14 at 12:50




1




1




I even checked twice that I am not on http://codegolf.stackexchange.com/
– VL-80
May 1 '14 at 12:50




I even checked twice that I am not on http://codegolf.stackexchange.com/
– VL-80
May 1 '14 at 12:50












The tags were removed from your question for a reason. These tags are not useful and add nothing of value to your question.
– Der Hochstapler
Jul 28 '14 at 11:04




The tags were removed from your question for a reason. These tags are not useful and add nothing of value to your question.
– Der Hochstapler
Jul 28 '14 at 11:04










6 Answers
6






active

oldest

votes

















up vote
6
down vote













This can be done easily with Powershell without any additional tools.



$x = Get-Content -Path data.txt; Set-Content -Path data.txt -Value ($x[($x.Length-1)..0])





share|improve this answer




























    up vote
    2
    down vote



    accepted










    Found the perfect tool for this: tac (part of CoreUtils for Windows)






    share|improve this answer

















    • 1




      Please read How to recommend software for minimum required information and other suggestions on how to recommend software on Super User. To keep your answer useful even if included link(s) breaks please edit these details into your answer.
      – Twisty Impersonator
      Nov 21 at 17:27


















    up vote
    1
    down vote













    Another powershell example. This just shows reversing. It would be trivial to export $x to a file.



    $x = Get-Content -Path .abc.txt
    [array]::Reverse($x)
    $x





    share|improve this answer




























      up vote
      0
      down vote













      In most cases a typical sort wont preserve your empty lines but will instead place them all together somewhere in your sort. If it was me I'd just copy and paste the lines in excel and sort there if it's just some one-off thing. Otherwise you'll be getting into some really fancy scripting to do this (but hey, it's possible to do).



      As for the script I'd first have some code that searches for blank lines and saves that location to an array. Then I'd sort the rest of the data, either using some built-in sort method or making my own "bubble sort" code. Once sorted, reintroduce the line breaks at the locations given and you'll be set.






      share|improve this answer




























        up vote
        0
        down vote













        I would read the file in reverse order (using a script, program, etc.) and write it to a 'new' file thus avoiding a sort. Then, delete the 'old' file and rename the 'new' one to the 'old' one.






        share|improve this answer




























          up vote
          0
          down vote













          Here's something native - a Visual Basic Script (.vbs).



          Const ForReading = 1
          Set objFSO = CreateObject("Scripting.FileSystemObject")
          infile = Wscript.Arguments(0)
          Set objTextFile = objFSO.OpenTextFile (infile, ForReading)
          strText = objTextFile.ReadAll
          objTextFile.Close
          arrText = Split(strText, vbCrLf)
          for l = ubound(arrText)-1 to 0 step -1
          wscript.echo arrText(l)
          Next


          save it as (e.g.) revfile.vbs and run it using the cscript engine. As written it echoes output to the console. To write the reversed lines to a file, use the > redirection operator like this



          cscript //nologo revfile.vbs "input.txt" > "output.txt"


          Use quotes around the file names/paths if they have spaces.



          C:Test>type input.txt
          apple
          bear
          cat
          dog
          egg
          fog
          gas
          hip

          ink
          joe
          kilo

          C:Test>type output.txt
          kilo
          joe
          ink

          hip
          gas
          fog
          egg
          dog
          cat
          bear
          apple





          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',
            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%2f748387%2fhow-to-reverse-a-text-file-on-windows%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








            up vote
            6
            down vote













            This can be done easily with Powershell without any additional tools.



            $x = Get-Content -Path data.txt; Set-Content -Path data.txt -Value ($x[($x.Length-1)..0])





            share|improve this answer

























              up vote
              6
              down vote













              This can be done easily with Powershell without any additional tools.



              $x = Get-Content -Path data.txt; Set-Content -Path data.txt -Value ($x[($x.Length-1)..0])





              share|improve this answer























                up vote
                6
                down vote










                up vote
                6
                down vote









                This can be done easily with Powershell without any additional tools.



                $x = Get-Content -Path data.txt; Set-Content -Path data.txt -Value ($x[($x.Length-1)..0])





                share|improve this answer












                This can be done easily with Powershell without any additional tools.



                $x = Get-Content -Path data.txt; Set-Content -Path data.txt -Value ($x[($x.Length-1)..0])






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 1 '14 at 14:26









                MFT

                45235




                45235
























                    up vote
                    2
                    down vote



                    accepted










                    Found the perfect tool for this: tac (part of CoreUtils for Windows)






                    share|improve this answer

















                    • 1




                      Please read How to recommend software for minimum required information and other suggestions on how to recommend software on Super User. To keep your answer useful even if included link(s) breaks please edit these details into your answer.
                      – Twisty Impersonator
                      Nov 21 at 17:27















                    up vote
                    2
                    down vote



                    accepted










                    Found the perfect tool for this: tac (part of CoreUtils for Windows)






                    share|improve this answer

















                    • 1




                      Please read How to recommend software for minimum required information and other suggestions on how to recommend software on Super User. To keep your answer useful even if included link(s) breaks please edit these details into your answer.
                      – Twisty Impersonator
                      Nov 21 at 17:27













                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    Found the perfect tool for this: tac (part of CoreUtils for Windows)






                    share|improve this answer












                    Found the perfect tool for this: tac (part of CoreUtils for Windows)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 2 '14 at 3:24









                    A. D.

                    16626




                    16626








                    • 1




                      Please read How to recommend software for minimum required information and other suggestions on how to recommend software on Super User. To keep your answer useful even if included link(s) breaks please edit these details into your answer.
                      – Twisty Impersonator
                      Nov 21 at 17:27














                    • 1




                      Please read How to recommend software for minimum required information and other suggestions on how to recommend software on Super User. To keep your answer useful even if included link(s) breaks please edit these details into your answer.
                      – Twisty Impersonator
                      Nov 21 at 17:27








                    1




                    1




                    Please read How to recommend software for minimum required information and other suggestions on how to recommend software on Super User. To keep your answer useful even if included link(s) breaks please edit these details into your answer.
                    – Twisty Impersonator
                    Nov 21 at 17:27




                    Please read How to recommend software for minimum required information and other suggestions on how to recommend software on Super User. To keep your answer useful even if included link(s) breaks please edit these details into your answer.
                    – Twisty Impersonator
                    Nov 21 at 17:27










                    up vote
                    1
                    down vote













                    Another powershell example. This just shows reversing. It would be trivial to export $x to a file.



                    $x = Get-Content -Path .abc.txt
                    [array]::Reverse($x)
                    $x





                    share|improve this answer

























                      up vote
                      1
                      down vote













                      Another powershell example. This just shows reversing. It would be trivial to export $x to a file.



                      $x = Get-Content -Path .abc.txt
                      [array]::Reverse($x)
                      $x





                      share|improve this answer























                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        Another powershell example. This just shows reversing. It would be trivial to export $x to a file.



                        $x = Get-Content -Path .abc.txt
                        [array]::Reverse($x)
                        $x





                        share|improve this answer












                        Another powershell example. This just shows reversing. It would be trivial to export $x to a file.



                        $x = Get-Content -Path .abc.txt
                        [array]::Reverse($x)
                        $x






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered May 13 '14 at 22:16









                        Jacob

                        1111




                        1111






















                            up vote
                            0
                            down vote













                            In most cases a typical sort wont preserve your empty lines but will instead place them all together somewhere in your sort. If it was me I'd just copy and paste the lines in excel and sort there if it's just some one-off thing. Otherwise you'll be getting into some really fancy scripting to do this (but hey, it's possible to do).



                            As for the script I'd first have some code that searches for blank lines and saves that location to an array. Then I'd sort the rest of the data, either using some built-in sort method or making my own "bubble sort" code. Once sorted, reintroduce the line breaks at the locations given and you'll be set.






                            share|improve this answer

























                              up vote
                              0
                              down vote













                              In most cases a typical sort wont preserve your empty lines but will instead place them all together somewhere in your sort. If it was me I'd just copy and paste the lines in excel and sort there if it's just some one-off thing. Otherwise you'll be getting into some really fancy scripting to do this (but hey, it's possible to do).



                              As for the script I'd first have some code that searches for blank lines and saves that location to an array. Then I'd sort the rest of the data, either using some built-in sort method or making my own "bubble sort" code. Once sorted, reintroduce the line breaks at the locations given and you'll be set.






                              share|improve this answer























                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                In most cases a typical sort wont preserve your empty lines but will instead place them all together somewhere in your sort. If it was me I'd just copy and paste the lines in excel and sort there if it's just some one-off thing. Otherwise you'll be getting into some really fancy scripting to do this (but hey, it's possible to do).



                                As for the script I'd first have some code that searches for blank lines and saves that location to an array. Then I'd sort the rest of the data, either using some built-in sort method or making my own "bubble sort" code. Once sorted, reintroduce the line breaks at the locations given and you'll be set.






                                share|improve this answer












                                In most cases a typical sort wont preserve your empty lines but will instead place them all together somewhere in your sort. If it was me I'd just copy and paste the lines in excel and sort there if it's just some one-off thing. Otherwise you'll be getting into some really fancy scripting to do this (but hey, it's possible to do).



                                As for the script I'd first have some code that searches for blank lines and saves that location to an array. Then I'd sort the rest of the data, either using some built-in sort method or making my own "bubble sort" code. Once sorted, reintroduce the line breaks at the locations given and you'll be set.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 1 '14 at 12:10









                                Bradley Forney

                                64158




                                64158






















                                    up vote
                                    0
                                    down vote













                                    I would read the file in reverse order (using a script, program, etc.) and write it to a 'new' file thus avoiding a sort. Then, delete the 'old' file and rename the 'new' one to the 'old' one.






                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote













                                      I would read the file in reverse order (using a script, program, etc.) and write it to a 'new' file thus avoiding a sort. Then, delete the 'old' file and rename the 'new' one to the 'old' one.






                                      share|improve this answer























                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        I would read the file in reverse order (using a script, program, etc.) and write it to a 'new' file thus avoiding a sort. Then, delete the 'old' file and rename the 'new' one to the 'old' one.






                                        share|improve this answer












                                        I would read the file in reverse order (using a script, program, etc.) and write it to a 'new' file thus avoiding a sort. Then, delete the 'old' file and rename the 'new' one to the 'old' one.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered May 1 '14 at 12:40









                                        rrirower

                                        617413




                                        617413






















                                            up vote
                                            0
                                            down vote













                                            Here's something native - a Visual Basic Script (.vbs).



                                            Const ForReading = 1
                                            Set objFSO = CreateObject("Scripting.FileSystemObject")
                                            infile = Wscript.Arguments(0)
                                            Set objTextFile = objFSO.OpenTextFile (infile, ForReading)
                                            strText = objTextFile.ReadAll
                                            objTextFile.Close
                                            arrText = Split(strText, vbCrLf)
                                            for l = ubound(arrText)-1 to 0 step -1
                                            wscript.echo arrText(l)
                                            Next


                                            save it as (e.g.) revfile.vbs and run it using the cscript engine. As written it echoes output to the console. To write the reversed lines to a file, use the > redirection operator like this



                                            cscript //nologo revfile.vbs "input.txt" > "output.txt"


                                            Use quotes around the file names/paths if they have spaces.



                                            C:Test>type input.txt
                                            apple
                                            bear
                                            cat
                                            dog
                                            egg
                                            fog
                                            gas
                                            hip

                                            ink
                                            joe
                                            kilo

                                            C:Test>type output.txt
                                            kilo
                                            joe
                                            ink

                                            hip
                                            gas
                                            fog
                                            egg
                                            dog
                                            cat
                                            bear
                                            apple





                                            share|improve this answer

























                                              up vote
                                              0
                                              down vote













                                              Here's something native - a Visual Basic Script (.vbs).



                                              Const ForReading = 1
                                              Set objFSO = CreateObject("Scripting.FileSystemObject")
                                              infile = Wscript.Arguments(0)
                                              Set objTextFile = objFSO.OpenTextFile (infile, ForReading)
                                              strText = objTextFile.ReadAll
                                              objTextFile.Close
                                              arrText = Split(strText, vbCrLf)
                                              for l = ubound(arrText)-1 to 0 step -1
                                              wscript.echo arrText(l)
                                              Next


                                              save it as (e.g.) revfile.vbs and run it using the cscript engine. As written it echoes output to the console. To write the reversed lines to a file, use the > redirection operator like this



                                              cscript //nologo revfile.vbs "input.txt" > "output.txt"


                                              Use quotes around the file names/paths if they have spaces.



                                              C:Test>type input.txt
                                              apple
                                              bear
                                              cat
                                              dog
                                              egg
                                              fog
                                              gas
                                              hip

                                              ink
                                              joe
                                              kilo

                                              C:Test>type output.txt
                                              kilo
                                              joe
                                              ink

                                              hip
                                              gas
                                              fog
                                              egg
                                              dog
                                              cat
                                              bear
                                              apple





                                              share|improve this answer























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                Here's something native - a Visual Basic Script (.vbs).



                                                Const ForReading = 1
                                                Set objFSO = CreateObject("Scripting.FileSystemObject")
                                                infile = Wscript.Arguments(0)
                                                Set objTextFile = objFSO.OpenTextFile (infile, ForReading)
                                                strText = objTextFile.ReadAll
                                                objTextFile.Close
                                                arrText = Split(strText, vbCrLf)
                                                for l = ubound(arrText)-1 to 0 step -1
                                                wscript.echo arrText(l)
                                                Next


                                                save it as (e.g.) revfile.vbs and run it using the cscript engine. As written it echoes output to the console. To write the reversed lines to a file, use the > redirection operator like this



                                                cscript //nologo revfile.vbs "input.txt" > "output.txt"


                                                Use quotes around the file names/paths if they have spaces.



                                                C:Test>type input.txt
                                                apple
                                                bear
                                                cat
                                                dog
                                                egg
                                                fog
                                                gas
                                                hip

                                                ink
                                                joe
                                                kilo

                                                C:Test>type output.txt
                                                kilo
                                                joe
                                                ink

                                                hip
                                                gas
                                                fog
                                                egg
                                                dog
                                                cat
                                                bear
                                                apple





                                                share|improve this answer












                                                Here's something native - a Visual Basic Script (.vbs).



                                                Const ForReading = 1
                                                Set objFSO = CreateObject("Scripting.FileSystemObject")
                                                infile = Wscript.Arguments(0)
                                                Set objTextFile = objFSO.OpenTextFile (infile, ForReading)
                                                strText = objTextFile.ReadAll
                                                objTextFile.Close
                                                arrText = Split(strText, vbCrLf)
                                                for l = ubound(arrText)-1 to 0 step -1
                                                wscript.echo arrText(l)
                                                Next


                                                save it as (e.g.) revfile.vbs and run it using the cscript engine. As written it echoes output to the console. To write the reversed lines to a file, use the > redirection operator like this



                                                cscript //nologo revfile.vbs "input.txt" > "output.txt"


                                                Use quotes around the file names/paths if they have spaces.



                                                C:Test>type input.txt
                                                apple
                                                bear
                                                cat
                                                dog
                                                egg
                                                fog
                                                gas
                                                hip

                                                ink
                                                joe
                                                kilo

                                                C:Test>type output.txt
                                                kilo
                                                joe
                                                ink

                                                hip
                                                gas
                                                fog
                                                egg
                                                dog
                                                cat
                                                bear
                                                apple






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 21 at 18:30









                                                Michael Harvey

                                                36637




                                                36637






























                                                    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.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • 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%2f748387%2fhow-to-reverse-a-text-file-on-windows%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

                                                    Plaza Victoria

                                                    Puebla de Zaragoza

                                                    Musa