Reverse a file

Reverse a file

am 10.12.2006 09:58:13 von Yaw Lim

The other day I had to reverse a file, i.e. to list a file from back to
front, last line becomes the first and so on. I wrote a script to do
it. I'm just wonder if there is a Unix command that can do it?

/Why Tea

Re: Reverse a file

am 10.12.2006 10:15:21 von cfajohnson

On 2006-12-10, Why Tea wrote:
> The other day I had to reverse a file, i.e. to list a file from back to
> front, last line becomes the first and so on. I wrote a script to do
> it. I'm just wonder if there is a Unix command that can do it?

There is no standard command for it, but there is a GNU utility
called tac. If it is not installed on your system, you can
download it from ftp.gnu.org (I don't know which package it is in,
probably coreutils or fileutils).

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Reverse a file

am 10.12.2006 10:49:11 von cichomitiko

"Why Tea" wrote ...
> The other day I had to reverse a file, i.e. to list a file from back to
> front, last line becomes the first and so on. I wrote a script to do
> it. I'm just wonder if there is a Unix command that can do it?

man tac

Or use awk:

awk '{a[NR]=$0}END{for(i=NR; i; --i)print a[i]}' file


Regards
Dimitre

Re: Reverse a file

am 10.12.2006 11:27:23 von Stephan Grein

Chris F.A. Johnson wrote:
> On 2006-12-10, Why Tea wrote:
>> The other day I had to reverse a file, i.e. to list a file from back to
>> front, last line becomes the first and so on. I wrote a script to do
>> it. I'm just wonder if there is a Unix command that can do it?
>
> There is no standard command for it, but there is a GNU utility
> called tac. If it is not installed on your system, you can
> download it from ftp.gnu.org (I don't know which package it is in,
> probably coreutils or fileutils).
>
It's in coreutils.

--
Stephan Grein,
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4

Re: Reverse a file

am 10.12.2006 11:44:46 von Stephane CHAZELAS

2006-12-10, 11:27(+01), Stephan Grein:
> Chris F.A. Johnson wrote:
>> On 2006-12-10, Why Tea wrote:
>>> The other day I had to reverse a file, i.e. to list a file from back to
>>> front, last line becomes the first and so on. I wrote a script to do
>>> it. I'm just wonder if there is a Unix command that can do it?
>>
>> There is no standard command for it, but there is a GNU utility
>> called tac. If it is not installed on your system, you can
>> download it from ftp.gnu.org (I don't know which package it is in,
>> probably coreutils or fileutils).
>>
> It's in coreutils.

coreutils englobes what used to be the fileutils, shutils,
textutils...

Some unices have tail -r for that

--
Stéphane

Re: Reverse a file

am 10.12.2006 11:58:26 von Dietrich Schaffer

Chris F.A. Johnson wrote:
> On 2006-12-10, Why Tea wrote:
>> The other day I had to reverse a file, i.e. to list a file from back to
>> front, last line becomes the first and so on. I wrote a script to do
>> it. I'm just wonder if there is a Unix command that can do it?
>
> There is no standard command for it, but there is a GNU utility
> called tac. If it is not installed on your system, you can
> download it from ftp.gnu.org (I don't know which package it is in,
> probably coreutils or fileutils).

,----[ ...sed1line.txt... ]
| # reverse order of lines (emulates "tac")
| # bug/feature in HHsed v1.5 causes blank lines to be deleted
| sed '1!G;h;$!d' # method 1
| sed -n '1!G;h;$p' # method 2
`----

Bye,
Dietrich

Re: Reverse a file

am 10.12.2006 18:54:31 von Warren Block

Why Tea wrote:
> The other day I had to reverse a file, i.e. to list a file from back to
> front, last line becomes the first and so on. I wrote a script to do
> it. I'm just wonder if there is a Unix command that can do it?

tail -r can reverse a file by line, although it appears to be mostly
found in BSD versions of tail.

--
Warren Block * Rapid City, South Dakota * USA

Re: Reverse a file

am 10.12.2006 19:08:38 von William James

Why Tea wrote:
> The other day I had to reverse a file, i.e. to list a file from back to
> front, last line becomes the first and so on. I wrote a script to do
> it. I'm just wonder if there is a Unix command that can do it?
>
> /Why Tea

ruby -e 'puts ARGF.to_a.reverse'

Re: Reverse a file

am 10.12.2006 19:50:37 von merlyn

>>>>> "Why" == Why Tea writes:

Why> The other day I had to reverse a file, i.e. to list a file from back to
Why> front, last line becomes the first and so on. I wrote a script to do
Why> it. I'm just wonder if there is a Unix command that can do it?

perl -e 'print reverse <>' filename

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
Posted via a free Usenet account from http://www.teranews.com

Re: Reverse a file

am 11.12.2006 03:29:28 von Bruce Barnett

"Why Tea" writes:

> The other day I had to reverse a file, i.e. to list a file from back to
> front, last line becomes the first and so on. I wrote a script to do
> it. I'm just wonder if there is a Unix command that can do it?

My Linux systems have rev(1) - marked BSD 1992.


--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

Re: Reverse a file

am 11.12.2006 03:39:58 von cfajohnson

On 2006-12-11, Bruce Barnett wrote:
> "Why Tea" writes:
>
>> The other day I had to reverse a file, i.e. to list a file from back to
>> front, last line becomes the first and so on. I wrote a script to do
>> it. I'm just wonder if there is a Unix command that can do it?
>
> My Linux systems have rev(1) - marked BSD 1992.

That reverses the characters on each line, not the order of the
lines.

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Reverse a file

am 11.12.2006 08:36:47 von Yaw Lim

Thanks to all who posted. I especially enjoy the one liner solutions
for perl and awk. I haven't tried ruby though. I just tried "tail -r"
on Solaris and it worked.

/Why Tea

Re: Reverse a file

am 12.12.2006 00:26:24 von Bruce Barnett

"Chris F.A. Johnson" writes:

>> My Linux systems have rev(1) - marked BSD 1992.
>
> That reverses the characters on each line, not the order of the
> lines.

Aaaaahhhhh!

Thanks, Chris.

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

Re: Reverse a file

am 12.12.2006 08:51:32 von Yaw Lim

Why Tea wrote:
> Thanks to all who posted. I especially enjoy the one liner solutions
> for perl and awk. I haven't tried ruby though. I just tried "tail -r"
> on Solaris and it worked.
>
> /Why Tea

By the way, so far we have solutions with perl, ruby, awk, etc. How
about Tcl and Python??

Re: Reverse a file

am 12.12.2006 09:13:27 von cfajohnson

On 2006-12-12, Why Tea wrote:
>
> Why Tea wrote:
>> Thanks to all who posted. I especially enjoy the one liner solutions
>> for perl and awk. I haven't tried ruby though. I just tried "tail -r"
>> on Solaris and it worked.
>>
>> /Why Tea
>
> By the way, so far we have solutions with perl, ruby, awk, etc. How
> about Tcl and Python??

If you search the archives you will find a pure shell version (that
I don't seriously recommend) that I posted a long time ago (this is
an OAQ, if not an FAQ).

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Reverse a file

am 12.12.2006 17:13:28 von Glenn Jackman

At 2006-12-12 02:51AM, "Why Tea" wrote:
>
> Why Tea wrote:
> > Thanks to all who posted. I especially enjoy the one liner solutions
> > for perl and awk. I haven't tried ruby though. I just tried "tail -r"
> > on Solaris and it worked.
> >
> > /Why Tea
>
> By the way, so far we have solutions with perl, ruby, awk, etc. How
> about Tcl and Python??

Tcl is not build for command line one-liners. It also does not have a
built-in list reversal command. Nevertheless, here goes:

#! /usr/bin/env tclsh
#
# tac.tcl -- reverse the lines in files specified on the cmd line

proc lreverse aList {
set rev [list]
for {set i [llength $aList]} {$i > 0} {} {
lappend rev [lindex $aList [incr i -1]]
}
return $rev
}

foreach filename $argv {
set fid [open $filename r]
puts [join [lreverse [split [read -nonewline $fid] \n]] \n]
close $fid
}


--
Glenn Jackman
Ulterior Designer

Re: Reverse a file

am 12.12.2006 17:23:27 von Klaus Alexander Seistrup

Why Tea wrote:

> By the way, so far we have solutions with perl, ruby, awk, etc.
> How about Tcl and Python??

A python solution could look like this:

python -c "import sys; print ''.join(sys.stdin.readlines()[::-1]),"

Cheers,

--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/

Re: Reverse a file

am 12.12.2006 19:04:15 von Yaw Lim

>
> Tcl is not build for command line one-liners. It also does not have a
> built-in list reversal command. Nevertheless, here goes:
>
> #! /usr/bin/env tclsh
> #
> # tac.tcl -- reverse the lines in files specified on the cmd line
>
> proc lreverse aList {
> set rev [list]
> for {set i [llength $aList]} {$i > 0} {} {
> lappend rev [lindex $aList [incr i -1]]
> }
> return $rev
> }
>
> foreach filename $argv {
> set fid [open $filename r]
> puts [join [lreverse [split [read -nonewline $fid] \n]] \n]
> close $fid
> }

Glenn,

That's beautifully done. I wrote a version that did the job, but it
looked clumsy. Thanks.

/Why Tea