Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting lines from longest to shortest [duplicate]

Tags:

How can I rearrange all of the lines in a file from longest to shortest? For e.g.:

elephant zoo penguin 

Would be changed to

elephant penguin zoo 
like image 579
Village Avatar asked Nov 28 '11 13:11

Village


People also ask

How to sort lines by length NotePad++?

Notepad++ has a built-in TextFX tool that sorts selected lines alphabetically. This tool can be hijacked to sort by the length of the lines by placing spaces on the left of each line, and making sure that all the lines are the same length.

How do you sort the length of a string?

Custom Sorting With key= For example with a list of strings, specifying key=len (the built in len() function) sorts the strings by length, from shortest to longest. The sort calls len() for each string to get the list of proxy length values, and then sorts with those proxy values.

How to sort numbers in NotePad++?

Please follow this step,To sorting data. Step 1: First to select Edit menu than select Line operation. Step 2: After select Sort Lines Lexicographically ascending. I hope this information will be useful.

How to sort in NotePad++ at a column?

After restarting Notepad++, go to (menu) Plugins -> Column sorting -> Set criterias , define point-of-start and width of your columns and hit the Sort button.


1 Answers

Add the line length as the first field of the line, sort, and remove the line length:

awk '{ print length($0) " " $0; }' $file | sort -r -n | cut -d ' ' -f 2- 
like image 105
thiton Avatar answered Sep 20 '22 17:09

thiton