PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Reverse a file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Reverse a file

Réponse
 
LinkBack Outils de la discussion
Vieux 10/12/2006, 08h58   #1
Why Tea
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Reverse a file

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

  Réponse avec citation
Vieux 10/12/2006, 09h15   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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
  Réponse avec citation
Vieux 10/12/2006, 09h49   #3
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file


"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


  Réponse avec citation
Vieux 10/12/2006, 10h27   #4
Stephan Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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
  Réponse avec citation
Vieux 10/12/2006, 10h44   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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
  Réponse avec citation
Vieux 10/12/2006, 10h58   #6
Dietrich Schaffer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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
  Réponse avec citation
Vieux 10/12/2006, 17h54   #7
Warren Block
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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
  Réponse avec citation
Vieux 10/12/2006, 18h08   #8
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file


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'

  Réponse avec citation
Vieux 10/12/2006, 18h50   #9
Randal L. Schwartz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

>>>>> "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

  Réponse avec citation
Vieux 11/12/2006, 02h29   #10
Bruce Barnett
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

"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.
  Réponse avec citation
Vieux 11/12/2006, 02h39   #11
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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
  Réponse avec citation
Vieux 11/12/2006, 07h36   #12
Why Tea
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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

  Réponse avec citation
Vieux 11/12/2006, 23h26   #13
Bruce Barnett
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

"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.
  Réponse avec citation
Vieux 12/12/2006, 07h51   #14
Why Tea
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file


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??

  Réponse avec citation
Vieux 12/12/2006, 08h13   #15
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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
  Réponse avec citation
Vieux 12/12/2006, 16h13   #16
Glenn Jackman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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
  Réponse avec citation
Vieux 12/12/2006, 16h23   #17
Klaus Alexander Seistrup
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

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/
  Réponse avec citation
Vieux 12/12/2006, 18h04   #18
Why Tea
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reverse a file

>
> 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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,24096 seconds with 26 queries