Scorpys Posted January 9, 2022 Share Posted January 9, 2022 So we start with this text in a txt file: Objects (id: bounding-box centroid area mean-color): 0: 280x80+0+0 140.8,39.4 20069 gray(0) 4: 25x27+136+26 148.4,38.9 342 gray(255) 5: 25x26+34+27 48.9,39.9 307 gray(255) 6: 26x25+65+27 77.5,39.3 292 gray(255) 11: 33x18+211+34 227.7,41.3 289 gray(255) 9: 23x26+111+34 122.1,44.3 254 gray(255) 7: 16x19+70+30 77.8,39.0 219 gray(0) 2: 25x27+163+25 172.0,36.5 199 gray(255) 10: 20x18+187+34 197.2,41.6 179 gray(255) 3: 15x26+95+26 101.2,37.1 153 gray(255) 13: 11x11+116+37 121.1,42.2 97 gray(0) Then I run this script which I pierced together from 3 different threads on the forum. I know the code is ugly, but I don't know how to make it pretty. I'm happy and lucky to make something that works 😛 #include<file.au3> $Filename = "results.txt" ;remove line 1 _FileWriteToLine($Filename, 1,"", 1) Sleep (500) $txt = FileRead("results.txt") $out = "gray(0)" $new = StringRegExpReplace($txt, '(?m)^.*\Q' & $out & '\E.*$', "") FileWrite("new.txt", $new) Sleep (500) $sText = FileRead("new.txt") $sText = StringRegExpReplace(StringRegExpReplace($sText, "(\v)+", @CRLF), "\A\v|\v\Z", "") $hFile = FileOpen("NewText.txt", 2) FileWrite($hFile, $sText) FileClose($hFile) This script does 3 things. It removes the 1st line in the text file. It removes each line that has the word "gray(0)" in it. And it removes all empty lines. And I'm left with this: 4: 25x27+136+26 148.4,38.9 342 gray(255) 5: 25x26+34+27 48.9,39.9 307 gray(255) 6: 26x25+65+27 77.5,39.3 292 gray(255) 11: 33x18+211+34 227.7,41.3 289 gray(255) 9: 23x26+111+34 122.1,44.3 254 gray(255) 2: 25x27+163+25 172.0,36.5 199 gray(255) 10: 20x18+187+34 197.2,41.6 179 gray(255) 3: 15x26+95+26 101.2,37.1 153 gray(255) So far so good (except for my code being ugly). What I need now is to change the order of these lines based on the first number in the middle of each line. Basically the number after the 25x27+136+26 on each line. So the above would become below: 11: 33x18+211+34 227.7,41.3 289 gray(255) 10: 20x18+187+34 197.2,41.6 179 gray(255) 2: 25x27+163+25 172.0,36.5 199 gray(255) 4: 25x27+136+26 148.4,38.9 342 gray(255) 9: 23x26+111+34 122.1,44.3 254 gray(255) 3: 15x26+95+26 101.2,37.1 153 gray(255) 6: 26x25+65+27 77.5,39.3 292 gray(255) 5: 25x26+34+27 48.9,39.9 307 gray(255) As you can see, the lines are now aligned based on those middle numbers there - 227/197/172/148/122/101/77/48 Not sure if AutoIT can do this or how simple it is. I guess the code would have to first locate the correct number on each line and get them all, and then compare them and align the lines correctly. Sounds a little far fetched, I don't know. What say you guys? Link to comment Share on other sites More sharing options...
Deye Posted January 10, 2022 Share Posted January 10, 2022 (edited) Try and let us know how did it work for you #include <File.au3> Local $a[0][5], $stxt = "NewText.txt" _ArrayAdd($a, StringRegExpReplace(StringRegExpReplace(FileRead($stxt), "(?m)^\s+", ""), ' ', "|")) For $i = 0 To UBound($a) - 1 _ArraySwap($a, $i, _ArrayMaxIndex($a, 1, $i, -1, 2)) Next ConsoleWrite(_ArrayToString($a, " ") & @LF) Edited January 11, 2022 by Deye Foot Print cleanup Link to comment Share on other sites More sharing options...
Nine Posted January 10, 2022 Share Posted January 10, 2022 (edited) Another way : #include <File.au3> example() Func example() Local $aLine = FileReadToArray("Test.txt") ; read the file and put it in an array Local $aFinal[0][3], $aTemp, $iCount For $i = 1 to UBound($aLine) - 1 ; skip first line If StringInStr($aLine[$i], "gray(0)") Then ContinueLoop ; skip gray(0) $aTemp = StringRegExp($aLine[$i], "(\H+\h\H+\h)(\d+)(.*)", 1) ; extract the 3 pieces of the line _ArrayAdd($aFinal, _ArrayToString($aTemp)) ; add the 3 pieces to a final array $aFinal[$iCount][1] = Number($aFinal[$iCount][1]) ; convert 2nd piece from string to number $iCount += 1 Next _ArraySort($aFinal, 1, 0, 0, 1) ; sort the final array based on that number _FileWriteFromArray("Sorted.txt", $aFinal, Default, Default, "") ; write the final array to a txt file _ArrayDisplay($aFinal) EndFunc ps. maybe some code can be inelegant, but "ugly" not sure. Overall, code is either working or is not working . Edited January 10, 2022 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
mikell Posted January 10, 2022 Share Posted January 10, 2022 (edited) For the fun #Include <Array.au3> $txt = "Objects (id: bounding-box centroid area mean-color):" & @crlf & _ " 0: 280x80+0+0 140.8,39.4 20069 gray(0)" & @crlf & _ " 4: 25x27+136+26 148.4,38.9 342 gray(255)" & @crlf & _ " 5: 25x26+34+27 48.9,39.9 307 gray(255)" & @crlf & _ " " & @crlf & _ " 6: 26x25+65+27 77.5,39.3 292 gray(255)" & @crlf & _ " 11: 33x18+211+34 227.7,41.3 289 gray(255)" & @crlf & _ " 9: 23x26+111+34 122.1,44.3 254 gray(255)" & @crlf & _ "" & @crlf & _ " 7: 16x19+70+30 77.8,39.0 219 gray(0)" & @crlf & _ " 2: 25x27+163+25 172.0,36.5 199 gray(255)" & @crlf & _ " 10: 20x18+187+34 197.2,41.6 179 gray(255)" & @crlf & _ " 3: 15x26+95+26 101.2,37.1 153 gray(255)" & @crlf & _ " 13: 11x11+116+37 121.1,42.2 97 gray(0)" $txt = StringRegExpReplace($txt, '(^.+\R)|(?m)^(\s*\R)|(.*gray\(0\)\R?)', "") $stmp = Execute("'" & StringRegExpReplace($txt, "(?m)(^.*?)(?<=\h)(\d+)(?=\.)", "' & StringFormat('%04i', '$2') & ' $1$2' & '") & "'") $atmp = StringRegExp($stmp, '\N+', 3) _ArraySort($atmp, 1) $res = StringRegExpReplace(_ArrayToString($atmp, @crlf), '(?m)^\d{4}\h*', "") Msgbox(0,"", $res) Edited January 10, 2022 by mikell completed jguinch, junkew and Deye 3 Link to comment Share on other sites More sharing options...
jguinch Posted January 10, 2022 Share Posted January 10, 2022 (edited) Another one : #Include <Array.au3> $file = "results.txt" $content = FileRead($file) ; removes first line, trailing spaces, lines containing 'gray(0)' and empty lines $newContent = StringRegExpReplace($content, "(?x)(?m) \A\N+\R | ^\s+(.+gray\(0\)\N*(?:\R|$))? | ^\s*\R", "") $aNumbers = StringRegExp($newContent, "([\d.]+),", 3) For $i = 0 To UBound($aNumbers) - 1 $aNumbers[$i] = Number($aNumbers[$i]) Next _ArraySort($aNumbers) Local $sOut For $i = 0 To UBound($aNumbers) - 1 $sOut &= ($i = 0 ? "" : @CRLF) & StringRegExp($newContent, "(?m)^(.+\Q" & $aNumbers[$i] & "\E(?:\.0)?,\N+)", 1)[0] Next ConsoleWrite($sOut) Edited January 10, 2022 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Deye Posted January 10, 2022 Share Posted January 10, 2022 (edited) One more inspired by @jguinch #include <File.au3> Local $a[0][5], $stxt = "NewText.txt" _ArrayAdd($a, StringRegExpReplace(StringRegExpReplace(FileRead($stxt), "(?m)^\s+", ""), ' ', "|")) For $i = 0 To UBound($a) - 1 $a[$i][2] = Number($a[$i][2]) Next _ArraySort($a, 1, Default, Default, 2) ConsoleWrite(_ArrayToString($a, " ") & @LF) Edited January 11, 2022 by Deye Link to comment Share on other sites More sharing options...
junkew Posted January 10, 2022 Share Posted January 10, 2022 Scratching my head for a while on: $stmp = Execute("'" & StringRegExpReplace($txt, "(?m)(^.*?)(?<=\h)(\d+)(?=\.)", "' & StringFormat('%04i', '$2') & ' $1$2' & '") & "'") But after a while I understood what was happening 😉 Depending on expressiveness, flexibility and further needs I would probably do this in powershell #$data=get-content result.txt -Encoding utf8 $data = @" Objects (id: bounding-box centroid area mean-color): 0: 280x80+0+0 140.8,39.4 20069 gray(0) 4: 25x27+136+26 148.4,38.9 342 gray(255) 5: 25x26+34+27 48.9,39.9 307 gray(255) 6: 26x25+65+27 77.5,39.3 292 gray(255) 11: 33x18+211+34 227.7,41.3 289 gray(255) 9: 23x26+111+34 122.1,44.3 254 gray(255) 7: 16x19+70+30 77.8,39.0 219 gray(0) 2: 25x27+163+25 172.0,36.5 199 gray(255) 10: 20x18+187+34 197.2,41.6 179 gray(255) 3: 15x26+95+26 101.2,37.1 153 gray(255) 13: 11x11+116+37 121.1,42.2 97 gray(0) "@ $MyArrayList = [System.Collections.ArrayList]@() #Lets first split it $datalines=$data -split "`r`n" #Create the arraylist foreach($dataline in $datalines) { $tline =$dataline.trimstart() $dataColumns=$tline.split(" ,") $MyArrayList.add([pscustomobject]@{ "dataline" = $dataLine "id" = $datacolumns[0] "boundingbox" = $datacolumns[1] "centroid a" = [double]$datacolumns[2] "centroid b" = [double]$datacolumns[3] "area" = $datacolumns[4] "meancolor" = $datacolumns[5] }) } #Format, sort, filter, reshuffle $myArraylist | format-table -autosize $myArraylist | where meancolor -notin @('gray(0)','mean-color)') | sort "centroid a" -Descending | select dataline Output dataline id boundingbox centroid a centroid b area meancolor -------- -- ----------- ---------- ---------- ---- --------- 0: 280x80+0+0 140.8,39.4 20069 gray(0) 0: 280x80+0+0 140,8 39,4 20069 gray(0) 4: 25x27+136+26 148.4,38.9 342 gray(255) 4: 25x27+136+26 148,4 38,9 342 gray(255) 5: 25x26+34+27 48.9,39.9 307 gray(255) 5: 25x26+34+27 48,9 39,9 307 gray(255) 6: 26x25+65+27 77.5,39.3 292 gray(255) 6: 26x25+65+27 77,5 39,3 292 gray(255) 11: 33x18+211+34 227.7,41.3 289 gray(255) 11: 33x18+211+34 227,7 41,3 289 gray(255) 9: 23x26+111+34 122.1,44.3 254 gray(255) 9: 23x26+111+34 122,1 44,3 254 gray(255) 7: 16x19+70+30 77.8,39.0 219 gray(0) 7: 16x19+70+30 77,8 39 219 gray(0) 2: 25x27+163+25 172.0,36.5 199 gray(255) 2: 25x27+163+25 172 36,5 199 gray(255) 10: 20x18+187+34 197.2,41.6 179 gray(255) 10: 20x18+187+34 197,2 41,6 179 gray(255) 3: 15x26+95+26 101.2,37.1 153 gray(255) 3: 15x26+95+26 101,2 37,1 153 gray(255) 13: 11x11+116+37 121.1,42.2 97 gray(0) 13: 11x11+116+37 121,1 42,2 97 gray(0) dataline -------- 11: 33x18+211+34 227.7,41.3 289 gray(255) 10: 20x18+187+34 197.2,41.6 179 gray(255) 2: 25x27+163+25 172.0,36.5 199 gray(255) 4: 25x27+136+26 148.4,38.9 342 gray(255) 9: 23x26+111+34 122.1,44.3 254 gray(255) 3: 15x26+95+26 101.2,37.1 153 gray(255) 6: 26x25+65+27 77.5,39.3 292 gray(255) 5: 25x26+34+27 48.9,39.9 307 gray(255) jguinch 1 FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
Scorpys Posted February 2, 2022 Author Share Posted February 2, 2022 Thanks for the replies guys. Appreciate it Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now