I just realized that I could solve this problem by reversing line numbers from 1, 2, 3,...,n to n, n-1, ..., 3,2,1 and then use the same logic as earlier. So I want to know how can I reverse the order of lines?
|
For your particular problem, this is not the most efficient way to go about it, however, you can use any of the following to print a file with lines in reverse order, with varying degrees of portability (
On my system the fastest is
... so if you have |
|||||||||||
|
|
(In the case of the particular problem you mentioned the solutions there are better than reversing the entire file just so you can append a file and reverse the whole file again) |
|||||||||
|
|
GNU utilities (Linux, Cygwin) and BusyBox have the On systems that don't have
(Replace As already noted by Michael Mrozek, reversing lines is a poor way of prepending data to a file. The method is hard to understand, and performs a lot of extra work. |
||||
|