4

Please help me, how reverse/sort row in text file with batch in Windows?
Example:
I have text in this format

15/04/2013-07:10:30 lalala
15/04/2013-07:10:30 Text text
15/04/2013-07:10:30 1 lala Text

15/04/2013-07:10:36 8 text lala X
15/04/2013-07:10:36 A text lala 1

17/04/2013-10:11:12 B bext lala 4
... (other rows)

and I need reversed it in this format

17/04/2013-10:11:12 B bext lala 4
15/04/2013-07:10:36 A text lala 1
15/04/2013-07:10:36 8 text lala X
15/04/2013-07:10:30 1 lala Text
15/04/2013-07:10:30 Text text
15/04/2013-07:10:30 lalala
... (other rows)

Thank You for Your help!

6
  • 3
    Why batch? Do you enjoy pain? It's not more than 10 lines in any decent scripting language. Apr 15, 2013 at 11:19
  • Other languages (c++, java, perl, VB,...) I don't understand :/
    – user2282194
    Apr 15, 2013 at 12:36
  • 1
    I'm thinking of a scripting language. My personal favourite is Python. Other people like Perl or Ruby or Lua. There are others. Even VBscript is better than batch. Apr 15, 2013 at 12:38
  • Duplicate of superuser.com/questions/582996
    – Karan
    Apr 15, 2013 at 22:16
  • Possible duplicate of superuser.com/questions/748387/… ? Dec 7, 2016 at 20:29

5 Answers 5

13

Maybe it's not the prettiest way, but it's just simple and works as you want.

echo. > output.txt

for /f  "delims=@" %%j in (yourfile.txt) do (
    type output.txt > tmp
    echo %%j > output.txt
    type tmp >> output.txt
)

del tmp

Don't use the example above if you want to process large files. It's really time and resources consuming solution. Here you have faster version I just prepared:

setlocal enabledelayedexpansion

set I=0

for /F "tokens=*" %%k in (yourfile.txt) do (
  set /A I=!I! + 1
  set LINE!I!=%%k
)

for /L %%c in (!I!,-1,1) do (
  echo !LINE%%c! >> out.txt
)

Reversing 40kb file (10k lines, 1 character in each) took ~1 minute on my machine. Remember it's still only batch. There are many better scripting or programming languages that would be better to perform that operation.

5
  • 4
    Just imagine how long this will take for a large file! This feels like Towers of Hanoi text processing! Apr 15, 2013 at 11:38
  • 2
    Well, I told it's not the best way. I just came with a simple idea for processing small files. It's his problem that he wants to use batch for such an operation :) The worst choice in my opinion.
    – ghost
    Apr 15, 2013 at 11:43
  • I've edited my post. New code is much faster comparing to the first one.
    – ghost
    Apr 15, 2013 at 12:15
  • 2
    Nice. Have a +1. Batch is just masochism. Apr 15, 2013 at 12:16
  • GHOST: It is work very well! It is it what I need. Thank You for your help.
    – user2282194
    Apr 15, 2013 at 12:44
2
perl -e "print reverse <>" file > file_reversed

Note that on Windows you will have to use double quotes instead of single quotes to delimit the perl string.

5
  • 1
    Windows has no perl command. The question specifies Windows.
    – user477799
    Jan 20, 2017 at 14:00
  • Three other people mentioned perl here so I assumed it was ok.
    – Kjetil S.
    Jan 21, 2017 at 19:09
  • Answers in this website must be self-contained. That means you are obligated to mention that perl is not included in Windows and optionally, include a hyperlink to download and install it. But most importantly you should have stated the prerequisites for running a script with perl.
    – user477799
    Jan 22, 2017 at 5:39
  • I had perl on my Windows system so this was handy. Jun 26, 2017 at 6:37
  • Thank you - in spite of the by-the-book complaints this is the best answer here. May 2, 2018 at 21:10
1

I thought I would add this as a second approach, as it might work better for some:

If you are open to something you can call, instead of batch logic:

This is a .NET application that you drop to %Systemroot% and call just like any other command in a batch, .bat, .cmd etc.1

Usage looks like: Reverse "C:\Path\File.txt" enter image description here

And, yes, I own the thing. It seems easier to me to call this than write the logic out. It will replace the file contents, at present at least and not create a second file.

4
  • 2
    Personally I'd sooner use the *nix tools for this. For example tac does exactly what you need. Really no point re-inventing the wheel. Apr 15, 2013 at 19:50
  • 2
    Tac is not a Windows tool though, so some form of add-in would still be required. Apr 15, 2013 at 21:35
  • Sure it's not part of Windows. But it's part of the GNU tools and who can live without them? Apr 15, 2013 at 21:47
  • 1
    This doesn't work with very large files - obviously reads whole file into memory.
    – Betty
    Jun 2, 2016 at 19:56
1

sort /r yourfile.txt > out.txt

This will put out all lines in yourfile.txt in reverse alphabetical order. Note that this may differ from putting out all lines in yourfile.txt in actual reverse order.

-1

exactly 10 lines perl, taking into account that nothing will be done if output file already exists:

#!/usr/bin/perl
usage() if( !$ARGV[0] );
my $f_out = my $f_in = $ARGV[0];
$f_out =~ s/(\..{1,3})?$/.reverse$1/i;
open(TXTIN, "<$f_in") || usage("Can't read file [$f_in]");
my @in = <TXTIN>; close(TXTIN);
usage( "File already exists, nothing done [$f_out]" ) if( -e $f_out );
open(TXTOUT, ">$f_out"); print TXTOUT join( '', reverse( @in ) ); close(TXTOUT);
print "File reverse written [$f_out]\n";
sub usage { print "$_[0]\nUsage: [perl] rev2.pl (filename)\n"; exit; }
4
  • 1
    Can you provide some explanation on what this code does? Please see How to Answer.
    – Burgi
    Dec 7, 2016 at 15:20
  • 1
    Welcome to Super User! While this may answer the question, it would be a better answer if you could provide some explanation why it does so.
    – DavidPostill
    Dec 7, 2016 at 15:31
  • Please explain what your perl script does exactly. You should also properly format the script, please make the code readable, over making it compact.
    – Ramhound
    Dec 7, 2016 at 16:01
  • Also, Windows doesn't have PERL, so you'll need to explain why it's necessary to install extra software if a default program will work. Jan 20, 2017 at 22:16

You must log in to answer this question.