How to reverse a text file on Windows?
up vote
3
down vote
favorite
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
add a comment |
up vote
3
down vote
favorite
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
(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
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
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
windows sorting
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
add a comment |
(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
add a comment |
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])
add a comment |
up vote
2
down vote
accepted
Found the perfect tool for this: tac (part of CoreUtils for Windows)
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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
add a comment |
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])
add a comment |
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])
add a comment |
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])
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])
answered May 1 '14 at 14:26
MFT
45235
45235
add a comment |
add a comment |
up vote
2
down vote
accepted
Found the perfect tool for this: tac (part of CoreUtils for Windows)
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
add a comment |
up vote
2
down vote
accepted
Found the perfect tool for this: tac (part of CoreUtils for Windows)
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
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Found the perfect tool for this: tac (part of CoreUtils for Windows)
Found the perfect tool for this: tac (part of CoreUtils for Windows)
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered May 13 '14 at 22:16
Jacob
1111
1111
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered May 1 '14 at 12:10
Bradley Forney
64158
64158
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered May 1 '14 at 12:40
rrirower
617413
617413
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 21 at 18:30
Michael Harvey
36737
36737
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
(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