Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

sort a text file according to line length


  • Please log in to reply
4 replies to this topic
edu
  • Members
  • 99 posts
  • Last active: Dec 21 2009 07:14 PM
  • Joined: 12 Oct 2006
Hi, I'm having this idea to accomplish a task by sorting the lines of a file according to their length.
The file or files I will be using are probably 500Kb or more. And I wonder if autohotkey would be able to handle this or if someone knows one of those little command line programs that could do this, could you please point me too?
Thanks!
The best things of life are free.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
that should not be a big problem. worst case you make a list of lines and lengths, then sort that, and read the final file in that order into another file.

edu as guest
  • Guests
  • Last active:
  • Joined: --
Thanks enguneer. Sounds like a good idea. I'll try to parse the file adding the line length at the beginning of each line, then I will sort it.

Thanks for the pointer, again.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
Use the F option of sort. :)
FileRead, text, %A_ScriptFullPath%
Sort, text, F SortFunc
MsgBox %text%

SortFunc(lineA, lineB, offset)
{
    ; Return positive if lineA is longer than line B.
    ; Return negative if lineA is shorter than line B.
    if StrLen(lineA) != StrLen(lineB)
        return StrLen(lineA)-StrLen(lineB)
    ; Use offset to try to preserve the order in the file when two lines are of equal length.
    return -offset
}
I had to laugh when I saw the result. :lol:

edu
  • Members
  • 99 posts
  • Last active: Dec 21 2009 07:14 PM
  • Joined: 12 Oct 2006
Wow. Drooling!
Man, you guys are amazing. Thank you very much!

I had to laugh when I saw the result. Laughing


I wonder why. Anyway, when I just tested your script I had an impression that something was wrong, but no... it was doing what was supposed to do :wink: Great. I could help smiling.

Cheers,

Eduardo
The best things of life are free.