Re: sort by line length

2007-04-24 Thread Eric Blake
According to Georg Müller on 4/24/2007 6:22 AM:
 Dear coreutils team,
 
 I would like to see a sort by line length in the sort tool.
 
 Is anything like this planned.

Not really planned, but it doesn't sound all that bad to me.  Submit a
patch for discussion, if you would like.  But it sounds non-trivial (don't
forget the documentation and testsuite), so you will need copyright
assignment on file.

 It seems easy to implement.
 I could also write the code for that, if you wish.

It may be possible to already do this with existing tools, although
certainly not conventional.  For example, it can probably be achieved
already by writing a custom locale that treats all bytes as the same
collation class, so that strcoll then compares according to line length.
Or you could use a series of operations, such as sed to number the lines
and convert all remaining characters to a single character, sort the
converted file by the reduced key, and then use the resulting order of the
line numbers as the directions for which order to retrieve lines from the
original file.

-- 
Don't work too hard, make some time for fun as well!

Eric Blake [EMAIL PROTECTED]



signature.asc
Description: OpenPGP digital signature
___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: sort by line length

2007-04-24 Thread Jim Meyering
Georg Müller [EMAIL PROTECTED] wrote:
 I would like to see a sort by line length in the sort tool.

 Is anything like this planned.
 It seems easy to implement.
 I could also write the code for that, if you wish.

You can do this in perl (or awk, ruby, etc) as a one-liner,
so it may not be worth adding to a C application:

echo 1 938 four aa a | fmt -1 \
  | perl -ne 'push @line, $_;sub END{print sort {length $a=length $b} @line}'
1
aa
938
four
a

Is that good enough for you?


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: sort by line length

2007-04-24 Thread Pádraig Brady
Georg Müller wrote:
 Dear coreutils team,
 
 I would like to see a sort by line length in the sort tool.
 
 Is anything like this planned.
 It seems easy to implement.
 I could also write the code for that, if you wish.

That seems like a very specific need which
is probably best fulfilled with a combination of tools?
Have you an example of why you need this?

If I was doing it, I would use my funcpy tool¹ as follows,
to transform the data into the appropriate format:

funcpy '%d\t%s % (len(x),x)' | sort -k1,1n | cut -f2-

thanks,
Pádraig.

¹http://www.pixelbeat.org/scripts/funcpy


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: sort by line length

2007-04-24 Thread Brian Dessent
Jim Meyering wrote:

 You can do this in perl (or awk, ruby, etc) as a one-liner,
 so it may not be worth adding to a C application:
 
 echo 1 938 four aa a | fmt -1 \
   | perl -ne 'push @line, $_;sub END{print sort {length $a=length $b} 
 @line}'
 1
 aa
 938
 four
 a

Not to derail on perl golfing but how about just:

$ perl -e 'print sort {length($a)=length($b)} ' file

Brian


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: sort by line length

2007-04-24 Thread Andreas Schwab
Georg Müller [EMAIL PROTECTED] writes:

 I would like to see a sort by line length in the sort tool.

awk '{ print length(), $0 }' input | sort -k1,1n | cut -d' ' -f2-  output

Andreas.

-- 
Andreas Schwab, SuSE Labs, [EMAIL PROTECTED]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
And now for something completely different.


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: sort by line length

2007-04-24 Thread Georg Müller
I know that I can solve this by these combinations, but having a -l
flag for sort would be much shorter to write and I am not so familiar
with perl.
It was just an idea to make things easier, if you don't like it, ignore it.

I need it atm for bug fixing other stuff, but searched for that a time
ago...

Thanks for the quick answers,
Georg



___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils