Jump to content

Sorting a text file by line length


leuce
 Share

Recommended Posts

My question follows on the conversatoin in this topic.

I have a text file which I want to sort... not alphabetically, but by line length. I don't know if AutoIt can do this directly, so I devised a method which combines AutoIt with the text editor Metapad (Notepad also works) and finally MS Excel.

Basically, I copy each line, gets its length, and then pastes the length of the line at the beginning of the line, plus a tab. Then I save the file manually, open it in Excel, sort by column A, and then delete column A.

There is a glitch, however, and I can't figure out what causes it. Sometimes, the script manages to get the line length, but other times it skips a step and then it misgets the line length. Ideally, a text like this:

THIS is a HOUSE.

this is a CAR.

THIS is A Book.

should end up like this:

16 [tab] THIS is a HOUSE.

14 [tab] this is a CAR.

15 [tab] THIS is A Book.

but sometimes it ends up like this:

16 [tab] THIS is a HOUSE.

this is a CAR. [tab] 1

15 [tab] THIS is A Book.

Can anyone tell me what I'm doing wrong here? Here is the script:

==

; The file testing.txt contains a number of lines of text.

; This script was designed for Metapad. Metapad must be set

; not to display the full path in the file name. Wordwrap

; must be off. Use DOS-text (for @CRLF).

; To run this script in Notepad, remove Send("+{LEFT}").

; GLITCHES

; For some reason, on some lines, the script does not return

; to the beginning of the line, which results in that line

; being counted as 1 character long, and the number being sent

; to the end of the line instead of the beginning.

; Sets an escape key for emergency purposes

HotKeySet("^q", "MyExit")

; Gets the number of lines in the text file

$file = FileOpen("testing.txt", 0)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

$FileText = FileRead("testing.txt", FileGetSize("testing.txt"))

$FileTextARRAY = StringSplit($FileText, @CRLF,1)

; Wait for user to open file, then go to top of file

WinWaitActive("testing.txt")

Send("^{HOME}")

; This loop continues for the number of lines in the text file

For $i = 1 To $FileTextARRAY[0]

; Copy the entire line at the cursor position

Send("{HOME}")

Send("+{END}")

Send("+{LEFT}")

Send("^c")

; Gets length of the line

$line = ClipGet()

$length = StringLen($line)

; Pastes line length and a tab at beginning of the line

ClipPut($length)

Send("^v")

Send("{TAB}")

ClipPut($line)

Send("^v")

; Go to the next line

Send("{DOWN}")

Next

MsgBox(4096, "Done", "The script is done, mate", 10)

Func MyExit()

Exit

EndFunc

==

Or... is there another way to sort a file in AutoIt by line length which either doesn't require using the text editor or using Excel?

Thanks in advance

Samuel Murray

Link to comment
Share on other sites

well i just had to try

#include <file.au3>
#include <array.au3>

Dim $Old_File = "C:\Test2.txt"; "YOUR OLD FILE NAME"
Dim $New_File = "C:\Test2New.txt"; "YOUR NEW FILE NAME"
Dim $aRecords

If Not _FileReadToArray($Old_File,$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    $aRecords[$x] = (StringLen($aRecords[$x])) & "," & $aRecords[$x]
    Msgbox(0,'Record:' & $x, $aRecords[$x]); for testing
Next
$count = $aRecords[0]
_ArraySort($aRecords,0,1)

_ArrayDisplay($aRecords, "sorted"); for testing

$NFile = FileOpen($New_File,2)
For $t = 1 to $count
    $pos = StringInStr($aRecords[$t], ",")
    FileWriteLine($NFile, (StringTrimLeft($aRecords[$t], $pos)))
    Msgbox(0,'Record:' & $t, $aRecords[$t]); for testing
    
Next
FileClose($NFile)

tested ok for me

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...