How to reverse text file in Windows

4

1

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!

user2282194

Posted 2013-04-15T10:47:51.800

Reputation:

Possible duplicate of http://superuser.com/questions/748387/how-to-reverse-a-text-file-windows-7 ?

– Charles Burge – 2016-12-07T20:29:35.003

If it was an actual reversal the ... (other rows) would be at the top. In addition you removed some blank lines. – Seth – 2017-01-20T11:15:44.803

2Why batch? Do you enjoy pain? It's not more than 10 lines in any decent scripting language. – David Heffernan – 2013-04-15T11:19:10.333

Other languages (c++, java, perl, VB,...) I don't understand :/ – None – 2013-04-15T12:36:58.647

1I'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. – David Heffernan – 2013-04-15T12:38:27.373

Duplicate of http://superuser.com/questions/582996

– Karan – 2013-04-15T22:16:52.757

Answers

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.

ghost

Posted 2013-04-15T10:47:51.800

Reputation: 231

4Just imagine how long this will take for a large file! This feels like Towers of Hanoi text processing! – David Heffernan – 2013-04-15T11:38:18.430

2Well, 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. – None – 2013-04-15T11:43:36.320

I've edited my post. New code is much faster comparing to the first one. – None – 2013-04-15T12:15:38.040

2Nice. Have a +1. Batch is just masochism. – David Heffernan – 2013-04-15T12:16:42.903

GHOST: It is work very well! It is it what I need. Thank You for your help. – None – 2013-04-15T12:44:15.413

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.

Kjetil S.

Posted 2013-04-15T10:47:51.800

Reputation: 259

1Windows has no perl command. The question specifies Windows. – None – 2017-01-20T14:00:05.137

Three other people mentioned perl here so I assumed it was ok. – Kjetil S. – 2017-01-21T19:09:37.430

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. – None – 2017-01-22T05:39:22.637

I had perl on my Windows system so this was handy. – Matthew Lock – 2017-06-26T06:37:37.853

Thank you - in spite of the by-the-book complaints this is the best answer here. – Michael Burr – 2018-05-02T21:10:08.560

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.

Austin T French

Posted 2013-04-15T10:47:51.800

Reputation: 9 766

1This doesn't work with very large files - obviously reads whole file into memory. – Betty – 2016-06-02T19:56:08.903

1Personally I'd sooner use the *nix tools for this. For example tac does exactly what you need. Really no point re-inventing the wheel. – David Heffernan – 2013-04-15T19:50:19.830

2Tac is not a Windows tool though, so some form of add-in would still be required. – Austin T French – 2013-04-15T21:35:50.727

Sure it's not part of Windows. But it's part of the GNU tools and who can live without them? – David Heffernan – 2013-04-15T21:47:55.230

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.

VeggieVampire

Posted 2013-04-15T10:47:51.800

Reputation: 27

-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; }

user277068

Posted 2013-04-15T10:47:51.800

Reputation: 1

1Can you provide some explanation on what this code does? Please see [answer]. – Burgi – 2016-12-07T15:20:04.170

1Welcome 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 – 2016-12-07T15:31:11.943

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 – 2016-12-07T16:01:12.820

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. – music2myear – 2017-01-20T22:16:26.823