Deye Posted January 20, 2022 Share Posted January 20, 2022 (edited) Every new paragraph ( from the second col - everything reading after ": " ) must be converted into a new row. $sVar = @LF & @CRLF _ & 'MainWindowHandle : 132730' _ & @LF & 'Id : 8808' _ & @LF & 'ProcessName : SciTE' _ & @LF & 'CPU : 112.15625' _ & @LF & 'Handles : 512' & @LF & @CRLF $sVar &= 'MainWindowHandle : 656932' _ & @LF & 'Id : 4268' _ & @LF & 'ProcessName : iexplore' _ & @LF & 'CPU : 0.15625' _ & @LF & 'Handles : 400' & @LF & @CRLF $sVar &= 'MainWindowHandle : 1182078' _ & @LF & 'Id : 6748' _ & @LF & 'ProcessName : explorer' _ & @LF & 'CPU : 12.09375' _ & @LF & 'Handles : 1230' & @LF & @CRLF $sVar &= 'MainWindowHandle : 1837220' _ & @LF & 'Id : 6868' _ & @LF & 'ProcessName : explorer' _ & @LF & 'CPU : 6.171875' _ & @LF & 'Handles : 552' & @LF & @CRLF the total outcome after processing, the data should look like so: $s = '' _ & 'MainWindowHandle|Id|ProcessName|CPU|Handles' _ & @LF & '132730|8808|SciTE|112.15625|512' _ & @LF & '656932|4268|iexplore|0.15625|400' _ & @LF & '1182078|6748|explorer|12.09375|1230' _ & @LF & '1837220|6868|explorer|6.171875|552' ConsoleWrite($s & @LF) at looking for fewer lines of code as possible to do this right $s = StringRegExpReplace($sVar, "(\w+)(.{2,})(\R)", "$1|") $s = StringRegExpReplace($s, "(.*\R)+(.*$)", "$1") ;~ ConsoleWrite($s & @LF) $s &= StringRegExpReplace($sVar, "(.{2,}\: )(.{2,})(\R)", "$2|") $s = StringRegExpReplace($s, "\R\R", "") $s = StringRegExpReplace($s, "\|\R", @CRLF) ConsoleWrite($s & @LF) TIA Edited January 21, 2022 by Deye Link to comment Share on other sites More sharing options...
Nine Posted January 20, 2022 Share Posted January 20, 2022 So what is your best effort on this one ? Or you are just asking us to bark some code for you...You should know better. “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...
Danp2 Posted January 20, 2022 Share Posted January 20, 2022 10 minutes ago, Nine said: bark some code 😆 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Gianni Posted January 21, 2022 Share Posted January 21, 2022 (edited) what does 'SRER' mean? ... here my bark belch... expandcollapse popup#include <String.au3> #include <Array.au3> $sVar = @LF & @CRLF _ & 'MainWindowHandle : 132730' _ & @LF & 'Id : 8808' _ & @LF & 'ProcessName : SciTE' _ & @LF & 'CPU : 112.15625' _ & @LF & 'Handles : 512' & @LF & @CRLF $sVar &= 'MainWindowHandle : 656932' _ & @LF & 'Id : 4268' _ & @LF & 'ProcessName : iexplore' _ & @LF & 'CPU : 0.15625' _ & @LF & 'Handles : 400' & @LF & @CRLF $sVar &= 'MainWindowHandle : 1182078' _ & @LF & 'Id : 6748' _ & @LF & 'ProcessName : explorer' _ & @LF & 'CPU : 12.09375' _ & @LF & 'Handles : 1230' & @LF & @CRLF $sVar &= 'MainWindowHandle : 1837220' _ & @LF & 'Id : 6868' _ & @LF & 'ProcessName : explorer' _ & @LF & 'CPU : 6.171875' _ & @LF & 'Handles : 552' & @LF & @CRLF ; ---------------- $sVar = StringReplace($sVar, @LF & @CR, @CR) Local $aHeaders = _ArrayUnique(_StringBetween($sVar, @LF, ':'), 0, 0, 0, 0) Local $s = _ArrayToString($aHeaders) & @LF ; _ArrayDisplay($aHeaders) For $i = 0 To UBound($aHeaders) - 1 $sVar = StringReplace($sVar, $aHeaders[$i] & ": ", "") Next $sVar = StringReplace($sVar, @LF, '|') Local $aRecords = _StringBetween($sVar, @CR, @CR) For $i = 0 To UBound($aRecords) - 1 $s &= StringTrimLeft($aRecords[$i], 1) & @LF Next $s = StringTrimRight($s, 1) ConsoleWrite($s & @LF) ; goal ; ---------------- _ArrayDisplay(_VarTo2D($s, "|")) Func _VarTo2D($var, $sSeparator = @TAB) Local $aRows = StringSplit(StringStripCR($var), @LF), $aColumns, $aResult[$aRows[0]][1] For $iRow = 1 To $aRows[0] $aColumns = StringSplit($aRows[$iRow], $sSeparator) If $aColumns[0] > UBound($aResult, 2) Then ReDim $aResult[$aRows[0]][$aColumns[0]] For $iColumn = 1 To $aColumns[0] $aResult[$iRow - 1][$iColumn - 1] = $aColumns[$iColumn] Next Next Return $aResult EndFunc ;==>_VarTo2D Edited January 21, 2022 by Chimp Deye 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
Deye Posted January 21, 2022 Author Share Posted January 21, 2022 1 minute ago, Chimp said: what does 'SRER' mean? Hey Chimp its StringRegExpReplace 2 hours ago, Nine said: So what is your best effort on this one ? right you are, added one above Link to comment Share on other sites More sharing options...
argumentum Posted January 21, 2022 Share Posted January 21, 2022 $aVar = _ArrayFromString(StringReplace(StringTrimRight(StringTrimLeft($sVar, 3), 3), ":", @LF), @LF, @CRLF) Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
mikell Posted January 21, 2022 Share Posted January 21, 2022 (edited) For the fun, a one-liner Edit : I saw 'challenge' in the title so I played the game. Obviously I don't recommend such a convoluted way #include <Array.au3> $sVar = @LF & @CRLF _ & 'MainWindowHandle : 132730' _ & @LF & 'Id : 8808' _ & @LF & 'ProcessName : SciTE' _ & @LF & 'CPU : 112.15625' _ & @LF & 'Handles : 512' & @LF & @CRLF $sVar &= 'MainWindowHandle : 656932' _ & @LF & 'Id : 4268' _ & @LF & 'ProcessName : iexplore' _ & @LF & 'CPU : 0.15625' _ & @LF & 'Handles : 400' & @LF & @CRLF $sVar &= 'MainWindowHandle : 1182078' _ & @LF & 'Id : 6748' _ & @LF & 'ProcessName : explorer' _ & @LF & 'CPU : 12.09375' _ & @LF & 'Handles : 1230' & @LF & @CRLF $sVar &= 'MainWindowHandle : 1837220' _ & @LF & 'Id : 6868' _ & @LF & 'ProcessName : explorer' _ & @LF & 'CPU : 6.171875' _ & @LF & 'Handles : 552' & @LF & @CRLF $s = StringTrimRight(StringRegExpReplace(StringSplit(StringStripWS($sVar, 3), @cr)[1], '\h*:\N+\n', "\|"), 1) &@cr& StringRegExpReplace(StringRegExpReplace(StringStripWS($sVar, 3), '(?m)^.+?:\h', "\|"), '((?<=\A|\v\v)\|)|\n', "") Msgbox(0,"", $s) Local $a[0][5] _ArrayAdd($a, $s, 0, "|", @cr) _ArrayDisplay($a) Edited January 21, 2022 by mikell Gianni 1 Link to comment Share on other sites More sharing options...
Deye Posted January 21, 2022 Author Share Posted January 21, 2022 (edited) Thanks mikell, I've noticed now that there are more various adjustments involved Here is the more authentic data input for this exercise : #include <Array.au3> _ArrayDisplay(_pInfoProc("scite.exe")) _ArrayDisplay(_pInfoProc()) Func _pInfoProc($iPID = "") ; from name or pid $iPID = (IsInt($iPID) ? "-id " : $iPID ? "-name " : "") & StringRegExpReplace($iPID, "\..{3}", "") Local $sCommand = "Get-Process " & $iPID & "|Select-Object MainWindowHandle,Id,ProcessName,MainWindowTitle,Handles|Select -First 4" $iPID = Run(@ComSpec & " /c " & 'powershell -Command "' & $sCommand & '|Format-List"', "", @SW_HIDE, $stdout_child) ProcessWaitClose($iPID) Local $s = StdoutRead($iPID) Return _2DHeadersToArray($s) EndFunc ;==>_pInfoProc Func _2DHeadersToArray($sVar = "", $sDelim = ':') Local $s = StringRegExpReplace(StringStripWS($sVar, 1), "(\w+)(.{2,})(\R)", "$1|") $s = StringSplit(StringStripWS($s, 3), @CR)[1] Local $s1 = StringRegExpReplace(StringStripWS($sVar, 1), '(\w+)(.{2,})(\R)', "$2|") $s &= @CRLF & $s1 $s = StringRegExpReplace($s, "\|\R", @CRLF) Return _VarTo2D($s, "|", ":") EndFunc ;==>_2DHeadersToArray ; Thanks Chimp Func _VarTo2D($var, $sSeparator = @TAB, $sDelim = ":") $var = StringStripWS($var, 3) Local $aRows = StringSplit(StringStripCR($var), @LF), $aColumns, $aResult[$aRows[0]][1] For $iRow = 1 To $aRows[0] $aColumns = StringSplit($aRows[$iRow], $sSeparator) If $aColumns[0] > UBound($aResult, 2) Then ReDim $aResult[$aRows[0]][$aColumns[0]] For $iColumn = 1 To $aColumns[0] $aResult[$iRow - 1][$iColumn - 1] = StringRegExpReplace(StringStripWS($aColumns[$iColumn], 3), $sDelim, "") Next Next Return $aResult EndFunc ;==>_VarTo2D Edited January 21, 2022 by Deye Link to comment Share on other sites More sharing options...
jguinch Posted January 22, 2022 Share Posted January 22, 2022 (edited) @Deye : Here is another way : $s = StringRegExpReplace($sVar, "\s+:\s+", "|") $cols = StringRegExpReplace($s, "(?s)^\R+|\|\K\N+\R(?!\R)||\|\N+\R{2,}.+", "") $values = StringRegExpReplace($s, "\R\w+|\R\K\R\w+\||\R+$", "") $result = $cols & $values ConsoleWrite($result & @LF) Edited January 22, 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 22, 2022 Author Share Posted January 22, 2022 (edited) Thanks @jguinch with my test, in one occasion your cols function stripped out a header so so far this is what i have saved : Func _2DHeadersToArray($sVar = "") Local $cols = StringRegExpReplace(StringRegExpReplace($sVar, "(\w+)(.{2,})(\R)", "|$1"), "(?m)\R\R|\R\|.*", "") Local $values = StringRegExpReplace(StringRegExpReplace($sVar, "(\w+)(.{2,})(\R)", "$2"), "[\h]+\:[\h]", "|") Local $s = StringStripWS($cols, 2) & @CRLF & StringStripWS($values, 3) $s = StringRegExpReplace($s, "(?m)^\|", "") ConsoleWrite($s & @LF & @LF) StringReplace($cols, "|", "|") Local $a[0][@extended] _ArrayAdd($a, $s) Return $a EndFunc Edited January 22, 2022 by Deye Link to comment Share on other sites More sharing options...
TheXman Posted January 22, 2022 Share Posted January 22, 2022 (edited) On 1/20/2022 at 2:07 PM, Deye said: the total outcome after processing, the data should look like so: $s = '' _ & 'MainWindowHandle|Id|ProcessName|CPU|Handles' _ & @LF & '132730|8808|SciTE|112.15625|512' _ & @LF & '656932|4268|iexplore|0.15625|400' _ & @LF & '1182078|6748|explorer|12.09375|1230' _ & @LF & '1837220|6868|explorer|6.171875|552' If you are going to execute a PowerShell command anyway, why not let Powershell do the heavy lifting in terms of formatting your output? Example: #include <Constants.au3> example() Func example() ;Command to get first 10 processes that have a window title and export ;the result to a pipe-delimited file. Const $PS_CMD = "Get-Process" & _ "|Select-Object MainWindowHandle,Id,ProcessName,MainWindowTitle,Handles" & _ "|Where-Object {$_.MainWindowTitle -ne ''}" & _ "|Select -First 10" & _ "|Export-CSV -path .\~temp.csv -Delimiter '|' -NoTypeInformation" Local $iPid = 0, $iExitCode = 0 Local $sCmdOutput = "" ;Execute the command $iPid = Run(StringFormat('powershell -command "%s"', $PS_CMD), "", Default, $STDERR_MERGED) ProcessWaitClose($iPid) $iExitCode = @extended $sCmdOutput = StdoutRead($iPid) If $iExitCode Then Exit ConsoleWrite("ERROR: " & @CRLF & $sCmdOutput & @CRLF) ;Show output ConsoleWrite(StringReplace(FileRead("~temp.csv"), '"', '') & @CRLF) ;Delete temp file If FileExists("~temp.csv") Then FileDelete("~temp.csv") EndFunc Example Output: MainWindowHandle|Id|ProcessName|MainWindowTitle|Handles 7669050|3932|firefox|SRER challenge - AutoIt General Help and Support - AutoIt Forums ? Mozilla Firefox|1117 13632660|2176|KeePass|KeePass.kdbx [Locked] - KeePass|229 15337406|4448|SciTE|C:\Projects\Personal\AutoIt\Test\A3Temp\a3_temp.au3 - SciTE [1 of 2]|197 18942332|4408|StikyNot|Sticky Notes|142 If you are using PowerShell 7.0+, you can use "-UseQuotes Never" in the Export-CSV. Then, you won't need to do the StringReplace to removes the quotes. I did it without -UseQuotes, in this example, so it would work using any versions of PowerShell. Edited January 22, 2022 by TheXman jguinch 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Deye Posted January 23, 2022 Author Share Posted January 23, 2022 Thanks @TheXman Thank you all and the pastime hobby drill exercise Link to comment Share on other sites More sharing options...
jguinch Posted January 23, 2022 Share Posted January 23, 2022 (edited) @TheXman : what about asking Powershell to to the whole job ? (replacement of " and without temp file) #include <Constants.au3> Local $PS_CMD = "(Get-Process|where{$_.MainWindowTitle}|select MainWindowHandle,Id,ProcessName,MainWindowTitle,Handles -first 4|ConvertTo-Csv -NoTypeInformation -Delimiter '|')-replace '""""', ''" $iPid = Run(StringFormat('powershell -command "%s"', $PS_CMD), "", @SW_HIDE, $STDOUT_CHILD ) ProcessWaitClose($iPid) $sOut = StdoutRead($iPid) ConsoleWrite($sOut) Edited January 23, 2022 by jguinch TheXman 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF 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