Sign in to follow this  
Followers 0
digitalexpl0it

Parse File - By Keyword

6 posts in this topic

Hello,

I am not sure where to start on this, but maybe some help from the community will help. I understand how to open a file and read it with autoit. My issue is this, I need to be able to select all the data on certain lines of the file by a keyword. I have a file that has multiple lines with the word "proxyAddresses:" that starts at the beginning of each line I want, there are other lines with other information I do not want. How can I select what I want and put it into an array? 

 

Example of file

dn: CN=John Doe,OU=Techs,OU=Users,DC=foo,DC=bar
changetype: add
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
proxyAddresses: 
 X500:/o=FooBar/ou=Exchange Administrative Group (FYDI)/cn=Re
 cipients/cn=Jogn Doec52
proxyAddresses: 
 x500:/o=ExchangeLabs/ou=Exchange Administrative Group (FYSPDLT)/cn=Rec
 ipients/cn=3ab8cd9xxxxxxxxxxxxxxxxxxxxxf64e0c-John Do
proxyAddresses: sip:jdoe@foobar.com
proxyAddresses: smtp:jdoe@foobar.mail.onmicrosoft.com
proxyAddresses: SMTP:jdoe@foobar.com

 

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

remove the proxy text...

 

#include <Array.au3>
#include <File.au3>

Local $aRetArray, $sFilePath = "Text.txt", $aArray[6], $count, $temp

_FileReadToArray($sFilePath, $aRetArray)
If @error Then Exit MsgBox(0, "Error", "Error")

For $i = 1 To UBound($aRetArray) - 1
    If StringInStr($aRetArray[$i], "proxyAddresses:") Then
        $temp = StringTrimLeft($aRetArray[$i], 15)
        $count += 1
        $aArray[$count] = $temp
    EndIf
Next

_ArrayDisplay($aArray, "1D array - count", Default, 8)

 

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

You guys rock, thanks that is what I was looking for

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Have a little trouble not sure why I get an error at $aArray[$count] = StringStripWS($temp,1), it works then the last two that it fins it doesn't like the array.

 

#include <Array.au3>
#include <File.au3>


local $user = @username
RunWait(@ComSpec & ' /c ldifde -l proxyAddresses,objectClass -f ' & $user & '.txt -r "(proxyAddresses=smtp:' & $user & '@foobar.com)"')
sleep(1000)
Local $aRetArray, $sFilePath = $user & ".txt", $aArray[6], $count, $temp

_FileReadToArray($sFilePath, $aRetArray)
If @error Then Exit MsgBox(0, "Error", "Error")

For $i = 1 To UBound($aRetArray) - 1
    If StringInStr($aRetArray[$i], "proxyAddresses:") Then
        $temp = StringTrimLeft($aRetArray[$i], 15)
        $count += 1
        $aArray[$count] = StringStripWS($temp,1)
        if ($aArray[$count] <> "") then
            msgbox(0,"",$aArray[$count])
        endif
    EndIf
Next

Text file

 

dn: CN=John Doe,OU=Valley,OU=California,OU=Agencies,DC=foo,DC=bar
changetype: add
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
proxyAddresses: smtp:jdoe@xxxxvalley.com
proxyAddresses: SMTP:johndoeN@xxxxvalley.com
proxyAddresses: smtp:john-doe@foobar.com
proxyAddresses: smtp:john.doe@foobar.com
proxyAddresses: 
 X500:/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Rec
 ipients/cn=3aec3f9078ee4a2a8c4c058d3df3f6c0-Elizabeth N
proxyAddresses: smtp:jdoe@xxxxvalley.com
proxyAddresses: smtp:john-doe@xxxxtent.mail.onmicrosoft.com

N/M I see that the array was set to 6 and more data was beeing added, upped it to 10 and its fine

Edited by digitalexpl0it

Share this post


Link to post
Share on other sites

You don't like Iczer's solution ? It's smart though :

#include <Array.au3>

$file = FileRead("test.txt")
$aproxyAddresses = StringRegExp($file,"proxyAddresses:\h*(\S+)",3)
_ArrayDisplay($aproxyAddresses)

 

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
Sign in to follow this  
Followers 0

  • Similar Content

    • 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
       
    • ur
      By ur
      How to get the thrid number of a file version.
      Example: Let's say I have a file with below properties.
      I want only the third number like here it is 1941 as highlighted.
      Is there any built in function to get it??

    • ur
      By ur
      Generally we will use tools like Winmerge or Beyond Compare for this purpose.
      Are there any UDF or libraries available in AutoIT to compare any two files or folder contents.
    • tremolux66
      By tremolux66
      I've run into the problem where multiple script processes writing to a log file at the same time can interfere with one another. That is, file writes using FileWriteLine are not atomic, so a line of text from one process can have fragments of text from other processes mixed in with it. Exclusive file-locking should take care of the problem.
      Looking through the standard UDFs, I see that _WinAPI_LockFile is available, but according to the MSDN documentation it doesn't block - it returns immediately if the lock can't be obtained - and there doesn't seem to be a UDF for the LockFileEx function. I considered writing my own UDF for LockFileEx, but some aspects (e.g., creating the OVERLAPPED structure and its members) look too complex for the amount of time I have available. I think approximating LockFileEx using LockFile could solve my problem, but I'd like to keep it simple while still working reliably. I'm not confident that (e.g.) a simple retry loop to simulate blocking will be adequate.
      Any suggestions for a simple way to achieve exclusive file-locking with blocking?
    • Rapidnxit
      By Rapidnxit
      Hi! I want to send by mail some files with a certain extension (.xml in my case). These files are located in the script folder. First, I thought to use _FileListToArrayRec function to have a list of these files. But than I don't know how to send them all at once. I know that I can attach multiple files in this way: path1;path2;path3; etc.  and so I have tried to make a string of this type with the path of the files (with a for loop) but It doesn't attach any file (only instructions.txt). How could I do? ( what I did is just an idea, if there's something better that would be great) Thanks!
      $aArrayXml = _FileListToArrayRec(@ScriptDir, "*.xml", $FLTAR_FILES) _ArrayDisplay($aArrayXml, "LIST XML") $LenghtArrayXml = UBound($aArrayXml) If ($LenghtArrayXml > 2) Then For $i = 2 To $LenghtArrayXml - 1 $temp = "&@ScriptDir&""\"&$aArrayXml[$i]&";"&"""" $XmlListFile = $XmlListFile & $temp Next EndIf $XmlListFile = StringTrimLeft($XmlListFile, 1) if($LenghtArrayXml = 2)Then $rc =_INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, @ScriptDir&"\"&$aArrayXml[1]&";"&@ScriptDir&"\instructions.txt", $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl) Else $rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, @ScriptDir&"\"&$aArrayXml[1]&$XmlListFile&@ScriptDir&"\instructions.txt", $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl) EndIf