Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > One Liner to reverse sort file

Reply
Thread Tools

One Liner to reverse sort file

 
 
hogg
Guest
Posts: n/a
 
      03-09-2005
I was wondering if there was a one liner that would reverse order a
file w/o having to write the output to another file. I can reverse
order a file using: awk '{printf "%10d\t%s\n",NR,$0}' <FILE> | sort -nr
| cut -f2- >> <FILE2>, but this makes me create another file. I wanted
to be able to do it 'on the fly'.
Any suggestions?

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      03-09-2005
In article <(E-Mail Removed) .com>,
hogg <(E-Mail Removed)> wrote:
:I was wondering if there was a one liner that would reverse order a
:file w/o having to write the output to another file.

my @revsortedfile = sort { $b cmp $a } <FILE>;
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-09-2005
"hogg" <(E-Mail Removed)> wrote:
> I was wondering if there was a one liner that would reverse order a
> file w/o having to write the output to another file. I can reverse
> order a file using: awk '{printf "%10d\t%s\n",NR,$0}' <FILE> | sort -nr
> | cut -f2- >> <FILE2>,


No need for all of that, at least not on modern Linux:

tac FILE > FILE2

> but this makes me create another file. I wanted
> to be able to do it 'on the fly'.


It does not make you create another file, that is what you chose to
do. You could certainly pipe the output into just about anything you want,
rather than redirecting it to a file.

> Any suggestions?


You were so busy telling us what you didn't want to do, you failed to tell
us what you do want to do.

If you want the reversed data as input to perl, you can do:

open my $fh, "tac File |" or die $!;
while (<$fh>) {
#do whatever
}
close $fh or die $!;

If you want the reversed data written back to the same file, you could tie
the file to an array with Tie::File, and then just reverse the array. But
that would probably be ungodly slow. You could study Tie::File code and
then make your own module which does what you want quickly, and then invoke
that module with a one-liner. Come to think of it, you could just read the
file into an array, reverse the array, and print it back out, assuming the
whole file fit into memory. Leaving perl and going back to Linux, you
could do (in tcsh, doesn't seem to work in bash):

tac FILE | ( rm FILE ; cat - > FILE )

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-09-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> If you want the reversed data written back to the same file, you could
> tie the file to an array with Tie::File, and then just reverse the array.


Come to think of it, that would also require the whole file to be memory
at once, unless you looped through the array reversing it in place.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      03-10-2005
hogg wrote:
> I was wondering if there was a one liner that would reverse order a
> file w/o having to write the output to another file. I can reverse
> order a file using: awk '{printf "%10d\t%s\n",NR,$0}' <FILE> | sort -nr
> | cut -f2- >> <FILE2>, but this makes me create another file. I wanted
> to be able to do it 'on the fly'.
> Any suggestions?


Something like this?

perl -lp0 -i -e '$_ = join "\n" => reverse split /\n/' in.file

or a bit shorter:

perl -aF"\n" -lp0 -i -e '$_=join$\,reverse@F' in.file

--Ala
 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      03-10-2005
> hogg wrote:
> >
> > I was wondering if there was a one liner that would reverse
> > order a file w/o having to write the output to another file.
> > I can reverse order a file using:
> > awk '{printf "%10d\t%s\n",NR,$0}' <FILE> | sort -nr
> > | cut -f2- >> <FILE2>, but this makes me create another file.
> > I wanted to be able to do it 'on the fly'.
> > Any suggestions?


Ala Qumsieh replied:
>
> Something like this?
>
> perl -lp0 -i -e '$_ = join "\n" => reverse split /\n/' in.file
>
> or a bit shorter:
>
> perl -aF"\n" -lp0 -i -e '$_=join$\,reverse@F' in.file



If I remember correctly, there was an even shorter solution given as
an answer to a problem in Randal L. Schwartz & Tom Phoenix's book
"Learning Perl":

perl -e "print reverse <>"

Quite elegant, in my opinion.

-- Jean-Luc Romano

 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      03-10-2005
(E-Mail Removed) wrote:

>>hogg wrote:
>>
>>>I was wondering if there was a one liner that would reverse
>>>order a file w/o having to write the output to another file.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

> If I remember correctly, there was an even shorter solution given as
> an answer to a problem in Randal L. Schwartz & Tom Phoenix's book
> "Learning Perl":
>
> perl -e "print reverse <>"
>
> Quite elegant, in my opinion.


Except that it doesn't fulfill the OP's requirement of in-place editing
of the file.

--Ala
 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      03-11-2005
> >>hogg wrote:
> >>
> >>>I was wondering if there was a one liner that would reverse
> >>>order a file w/o having to write the output to another file.

> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


(E-Mail Removed) replied:
>
> > If I remember correctly, there was an even shorter
> > solution given as an answer to a problem in
> > Randal L. Schwartz & Tom Phoenix's book "Learning Perl":
> >
> > perl -e "print reverse <>"


Ala Qumsieh replied:
>
> Except that it doesn't fulfill the OP's requirement of
> in-place editing of the file.



Oh, so in-place editing was what the original poster meant by
"without having to write the output to another file." (Before I
learned Perl, I would use sed and awk quite extensively. On occasion,
I would have to write out an intermediate file because of some logical
limitation that wouldn't let me chain awk and sed commands
indefinitely. I thought the original poster was talking about this
logical limitation. I was wondering why nobody had mentioned my
solution.)

To redeem myself, let me offer another solution, one that does what
I think the original poster wanted:

perl -pi -e 'unshift @a,$_; $_=""; print @a if eof' in.file

(I know that a solution isn't needed anymore, but I thought I might
offer yet another solution just for solution's sake.)

-- Jean-Luc

 
Reply With Quote
 
Wes Groleau
Guest
Posts: n/a
 
      03-11-2005
>>>>>I was wondering if there was a one liner that would reverse
>>>>>order a file w/o having to write the output to another file.

>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


> Oh, so in-place editing was what the original poster meant by
> "without having to write the output to another file." (Before I
> [snip]
> To redeem myself, let me offer another solution, one that does what
> I think the original poster wanted:
>
> perl -pi -e 'unshift @a,$_; $_=""; print @a if eof' in.file


FWIW, perl in-place editing actually does write to a temporary file,
then renames it and deletes the original. Saves you the trouble of
writing explicit code for that, but that's what it does behind the
scenes. Anything that can stop you from doing it explicitly can stop
perl from doing it. (Full disk, for one example)

--
Wes Groleau

Armchair Activism: http://www.breakthechain.org/armchair.html
 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      03-14-2005
(E-Mail Removed) wrote:
> "hogg" <(E-Mail Removed)> wrote:
>> I was wondering if there was a one liner that would reverse order a
>> file w/o having to write the output to another file. I can reverse
>> order a file using: awk '{printf "%10d\t%s\n",NR,$0}' <FILE> | sort -nr
>> | cut -f2- >> <FILE2>,


> No need for all of that, at least not on modern Linux:


> tac FILE > FILE2


Or for those lacking tac:

% tail -r

Axel

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File.delete one-liner David Stanford Ruby 8 02-05-2009 10:06 PM
Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute cjt22@bath.ac.uk Python 7 09-10-2007 11:10 AM
perl one-liner question: add file into text. ahq_2001 Perl Misc 2 05-17-2005 09:04 AM
one-liner to make all programs one-liners Larry Perl Misc 1 02-03-2005 11:35 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments