|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 <http://cfaj.freeshell.org/shell> 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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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, <stephan at stephan minus rockt dot de> https://stephan-rockt.de GnuPG-Key-ID: 0xF8C275D4 FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Chris F.A. Johnson <cfajohnson@gmail.com> 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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Why Tea <ytlim1@gmail.com> 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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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' |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
>>>>> "Why" == Why Tea <ytlim1@gmail.com> 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 <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/> 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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
"Why Tea" <ytlim1@gmail.com> 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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
On 2006-12-11, Bruce Barnett wrote:
> "Why Tea" <ytlim1@gmail.com> 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 <http://cfaj.freeshell.org/shell> 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 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
"Chris F.A. Johnson" <cfajohnson@gmail.com> 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. |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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?? |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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 <http://cfaj.freeshell.org/shell> 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 |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
>
> 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 |
|
![]() |
| Outils de la discussion | |
|
|