hubertf's NetBSD Blog
Send interesting links to hubert at feyrer dot de!
[20050729] Reversing a file line-by-line (Update #2)
I've been searching for a tool to reverse the contents of a file line-by line (i.e. print last line first, etc.) for some time, and found by accident that NetBSD's "tail -r" can do that. Whee! (Pity that's non in SUSv3)

Update: Julien Gabel pointed me at an article "Eight Ways to Reverse a File". I knew GNU's 'tac' was there, and all the other options use some other tools (perl, vi, sed, etc.). Now I find it funny that the solution I used before I knew "tail -r" isn't listed there: cat -n file | sort -rn | some sed stuff to get rid off the line numbers.

Update #2: Martin Neitzel pointed me at another, imho very cute way to solve the problem with an ed/ex command, following the basic idea of marking every line, and then moving it to the beginning. Here's how to do it in vi (load any file first): g/./m0 or g/^/m0. Simple, hu? And doesn't need another tool installed. :)

[Tags: ]

Disclaimer: All opinion expressed here is purely my own. No responsibility is taken for anything.

Access count: 24529030
Copyright (c) Hubert Feyrer