blemas

Split a multi line string into separate variables

17 posts in this topic

The McAfee return for AV defs per "C:\Progra~1\Common~1\McAfee\SystemCore\csscan.exe -Versions" is ...

CommonShell Command Line Scanner Lite (VSCORE.15.5.0.3960)

    Engine version: 5800.7501

    DAT version:    8450.0

    Time required:  15 milliseconds

I want to isolate the actual DAT version as "8450.0".  There may be an easier way to get the DAT Version via other McAfee or registry methods but essentially I just want to know how to parse a string at a character or @CRLF into two separate strings for further parsing. Example:

$string = "Name=Microsoft Windows 10 Professional |C:\windows|\Device\Harddisk0\Partition2"

Parse into $var1 = "Name" & $var2 = "Microsoft Windows 10 Professional |C:\windows|\Device\Harddisk0\Partition2"

From there I'd like to parse $var2 at "|" into  $var3 = "Microsoft Windows 10 Professional" and $var4 "C:\windows|\Device\Harddisk0\Partition2"

 

 

 

 

 

Share this post


Link to post
Share on other sites



Welcom to AutoIt and the forum!

Use something like this:

$string = "Name=Microsoft Windows 10 Professional |C:\windows|\Device\Harddisk0\Partition2"
$aResult1 = StringSplit($string, "=")
$aResult2 = StringSplit($aResult1[2], "|")

You get two arrays with the resulting substrings.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks for the help. That looks like it would work but I'm getting no results (pebkac?).

$string = "Name=Microsoft Windows 10 Professional |C:\windows|\Device\Harddisk0\Partition2"
$aResult1 = StringSplit($string, "=")
$aResult2 = StringSplit($aResult1[2], "|")
MsgBox(0, "aResult1", "" & $aResult1, 1.5)
MsgBox(0, "aResult2", "" & $aResult2, 1.5)

What am I missing?

 

Share this post


Link to post
Share on other sites

Moved to General Help and Support, as the Examples forum clearly states:

Quote

Do not post general support questions here, instead use the AutoIt Help and Support forums.

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

It's an array not a string, so use _ArrayDisplay in place of MsgBox

_ArrayDisplay($aResult1)
_ArrayDisplay($aResult2)

 

Edited by Subz

Share this post


Link to post
Share on other sites

Got it...

#include <Constants.au3>

Global $DOS, $Message = '' ;; added "= ''" for show only.

$DOS = Run(@ComSpec & " /c C:\Progra~1\Common~1\McAfee\SystemCore\csscan.exe -Versions", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($DOS)
$local_av_version = StdoutRead($DOS)

MsgBox(65536, "", "The McAfee AV information" & @CRLF & $local_av_version)

Dim $split[3]
Global $split = StringSplit($local_av_version, ":")
$1 = $split[1]
$2 = $split[2]
$variable = $split[3]

Dim $sourcetwo[2]
$sourcetwo = StringSplit($variable, "Time")
$DAT_Version = $sourcetwo[1]
$b = $sourcetwo[2]

$DAT_Ver = StringStripWS($DAT_Version,$STR_STRIPALL)
MsgBox(0, 'The McAfee DAT Version is....', "Results = " & $DAT_Ver)

 

Share this post


Link to post
Share on other sites

You could also do it like so:

$sString = ""
$sString &= "CommonShell Command Line Scanner Lite (VSCORE.15.5.0.3960)" & @LF
$sString &= "    Engine version: 5800.7501" & @LF
$sString &= "    DAT version:    8450.0" & @LF
$sString &= "    Time required:  15 milliseconds" & @LF

$aString = StringSplit($sString, @LF)
For $i = 1 To $aString[0]
    If StringInStr($aString[$i], "DAT Version:") Then $sDATVersion = StringStripWS(StringReplace($aString[$i], "DAT Version:", ""), 8)
Next
ConsoleWrite($sDATVersion & @CRLF)

 

1 person likes this

Share this post


Link to post
Share on other sites

That's a great piece of code, I may end up using it. Thanks!

Share this post


Link to post
Share on other sites

Much cleaner. I'm going with this....

Share this post


Link to post
Share on other sites

#10 ·  Posted

 

#include <Constants.au3>

Global $DOS, $Message = ''

$DOS = Run(@ComSpec & " /c C:\Progra~1\Common~1\McAfee\SystemCore\csscan.exe -Versions", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($DOS)
$sString = StdoutRead($DOS)

$aString = StringSplit($sString, @LF)
For $i = 1 To $aString[0]
    If StringInStr($aString[$i], "DAT Version:") Then $sDATVersion = StringStripWS(StringReplace($aString[$i], "DAT Version:", ""), 8)
Next
MsgBox(0,"", "DATVersion=" & $sDATVersion & @CRLF)

 

Share this post


Link to post
Share on other sites

#11 ·  Posted

Thanks again

Share this post


Link to post
Share on other sites

#12 ·  Posted

#include <Array.au3>
$string = "Name=Microsoft Windows 10 Professional |C:\windows|\Device\Harddisk0\Partition2"
$array1 = StringSplit($string,"=,|",2)
_ArrayDisplay($array1)

 

Share this post


Link to post
Share on other sites

#13 ·  Posted

What is the "," for in your example? There is no "," in the example data to split at :huh:


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#14 ·  Posted

or
 

#include <Array.au3>
$sString  = "    Engine version: 5800.7501" & @LF
$sString &= "    DAT version:    8450.0" & @LF
$sString &= "    Time required:  15 milliseconds" & @LF

$array1 = StringRegExp($sString,"DAT version:\s*(\d+\.?\d*)",1)
_ArrayDisplay($array1)

the regex reads:
"DAT version" then zero or more spaces then one or more digits then zero or one fullstop then zero or more digits

Share this post


Link to post
Share on other sites

#15 ·  Posted

hi water, that comma slipped past me .... it's the end of the day here and i'm pooped :yawn:

Share this post


Link to post
Share on other sites

#16 ·  Posted

I see. Good night :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

  • Similar Content

    • kneze
      By kneze
      Hi
      i try to read Wlan Profiles from local Computer. First Step Command Prompt: netsh wlan show profiles >> C:\temp\test\wlan.txt.
      Second Step (here's a sample script to find a solution for my problem. Script read each line of wlan.txt and Display Name of Wireless Connection which i have set up in the past.  If i edit wlan.txt to wlan - sample.txt script works. wlan.txt containsline without : so i get error message. How can i read only lines beginning with All User Profile so i can use original file wlan.txt which i crate in the command prompt with: netsh wlan show profiles >> C:\temp\test\wlan.txt ?
      Thanks for any suggestions.
      Kneze
      $Form1 = GUICreate("Form1", 374, 268, 892, 512) $Button1 = GUICtrlCreateButton("button", 145, 35, 75, 25) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $file = "C:\temp\test\wlan.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) $after = StringSplit($line, ":")[2] MsgBox(262144, "Result", $after, 0) Next FileClose($file) EndSwitch WEnd  
       
      wlan - sample.txt
      wlan.txt
    • Cormin
      By Cormin
      My script currently reads a text file line by line. I want it to look for this the chat message below and skip the session messages. 
      There are some garbage lines that I want it to skip over. So if a line doesnt match that format then skip it. I tried experimenting with StringRegExp but I think I'm using it wrong. Heres my code
      If StringRegExp($read, '\W\d\d\w\d\d\D.\W') = Not @error Then I was hoping that this would be able to see the open bracket, 2 digits, a colon, 2 digits,  close bracket, a space and a <. But it doesn't seem to be working.
       
      Eventually I want to be able to extract the username and chat message into separate strings but I haven't got that far yet.
    • ripdad
      By ripdad
      Here is  another WMIC script that parses properties from various alias classes.
      ie: COMPUTERSYSTEM, CDROM, IDECONTROLLER, DISKDRIVE, etc.
      Download:  WMIC_GET.au3
       
    • crushyna
      By crushyna
      Hello everyone!

      I've encountered a strange problem while adding files (or rather file names) to a ListView.
      Mechanics are simple:
      1. Select files to add (FileOpenDialog + Multiselect)
      2. Split selected string (filepath) into array (StringSplit)
      3. File names appear on ListView (GUICtrlCreateListViewItem)

      Writing starts from second array (since 0 is array size, and 1 is filepath).
      Problem occurs when I try to add one file. Then, no filenames are added.
      As far as I know, selecting one file generates only 2 strings (0 and 1) to split, therefore no string is written into ListView.
       
      Is there any way / workaround to make this thing work properly?

      Some code here:
      While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $idAddTest $sMessage = "Select test cases to load:" $sFileOpenDialog = FileOpenDialog($sMessage, @WorkingDir & "\data\tests\", "All (*.*)", $FD_FILEMUSTEXIST & $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No test(s) were selected.") FileChangeDir(@ScriptDir) Else FileChangeDir(@ScriptDir) $aFileList = StringSplit($sFileOpenDialog, "|", @CRLF) For $i = 2 to $aFileList[0] MsgBox( 0, "test", $aFileList[$i]) ;just for testing purposes GUICtrlCreateListViewItem($aFileList[$i], $idTestCaseList) Next EndIf EndSwitch WEnd  
      And nothing more spectacular from ListView side:
      Global $idTestCaseList = GUICtrlCreateListView("List of Tests ", 24, 24, 225, 502, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT)) GUICtrlSetTip(-1, "List of test cases."& @CRLF &"Hold CTRL to select multiple lines.")  
      Any help appreciated!
    • Vivaed
      By Vivaed
      I have another AutoIT script making a Log file 

      Sample of Log file:
      2016/08/22 12:44:18 > Process: [RUNNING] [ACTIVE] 2016/08/22 12:48:35 > Process: [WAS NOT RUNNING] 2016/08/22 13:40:00 > Process: [FAILED] 2016/08/22 14:01:10 > Process: [WAS NOT RUNNING] I am looping through the Log file for  the word "FAILED"
      I then want to get all lines that have "FAILED" and get their TIME
       
      My Current code to get this far:
      If FileExists($fileLog) Then $contents = FileRead($fileLog) If @error Then MsgBox(0, 'File Error', $fileLog & ' could not be read.') Else For $i = 1 To _FileCountLines($fileLog) $result = StringInStr($contents,$search) If $result >= 1 Then $filteredLine = FileReadLine($fileLog,$i) If StringInStr($filteredLine,$search) Then ConsoleWrite($filteredLine & @CRLF) ; this gets me the results I want sans the time parse EndIf Else ConsoleWrite( $search & " not found!" & @CRLF) EndIf Next EndIf EndIf For this part:
      If StringInStr($filteredLine,$search) Then ConsoleWrite($filteredLine & @CRLF) ; this gets me the results I want sans the time parse EndIf OUTPUT: 2016/08/22 13:40:00 > Process: [FAILED] I dont understand how I read the time in that output?
       
      I have tried _DateTimeFormat - Dont think this applies 
      Tried _DateDiff - I dont have a the date yet so this doesnt work
       
      Would love if someone could tell me if I am thinking is the wrong direction and possibly lead me down the correct path to light side of the force