Sign in to follow this  
Followers 0
Fyzzle

Searching a file for a line of text

12 posts in this topic

Heya,

What I'm trying to do is look into .cfg files (which are formatted like txt files) and pull out registration numbers to audit our network on a specific software piece.

The line is: "SerialNum"="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"

Quotes are included in the line and it occurs on different lines of the config file depending on version, options, etc so a filereadline isn't going to work. I have been reading into Regex but I admit it's a bit over my head at the moment. I'm hoping that seeing this example in action will help things come together.

I have most of the script written but I can't get that value. Is anyone able to help?

Share this post


Link to post
Share on other sites



Read the file. Do regex with option 3 and use this pattern. "SerialNum"="(\w*[-"])+


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Read the file. Do regex with option 3 and use this pattern. "SerialNum"="(\w*[-"])+

Wow thanks for the quick reply.

Edit:

I simplified what I was doing, hopefully I just missed something small, reposting once I have the code narrowed down to the easiest form that works :mellow:

Edited by Fyzzle

Share this post


Link to post
Share on other sites

That won't work.

The function returns an array (if success!)

So, you need to use _ArrayDisplay to show the result.

And you need to read the File with FileRead to get its content.


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

That won't work.

The function returns an array (if success!)

So, you need to use _ArrayDisplay to show the result.

And you need to read the File with FileRead to get its content.

Nice, you replied before I edited my lunacy :mellow:

Dim $searchString = "path" 
    $OpenFile = FileOpen($searchString, 1)
    FileRead($OpenFile)
    $Key = StringRegExp($OpenFile, '"SerialNum"="(\w*[-"])+',3)
        MsgBox(0, "Test", $Key)
FileClose($OpenFile)

This returns a 1 so it looks like it's finding it. Is there a way to output the array to a file with the results? I was just using the MsgBox for testing. What I am doing is putting this in the startup script and having the output go to a log file with a bunch of other stuff I like to gather.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Nice, you replied before I edited my lunacy :mellow:

Dim $searchString = "path" 
    $OpenFile = FileOpen($searchString, 1)
    FileRead($OpenFile)
    $Key = StringRegExp($OpenFile, '"SerialNum"="(\w*[-"])+',3)
        MsgBox(0, "Test", $Key)
FileClose($OpenFile)

This returns a 1 so it looks like it's finding it. Is there a way to output the array to a file with the results? I was just using the MsgBox for testing. What I am doing is putting this in the startup script and having the output go to a log file with a bunch of other stuff I like to gather.

Oi never mind When i add the single quotes to the pattern in order for the variable to be accepted it ruins the pattern. Back to the drawing board. It just doesn't seem to like all the quotes.

Grats BTW on the medals in Vancouver :( I noticed you're from Germany.

Edited by Fyzzle

Share this post


Link to post
Share on other sites

Oi never mind When i add the single quotes to the pattern in order for the variable to be accepted it ruins the pattern. Back to the drawing board. It just doesn't seem to like all the quotes.

Grats BTW on the medals in Vancouver :mellow: I noticed you're from Germany.

I made this string up outside of Autoit and it works.

^\"SerialNum\"=\"[0-9A-Z]{5}-[0-9A-Z]{5}-[0-9A-Z]{5}-[0-9A-Z]{5}-[0-9A-Z]{5}\"$

I'm missing something here...

I misinterpreted as well, a return of 1 is error with a 3 flag. So it looks like yours is good as well (not that I doubted)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I'd love to be able to get my head around regex, and I have a little try when I see a thread like this.

I tried to make a pattern (my first one) and it sort of does a job

but only if your serials are exactly the same length and format.

and only in element[0] of array.

#include <Array.au3>

$sStr = 'hcbhjldfgeybhvb99(((((>>>>>>"SerialNum"="XXAXX-XX2XX-XXqXX-XXwXX-XX0XX"ghjdcgvjnSSf"""""%%&>>>>?IGH'

$Key = StringRegExp($sStr, '("SerialNum"=")(.){30}',2)
_ArrayDisplay($Key)

EDIT: this is far from a solution to your problem.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

I'd love to be able to get my head around regex, and I have a little try when I see a thread like this.

I tried to make a pattern (my first one) and it sort of does a job

but only if your serials are exactly the same length and format.

and only in element[0] of array.

#include <Array.au3>

$sStr = 'hcbhjldfgeybhvb99(((((>>>>>>"SerialNum"="XXAXX-XX2XX-XXqXX-XXwXX-XX0XX"ghjdcgvjnSSf"""""%%&>>>>?IGH'

$Key = StringRegExp($sStr, '("SerialNum"=")(.){30}',2)
_ArrayDisplay($Key)

EDIT: this is far from a solution to your problem.

Hey no worries, i'll see if I can get my head around it myself tonight when I get home. At the very least it may point me in the right direction.

Share this post


Link to post
Share on other sites

What are you doing. :mellow:

#include <Array.au3>
;~ Global $searchString = 'yourFile.extension'
;~ $filecontent = FileRead(FileOpen($searchString, 1))
; to fake the FileRead I use a string

$filecontent = 'hcbhjldfgeybhvb99(((((>>>>>>"SerialNum"="XXAXX-XX2XX-XXqXX-XXwXX-XX0XX"ghjdcgvjnSSf"""""%%&>>>>?IGH' & @CRLF & _
'>>"SerialNum"="12343-XX24X-XXqXX-XXwXX-XX0XX"ghjdcgvjnSSf"""'
$Key = StringRegExp($filecontent, '"SerialNum"="(\w{5}-\w{5}-\w{5}-\w{5}-\w{5})+', 3)
_ArrayDisplay($Key)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

What are you doing. :mellow:

#include <Array.au3>
;~ Global $searchString = 'yourFile.extension'
;~ $filecontent = FileRead(FileOpen($searchString, 1))
; to fake the FileRead I use a string

$filecontent = 'hcbhjldfgeybhvb99(((((>>>>>>"SerialNum"="XXAXX-XX2XX-XXqXX-XXwXX-XX0XX"ghjdcgvjnSSf"""""%%&>>>>?IGH' & @CRLF & _
'>>"SerialNum"="12343-XX24X-XXqXX-XXwXX-XX0XX"ghjdcgvjnSSf"""'
$Key = StringRegExp($filecontent, '"SerialNum"="(\w{5}-\w{5}-\w{5}-\w{5}-\w{5})+', 3)
_ArrayDisplay($Key)

Cocaine is one hell of a drug....

That's working beautifully, and the _arraydisplay is a lot simpler than I was making it :( The output to the array is:

XXAXX-XX2XX-XXqXX-XXwXX-XX0XX

12343-XX24X-XXqXX-XXwXX-XX0XX

I understand why, working on how to get it to display the actual data.

Share this post


Link to post
Share on other sites

No problem. When there is still something open, just ask again.


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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