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 ?
How can I do this ?
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>length-sorted.txt


Give TED Notepad a try:Great tip!
http://jsimlo.sk/notepad/
Best plain text editor ever ;-)We'll see about that...

%+l0|%0- Press Cut (Alt-C), length and a separator are inserted in each line%1- Change settings on %1 tab


Give TED Notepad a try:Great tip!
http://jsimlo.sk/notepad/Best plain text editor ever ;-)We'll see about that...
Now let's try this TED Notepad...
- 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)Code- Press Cut (Alt-C), length and a separator are inserted in each line%+l0|%0
- 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 toCode- Change settings on %1 tab%1
- 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
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

Simple don't get results .Huh? You do select all text (Ctrl-A) before running any of the tools? It makes working with the preview of "Tools/Lines/Columns, Numbers" possible, so you should see a preview while entering the Output mask.
I have tried. But I think i am doing something wrong.Uhm, did you retype the Output mask values, or copy/paste from this page?
Python 2.7.6-notepad++ r2 (default, Apr 21 2014, 19:26:54) [MSC v.1600 32 bit (Intel)]
Initialisation took 125ms
Ready.- Choose Plugins/Python Script/New Script, type a filename: sort-by-length.py, and press Save. The file is created and opened in Notepad++# Ath: Found this in the notepad-plus forum on sourceforge.net, modified as suggested in the comments there
editor.beginUndoAction()
if editor.getSelText(): # grab the lines
linee = editor.getSelText().splitlines()
else:
linee = editor.getText().splitlines()
sorted_lines=sorted(linee,key=lambda x:len(x)) # add parameter reverse=True, to sort descending by length
editor.clearAll() # wipe lines in editor
for line in sorted_lines:
print(line) # put all lines back in the editor
editor.endUndoAction()- Make sure the code is not indented (so each line starting on column 1) except for lines 4, 6 and 10 as they must be indented.
I am going to try.Simple don't get results .Huh? You do select all text (Ctrl-A) before running any of the tools? It makes working with the preview of "Tools/Lines/Columns, Numbers" possible, so you should see a preview while entering the Output mask.
You can attach a sample file here, or PM me for my e-mail, and I'll verify if the file is causing the trouble. Current TED Notepad seems to have issues with UTF-8 files without a BOM, and you might have such a file, but you should be able to use File/Re-open as UTF-8 and it could resolve your issue.
Though it worked fine for me, using TED Notepad, I wanted to resolve this one using Notepad++ too.I will try too.
It won't be possible to use the, quite limited, macro feature of Npp, so I used a scripting plugin: Notepad++ Python Script
[One time only]
- Install the Notepad++ Python Script plugin, using the MSI installer, not using the plugin manager. It can be downloaded from here. Close Notepad++ before the installer is run.
- Open Notepad++
- Check if Python Script is working correctly by opening the console: Plugins/Python Script/Show Console, en verifying that it shows the Python version, something like this:
[Select]
Python 2.7.6-notepad++ r2 (default, Apr 21 2014, 19:26:54) [MSC v.1600 32 bit (Intel)]
Initialisation took 125ms
Ready.
- Choose Plugins/Python Script/New Script, type a filename: sort-by-length.py, and press Save. The file is created and opened in Notepad++
- Paste this script into sort-by-length.py:
Code: Python [Select]
# Ath: Found this in the notepad-plus forum on sourceforge.net, modified as suggested in the comments there
editor.beginUndoAction()
if editor.getSelText(): # grab the lines
linee = editor.getSelText().splitlines()
else:
linee = editor.getText().splitlines()
sorted_lines=sorted(linee,key=lambda x:len(x)) # add parameter reverse=True, to sort descending by length
editor.clearAll() # wipe lines in editor
for line in sorted_lines:
print(line) # put all lines back in the editor
editor.endUndoAction()
- Save the file
- The script should now be available in the menu Plugins/Python Script/Scripts/ (don't select it, it will execute immediately!)
[Process a file]
- Open the file to sort, and make sure it is the selected file
- Select Plugins/Python Script/Scripts/sort-by-length
- The file will be emptied and filled with all lines again, shortest first (or longest first if reversed=True was set). It may take some time, as Python (Script) doesn't seem very fast in this configuration
- If an error occurs during processing, it will be shown in the Python Script console (see above how to open that)
- Save the file
Done
Hi ControThis method works fine.
You can try http://www.miniwebtool.com/sort-text-by-length/
I got below result,
[attachment 1 not found]
Regards,
Anand
For reference: short AutoHotkey example (one of several you can find there on the forum) https://autohotkey.com/board/topic/23093-sort-a-text-file-according-to-line-length/?p=149560
For reference: short AutoHotkey example (one of several you can find there on the forum) https://autohotkey.com/board/topic/23093-sort-a-text-file-according-to-line-length/?p=149560Where have I to put the name of my txt file in the script ?
If you apply the description of my TED Notepad procedure, copy/pasting the 'Output mask' values from here, you should be fine, I think I see at least 3 typos in your first screenshot, and then it only gets worse from there...Don't worry I keep trying.
Where have I to put the name of my txt file in the script ?The first line says
FileRead, text, %A_ScriptFullPath%replace it withFileRead, text, pathtoyour\file.txtAssuming you want to save it replace MsgBox %text% withFileDelete, pathtoyour\sortedfile.txt
FileAppend, %text%, pathtoyour\sortedfile.txtTrying. Best RegardsWhere have I to put the name of my txt file in the script ?The first line saysCode: ( autohotkey)replace it withFileRead, text, %A_ScriptFullPath%Code: ( autohotkey)Assuming you want to save it replace MsgBox %text% withFileRead, text, pathtoyour\file.txtCode: ( autohotkey)FileDelete, pathtoyour\sortedfile.txt FileAppend, %text%, pathtoyour\sortedfile.txt
I think I got. Is fast and easy. Best RegardsTrying. Best RegardsWhere have I to put the name of my txt file in the script ?The first line saysCode: ( autohotkey)replace it withFileRead, text, %A_ScriptFullPath%Code: ( autohotkey)Assuming you want to save it replace MsgBox %text% withFileRead, text, pathtoyour\file.txtCode: ( autohotkey)FileDelete, pathtoyour\sortedfile.txt FileAppend, %text%, pathtoyour\sortedfile.txt
If you apply the description of my TED Notepad procedure, copy/pasting the 'Output mask' values from here, you should be fine, I think I see at least 3 typos in your first screenshot, and then it only gets worse from there...I think very interesting to load a python script.

Original address: https://www.donationcoder.com/forum/index.php?topic=43525.0