Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question

migrated from stackoverflow.com Apr 15 '13 at 21:18

This question came from our site for professional and enthusiast programmers.

1  
Why batch? Do you enjoy pain? It's not more than 10 lines in any decent scripting language. –  David Heffernan Apr 15 '13 at 11:19
    
Other languages (c++, java, perl, VB,...) I don't understand :/ –  user2282194 Apr 15 '13 at 12:36
    
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. –  David Heffernan Apr 15 '13 at 12:38
    
Duplicate of superuser.com/questions/582996 –  Karan Apr 15 '13 at 22:16

3 Answers 3

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.

share|improve this answer
3  
Just imagine how long this will take for a large file! This feels like Towers of Hanoi text processing! –  David Heffernan Apr 15 '13 at 11:38
1  
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 '13 at 11:43
    
I've edited my post. New code is much faster comparing to the first one. –  ghost Apr 15 '13 at 12:15
1  
Nice. Have a +1. Batch is just masochism. –  David Heffernan Apr 15 '13 at 12:16
    
GHOST: It is work very well! It is it what I need. Thank You for your help. –  user2282194 Apr 15 '13 at 12:44

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.

share|improve this answer
    
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. –  David Heffernan Apr 15 '13 at 19:50
1  
Tac is not a Windows tool though, so some form of add-in would still be required. –  AthomSfere Apr 15 '13 at 21:35
    
Sure it's not part of Windows. But it's part of the GNU tools and who can live without them? –  David Heffernan Apr 15 '13 at 21:47

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.