topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 6:40 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: Sort lines of a txt file by lenght of the string  (Read 20209 times)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Sort lines of a txt file by lenght of the string
« on: February 18, 2017, 08:02 AM »
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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #1 on: February 18, 2017, 08:08 AM »
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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #2 on: February 18, 2017, 08:43 AM »
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

  • Supporting Member
  • Joined in 2008
  • **
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #3 on: February 18, 2017, 12:21 PM »
Give TED Notepad a try:

http://jsimlo.sk/notepad/


Best plain text editor ever ;-)

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #4 on: February 18, 2017, 02:23 PM »
Give TED Notepad a try:

http://jsimlo.sk/notepad/
Great tip!

Best plain text editor ever ;-)
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

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #5 on: February 18, 2017, 03:51 PM »
Give TED Notepad a try:

http://jsimlo.sk/notepad/
Great tip!

Best plain text editor ever ;-)
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

We are entushiastic of software !!!!!!
another notepad : pspad, notepad++, ted notepad .....

Running to try
First downloading the editor

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #6 on: February 19, 2017, 05:27 AM »
I have tried. But I think i am doing something wrong.
Simple don't get results .

And try to apply all the steps !!!
 :-[

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #7 on: February 19, 2017, 06:43 AM »
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.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #8 on: February 19, 2017, 06:51 AM »
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?
Copy/paste is the least error-prone, especially as the first sequence is: percent, plus, lowercase-L, zero, vertical bar, percent, zero

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #9 on: February 19, 2017, 08:51 AM »
Though it worked fine for me, using TED Notepad, I wanted to resolve this one using Notepad++ 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:
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]
  1. # Ath: Found this in the notepad-plus forum on sourceforge.net, modified as suggested in the comments there
  2. editor.beginUndoAction()
  3. if editor.getSelText(): # grab the lines
  4.     linee = editor.getSelText().splitlines()
  5. else:
  6.     linee = editor.getText().splitlines()
  7. sorted_lines=sorted(linee,key=lambda x:len(x)) # add parameter reverse=True, to sort descending by length
  8. editor.clearAll() # wipe lines in editor
  9. for line in sorted_lines:
  10.     print(line) # put all lines back in the editor
  11. 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.
- 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 :D

Edit: Added sentence about the code needing to be at column 1
« Last Edit: February 20, 2017, 02:07 PM by Ath »

anandcoral

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 777
    • View Profile
    • Free Portable Apps
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #10 on: February 19, 2017, 11:37 AM »
Hi Contro

You can try http://www.miniwebto...sort-text-by-length/

I got below result,

2017-02-19_230714.pngSort lines of a txt file by lenght of the string

Regards,

Anand

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 120
    • View Profile
    • Lintalist
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #11 on: February 19, 2017, 01:47 PM »
For reference: short AutoHotkey example (one of several you can find there on the forum) https://autohotkey.c...ine-length/?p=149560

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #12 on: February 19, 2017, 01:51 PM »
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.
I am going to try.
I was making by hand , not copy / paste.
I will put screenshots .

Trying !

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #13 on: February 19, 2017, 02:09 PM »
Though it worked fine for me, using TED Notepad, I wanted to resolve this one using Notepad++ 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 :D

I will try too.

The other method with notepad ted give me no results.

The zeros stay in the final result ?




Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #14 on: February 19, 2017, 02:14 PM »
AsusPortatil - 19_02_2017 , 19_54_59.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_55_55.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_56_30.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_56_41.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_56_46.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_56_57.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_57_02.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_57_09.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_57_20.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_57_46.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_58_10.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_58_40.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 19_59_12.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 20_00_21.pngSort lines of a txt file by lenght of the string

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #15 on: February 19, 2017, 02:15 PM »
Hi Contro

You can try http://www.miniwebto...sort-text-by-length/

I got below result,

[ Invalid Attachment ]

Regards,

Anand

This method works fine.

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #16 on: February 19, 2017, 02:16 PM »
For reference: short AutoHotkey example (one of several you can find there on the forum) https://autohotkey.c...ine-length/?p=149560

Trying

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #17 on: February 19, 2017, 02:18 PM »
For reference: short AutoHotkey example (one of several you can find there on the forum) https://autohotkey.c...ine-length/?p=149560
Where have I to put the name of my txt file in the script ?


Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #18 on: February 19, 2017, 02:36 PM »
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...

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #19 on: February 19, 2017, 02:43 PM »
I am doing bad. I obtain an error with the python script.

The screenshots :

AsusPortatil - 19_02_2017 , 20_31_44.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 20_38_48.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 20_39_45.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 20_40_18.pngSort lines of a txt file by lenght of the stringAsusPortatil - 19_02_2017 , 20_42_25.pngSort lines of a txt file by lenght of the string

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #20 on: February 19, 2017, 02:47 PM »
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.

But it's strange for me. I think i am doing well.
Any option left ?


Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 120
    • View Profile
    • Lintalist
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #21 on: February 19, 2017, 02:58 PM »
Where have I to put the name of my txt file in the script ?
The first line says
Code: Autohotkey [Select]
  1. FileRead, text, %A_ScriptFullPath%
replace it with
Code: Autohotkey [Select]
  1. FileRead, text, pathtoyour\file.txt
Assuming you want to save it replace MsgBox %text% with
Code: Autohotkey [Select]
  1. FileDelete, pathtoyour\sortedfile.txt
  2. FileAppend, %text%, pathtoyour\sortedfile.txt

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #22 on: February 19, 2017, 03:36 PM »
Where have I to put the name of my txt file in the script ?
The first line says
Code: Autohotkey [Select]
  1. FileRead, text, %A_ScriptFullPath%
replace it with
Code: Autohotkey [Select]
  1. FileRead, text, pathtoyour\file.txt
Assuming you want to save it replace MsgBox %text% with
Code: Autohotkey [Select]
  1. FileDelete, pathtoyour\sortedfile.txt
  2. FileAppend, %text%, pathtoyour\sortedfile.txt
Trying. Best Regards

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #23 on: February 19, 2017, 03:42 PM »
Where have I to put the name of my txt file in the script ?
The first line says
Code: Autohotkey [Select]
  1. FileRead, text, %A_ScriptFullPath%
replace it with
Code: Autohotkey [Select]
  1. FileRead, text, pathtoyour\file.txt
Assuming you want to save it replace MsgBox %text% with
Code: Autohotkey [Select]
  1. FileDelete, pathtoyour\sortedfile.txt
  2. FileAppend, %text%, pathtoyour\sortedfile.txt
Trying. Best Regards

I think I got. Is fast and easy. Best Regards

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #24 on: February 19, 2017, 03:43 PM »
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.
It's new for me.

Can I try again ?
 :-*