How to reverse a text file on Windows

How do I reverse the contents of a .txt file while 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.


Solution 1:

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])

Solution 2:

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

Solution 3:

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