ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Post New Requests Here

Sort lines of a txt file by lenght of the string

(1/7) > >>

Contro:
If each line is a string of text I would like to order the txt file from the short to the longest one.

How can I do this ?

Ath:
insert the length, fixed format (0001 etc.), of each line in front of each line, with a separator
sort the file
remove the added columns, cut at the separator

Ath:
Again a 'one-liner' this time using awk and sort:

--- ---awk '{ printf"%04d|%s\n",length($0),$0}' contro.txt|sort|awk -F'|' '{print $2}'- first awk command adds the length and the separator, a vertical pipe
- sort command puts them in the desired order
- second awk command removes the length and the separator
to redirect to a file, add this at the end:

--- --->length-sorted.txt
I've used Cygwin, then you might want/need to issue 'export LC_ALL=C' before running the command, else awk might give some error about binary data (the accented characters)
Couldn't find a way to do this in NotePad++ yet, maybe using the Python Script plugin for NotePad++, I'll investigate that later

I used your previous sample input text 'contro.txt' as input :P

MrZebulon:
Give TED Notepad a try:

http://jsimlo.sk/notepad/


Best plain text editor ever ;-)

Ath:
Give TED Notepad a try:

http://jsimlo.sk/notepad/
-MrZebulon (February 18, 2017, 12:21 PM)
--- End quote ---
Great tip!

Best plain text editor ever ;-)
-MrZebulon (February 18, 2017, 12:21 PM)
--- End quote ---
We'll see about that...

Now let's try this TED Notepad... :tellme:

- Open the file
- Select All (Ctrl-A)
- Select Tools/Lines/Columns, Numbers... (Alt-T,L,N or Alt-Ctrl-Y)
- Change Output mask to (Line length right aligned with zeros, separator |, original line)

--- ---%+l0|%0- Press Cut (Alt-C), length and a separator are inserted in each line
- Select All (Ctrl-A)
- Sort Ascending (Tools/Sort/Ascending, Alt-T,S,A or Alt-Ctrl-A)
- Select All (Ctrl-A)
- Select Tools/Lines/Columns, Numbers... (Alt-T,L,N or Alt-Ctrl-Y)
- Change Output mask to

--- ---%1- Change settings on %1 tab
  - Select 'Entire line'
  - Check: 'Use only characters from
  - set: 'position' to 5
  - set: 'to' to 999 (assuming no line is longer than 999 characters, or use 9999)
(Have Preview checked, then the preview should show the result without length and separator, if a column is missing change 5 to 4 or 3)
- Press Cut (Alt-C)
- Save result (Ctrl-S or Ctrl-B, Save as...)
- Job well done :D

NB: The use of a separator is entirely optional, ofcourse ;)

I kinda like this TED Notepad, looks like something safe enough for @Contro to handle ;D

Navigation

[0] Message Index

[#] Next page

Go to full version