Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to sort...

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL

I have a text file:

fsaffafasfasfd
afafafafa
asfafafasfafasfafa
af
fasf
affafasasfa


and as a sort result I expect lines sored from the shortest to the longes, so:

af
fasf
afafafafa
affafasasfa
fsaffafasfasfd
asfafafasfafasfafa



thx in advance!
 
Hi

Code:
sed 'h;s/././g;G;s/\n/\t/' /input/file | sort | sed 's/.*\t//'

[gray]# or[/gray]

awk '{$0=$0"\t"length($0)}1' /input/file | sort -t$'\t' -k2 -n | awk '{NF--}1'

[gray]# or[/gray]

perl -ne '$l[$.]=$_;END{print sort{length $a<=>length $b}@l}' /input/file

Feherke.
 
thank you.

in the meantime I found also such script:


#! /bin/sh
#
### lensort - sort by line length
### Usage: lensort [files]

# print each line's length, a TAB and then the actual line.
# By default, awk can run out of fields on long lines;
# setting FS=RS, to make each line one long field, can help.
awk 'BEGIN { FS=RS }
{ print length, $0 }' $* |

# Sort the lines numerically
sort +0n -1 |

# Remove the length and the space and print each line
sed 's/^[0-9][0-9]* //'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top