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

    • ur
      By ur
      Hi,
       
      I have a property file format configuration file for our project.
      The sample file is as below.
      BuildLocation:C:\Build BuildExe:erwin Data Modeler r9.7 (64-bit)_2378.exe Release:r9.64.02 Silent:No InstallPath:default Compare :No MartUpgrade :Yes Bit:64 ERwinUpgrade:No License_File:150416-1952 Navigator (ca.com).lic To read this file, I am using below code.
      Func readConfig($sFilePath,$intStartCode) ;Usage: MsgBox(0,"Silent",readConfig(@ScriptDir&"\Config.txt","Silbent")) ;$sReplaceText = "Mani Prakash" ;$sFilePath = "C:\Users\KIRUD01\Desktop\Config.txt" ;$intStartCode = "BuildExe" $arrRetArray = "" $s = _FileReadToArray($sFilePath, $arrRetArray);Reading text file and saving it to array $s will show status of reading file.. For $i = 1 To UBound($arrRetArray)-1 $line = $arrRetArray[$i];retrieves taskengine text line by line If StringInStr($line, $intStartCode) Then ConsoleWrite ("Starting point "& $line & @CRLF) return StringStripWS(StringSplit($line,":")[2],$STR_STRIPLEADING + $STR_STRIPTRAILING ) EndIf if $i = UBound($arrRetArray)-1 then return "Not Found" Next EndFunc The above code is working to read the particular key value.
      But problem is , if I try to read the key "Bit" it is giving the value of key "BuildExe" as the line contains the word "bit"..
      Can you suggest how to do this. 
       
      If possible I need to fix writeConfig also.
       
      Func writeConfig($sFilePath,$intStartCode,$sReplaceText) ;$sReplaceText = "Mani Prakash" ;$sFilePath = "C:\Users\KIRUD01\Desktop\Config.txt" ;$intStartCode = "BuildExe" $arrRetArray = "" $s = _FileReadToArray($sFilePath, $arrRetArray);Reading text file and saving it to array $s will show status of reading file.. $intStartingPointFound = 0 For $i = 1 To UBound($arrRetArray)-1 $line = $arrRetArray[$i];retrieves taskengine text line by line If StringInStr($line, $intStartCode) Then $intStartingPointFound = 1;if found the starting point of the module to copy then set this variable to 1 ConsoleWrite ("Starting point " & @CRLF) $arrRetArray[$i] = $intStartCode & ": " & $sReplaceText ExitLoop EndIf if $i = UBound($arrRetArray)-1 then ConsoleWrite("Not Found" & @CRLF) Next _FileWriteFromArray ($sFilePath, $arrRetArray,1) EndFunc  
    • virhonestum
      By virhonestum
      Hey,
      I've coma accross a very odd problem. I want to download a CSV-File, and process the contents.
      This is the extremely simplyfied AutoIT-Code, given the file is already downloaded:
      $f= @ScriptDir & "\TestFile.csv" $file = FileOpen($f,0) Local $line = FileReadLine($file) MsgBox(0,"",$line) The downloaded CSV file I want to process contains something like this:
      Artikelnummer;EAN-Code;Artikelname;Artikelgewicht;Beschreibung;Kurzbeschreibung;Eigenschaften;Technische-Daten;Bild1;Bild2;Bild3;Bild4;Bild5;Bild6;Bild7;Bild8;Lieferbar;"Lieferbar Ab";Versandzeit;"UVP-Preis inkl. MwSt.";"Preis1";"Preis2";Hersteller L7335272;5420025602129; Mini Light XLR ;0.1000; JB Systems Schwanenhalsleuchte mit XLR Anschluss. ;;;;http://www.example.com/media/images/org/pic20070114153500a.jpg;;;;;;;;JA;; 1-3 Tage ;12,90;12,90;6,57; JB Systems L3320502;540207025601636; Mini Light LED BNC ;0.1000; JB Systems LED Schwanenhalsleuchte mit BNC Anschluss. ;;;;http://www.example.com/media/images/org/pic20061231171705a.jpg;;;;;;;;JA;; 1-3 Tage ;29,90;25,89;15,26; JB Systems L1332254;542002556023143; Mini Light LED XLR ;0.1000; JB Systems LED Schwanenhalsleuchte mit XLR Anschluss. ;;;;http://www.example.com/media/images/org/pic20061231171728a.jpg;;;;;;;;JA;; 1-3 Tage ;29,90;25,89;15,26; JB Systems L8302591;504200256280277; Spiegelkugel 10cm ;0.5000; JB Systems Spiegelkugel 10cm Durchmesser mit einer hohen Dichte durch 10 x 10 mm Echtglasspiegel. ;;;;http://www.example.com/media/images/org/pic20060324214825a.jpg;;;;;;;;JA;; 1-3 Tage ;5,50;4,90;2,81; JB Systems L7302932;542000256510222; Spiegelkugel 20cm ;0.8400; JB Systems Spiegelkugel 20cm Durchmesser mit einer hohen Dichte durch 10 x 10 mm Echtglasspiegel. ;;;;http://www.example.com/media/images/org/pic20060324214907a.jpg;;;;;;;;JA;; 1-3 Tage ;12,90;11,50;6,58; JB Systems L2350293;534200562064239; Spiegelkugel 30cm ;2.1300; JB Systems Spiegelkugel 30 cm Durchmesser mit einer hohen Dichte durch 10 x 10 mm Echtglasspiegel. ;;;;http://www.example.com/media/images/org/pic20060324214956a.jpg;;;;;;;;JA;; 1-3 Tage ;26,90;23,00;13,72; JB Systems L3302984;545200252024246; Spiegelkugel 40cm ;3.5000; JB Systems Spiegelkugel 40cm Durchmesser mit Sicherungsring und einer hohen Dichte durch 10 x 10 mm Echtglasspiegel. ;;;;http://www.example.com/media/images/org/pic20060324215050a.jpg;;;;;;;;JA;; 1-3 Tage ;54,90;49,00;28,00; JB Systems L9302495;542205056225600; Spiegelkugel 50cm ;5.3900; JB Systems Spiegelkugel 50cm Durchmesser mit Sicherungsring und einer hohen Dichte durch 10 x 10 mm Echtglasspiegel. ;;;;http://www.example.com/media/images/org/pic20060324215122a.jpg;;;;;;;;JA;; 1-3 Tage ;89,00;79,00;45,39; JB Systems But the message box that pops up after FileReadLine contains this:
      䅲瑩步汮畭浥爻䕁中䍯摥㭁牴楫敬湡浥㭁牴楫敬来睩捨琻䉥獣桲敩扵湧㭋畲穢敳捨牥楢畮朻䕩来湳捨慦瑥渻呥捨湩獣桥ⵄ慴敮㭂楬搱㭂楬搲㭂楬搳㭂楬搴㭂楬搵㭂楬搶㭂楬搷㭂楬搸㭌楥晥牢慲㬢䱩敦敲扡爠䅢∻噥牳慮摺敩琻≕噐ⵐ牥楳⁩湫氮⁍睓琮∻≐牥楳ㄢ㬢偲敩猲∻䡥牳瑥汬敲ੌ㜳㌵㈷㈻㔴㈰〲㔶〲ㄲ㤻M楮椠䱩杨琠塌刀㬰⸱〰〻J䈠卹獴敭猠卣桷慮敮桡汳汥畣桴攠浩琠塌删䅮獣桬畳献;㬻㭨瑴瀺⼯睷眮數慭灬攮捯洯浥摩愯業慧敳⽯牧⽰楣㈰〷〱ㄴㄵ㌵〰愮橰朻㬻㬻㬻㭊䄻㬀ㄭ㌠呡来;ㄲⰹ〻ㄲⰹ〻㘬㔷㬀䩂⁓祳瑥浳 I've attached both files I use. 
      My guess is, that there's something wrong with the encoding, but I'm not sure how to fix it.
       
      Thank you very much for your help
      - virhonestum
      Encodingtester.au3
      TestFile.csv
    • 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
       
    • 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??