sort a file in reverse order

I a file with log entries... I want to sort it so that the last line in the file is first and the first line is last..

eg.
Sample file
1
h
a
f
8
6

After sort should look like
6
8
f
a
h
1

Is this possible?

tail -r filename 

rishi :cool:

Its always something simple ehh.. I was playing around with sort -r but that was doing too much sorting..

Thanks for that!!!

Yet another way.

The actual sample file.

$ cat sample.file 
1
h
a
f
8
6

The reversed sample.file

$ tac sample.file 
6
8
f
a
h
1

vino

I think its better to use sort itself...

sort -r <filename>

I didn't c an option " -r " with tail...

hey vino,
tac is not available under solaris 5.8 :frowning:

rishi

And as RishiPahuja has told, tail also works but with -n option

Code : tail -n filename

Abey,Seems these options are not in confirmation to any standards :slight_smile: as I told the "-r" option works with solaris 5.8, which version of unix uses "-n" option?

rishi

mine is HP-Unix 11, tht tac didn't worked on mine too....

abey

Here is a non-tac solution. Courtesy sed one-liners

Hope you have a version of sed which will make the following work.

sed '1!G;h;$!d' sample.file

and yet another sed solution

sed -n '1!G;h;$p'  sample.file

vino

Rishi, how very true! :stuck_out_tongue:

"tac" is a non-standard utility but linux at least has it.
"tail -r" is not in Posix, but Solaris has it (even SunOS 5.5.1 has it... I don't have access to an earlier version.)
"sort -r" will sort based on the content of the lines
"tail -n" requires an integer argument and does not reverse the lines

Those sed scripts are standard but they will overflow the size of the hold space for large files. HP-UX documents this as 8192 characters. Both SunOS and HP-UX coredump on large files. The scripts are very clever though! :slight_smile:

Finally, I'm glad to see that no one wasted any time trying our search function. Because this thread is the very first time that anyone has encountered the problem of reversing the lines in a file, there is no point in trying to find an earlier solution. NOT! reversing a list

Yes, when dealing with large files it usually comes down to perl, so that being said:

perl -e 'print reverse <>' < file