Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to reverse the order of multiline text sections separated by blank newlines/line breaks...

..., with a simple command or application, preferably without programming?

I don't want a sort, but a reverse order of the text sections only.

For example:

A
B

0
1

X
YX
Y

HTTP
FTP

becomes

HTTP
FTP

X
YX
Y

0
1

A
B
share|improve this question
    
Sorry that I haven't accepted any answers yet. I have been away for awhile. I accepted peth's solution because it is the simplest. I upvoted all working solutions. Thanks, everyone. – XP1 May 4 '11 at 4:50
up vote 2 down vote accepted

tac accepts a separator, so in e.g. bash you can do:

tac -s $'\n\n' foo

which will produce slightly incorrect output because the last paragraph isn't terminated by a double newline. So,

(cat foo; echo) | tac -s $'\n\n' | sed '$d'

is probably what you'd want. (Pipe contents of foo and newline through tac, then sed to strip the last line.)

Note: Do not redirect this into > foo again as you could end up with an empty file.

share|improve this answer
    
+1. It works. For future reference, "foo" is the filename. To output to file, I just redirected it to a new file with this command: "(cat foo; echo) | tac -s $'\n\n' | sed '$d' > foo1.txt". – XP1 May 4 '11 at 4:48

Although the author doesn't prefer programming solutions, using Python would be fairly easy in 1 line:

open('output.txt', 'w').write('\n\n'.join(reversed(open('input.txt', 'r').read().split('\n\n'))))

Obviously replace input.txt and output.txt with the desired files, and change \n to \r\n for Windows-formatted text files.

Put this in a .py file and call it via python myscript.py.

share|improve this answer
    
+1, I like the one-liner. Python really allows some cool stuff – Siim K Apr 29 '11 at 13:05
    
+1. It works.`` – XP1 May 4 '11 at 4:48

If you're using Windows then you could simply use Powershell. This reverses the contents of test.txt and saves the result to test2.txt:

$f=Get-Content c:\folder\test.txt
[array]::Reverse($f)
$f | out-file c:\folder\test2.txt

Update:

This should do the trick (reads all the lines from test.txt and rearranges them to test2.txt file the way you wanted):

$content = [System.IO.File]::ReadAllLines("C:\Folder\test.txt")
$a = New-Object System.Collections.ArrayList
$i=0
ForEach ($line in $content) {If ($line -ne "") {$a.insert($i++, $line)} else {$a.insert(0, $line); $i=0}}
$a | out-file C:\Folder\test2.txt

Probably not the most elegant way but it did the job with your test case.

share|improve this answer
    
Unfortunately, the command also reverses the order of the individual lines within the text sections, which is not what I want. For example, the order for "A, B" becomes "B, A", where the comma and space are a newline. – XP1 Apr 27 '11 at 20:56
    
@XP1: I assume therefore this is Windows ? – BigChief Apr 28 '11 at 21:32
    
@BigChief, I am on Windows Vista, but I will accept solutions for either Windows or GNU/Linux-based OSes. Even though I said that I prefer a solution without programming, I will accept programming as long as the solution is simple (for example, a one-liner or a couple lines). – XP1 Apr 29 '11 at 0:12
    
@XP1: sorry, misread the original question yesterday. – Siim K Apr 29 '11 at 13:00
    
+1. The updated commands work. – XP1 May 4 '11 at 4:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .