Jump to content

Trim certain parts of a line containing my searched string


Recommended Posts

Im searching a file for a certain word, "Test" in this case. When I find "test" I need to get the persons name who sent me "test".

example of text:

From [John]: Test

So I am looking to get john. Im pretty new to this and just learning autoit. Any help is appreciated.

Below is what I have so far to find the word test

$FileName = "c:\logfile.txt"

$Source = FileRead($FileName)

$Word = "Test"

If StringInStr($Source, "$Word") Then
MsgBox (1, "Result", "$Word")
Else
MsgBox (1, "Result", "Not Found")
EndIf
Link to comment
Share on other sites

Im searching a file for a certain word, "Test" in this case. When I find "test" I need to get the persons name who sent me "test".

One method would be to write a wrapper around a windows port of "grep" or the windows "find" utility.

This would provide scalability should your logsize increase.

Otherwise, read the file contents into an array or read / test each line individually.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

stringmid($source,StringInStr($Source, "$Word")-7,5)

Would probably work in thise case but this changes depending on how long the users name is...

stringmid extracts from $source, starts at the posision of "Word"-7, that is 7 charactors before word, and get the next 5 charactors...should be about right (depends on if there's a space there or not)..but its harder to make this dynamic if the user name is not known before hand:P

Link to comment
Share on other sites

Hi,

how does the log look like? maybe RegExp can do the job too.

So long,

Mega

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

Link to comment
Share on other sites

Hi,

how does the log look like? maybe RegExp can do the job too.

So long,

Mega

log is pretty basic saved chatlog with each event on a seperate line. I need to search the txt file to see if a word (Test) is in there. If it is I need to get the persons name, which always starts at 7th space and ends 4 spaces before the word, test in this case.

To [John]: Hey

From [bill]: Test

From [bill]: what time is it

[Jill] logged on

To [bill]: Its 3:17

Edited by wpsatisfide
Link to comment
Share on other sites

  • Moderators

log is pretty basic saved chatlog with each event on a seperate line. I need to search the txt file to see if a word (Test) is in there. If it is I need to get the persons name, which always starts at 7th space and ends 4 spaces before the word, test in this case.

To [John]: Hey

From [bill]: Test

From [bill]: what time is it

[Jill] logged on

To [bill]: Its 3:17

#include <string.au3>
#include <array.au3>
$sString = "To [John]: Hey" & @CRLF & _
    "From [Bill]: Test" & @CRLF & _
    "From [Bill]: what time is it" & @CRLF & _
    "[Jill] logged on" & @CRLF & _
    "To [Bill]: Its 3:17 "
$SearchString = 'Test'
$aArray = _StringBetween($sString, 'From\s*\[', ']:\s*' & $SearchString, -1, 1)
_ArrayDisplay($aArray, '')

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

#include <string.au3>
#include <array.au3>
$sString = "To [John]: Hey" & @CRLF & _
    "From [Bill]: Test" & @CRLF & _
    "From [Bill]: what time is it" & @CRLF & _
    "[Jill] logged on" & @CRLF & _
    "To [Bill]: Its 3:17 "
$SearchString = 'Test'
$aArray = _StringBetween($sString, 'From\s*\[', ']:\s*' & $SearchString, -1, 1)
_ArrayDisplay($aArray, '')

I dont have a damn clue whats going on here, but it works.

Thank you very much

Link to comment
Share on other sites

HI,

#include <array.au3>
$sString = "To [John]: Hey" & @CRLF & _
        "From [Bill]: Test" & @CRLF & _
        "Fro    qwm    [Hugo]      : Test" & @CRLF & _
        "From [Bill]: what time is it" & @CRLF & _
        "[Jill] logged on" & @CRLF & _
        "To [Bill]: Its 3:17 "

;$aArray = StringRegExp($sString, '\[.*](?=(.*Test))', 3) Thought this should work, but it doesn't like Regexbuddy shows :-(
$aArray = StringRegExp($sString, '(\[.*])(?=(.*Test))', 3)
_ArrayDisplay($aArray, '')

So long,

Mega

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

Link to comment
Share on other sites

HI,

#include <array.au3>
$sString = "To [John]: Hey" & @CRLF & _
        "From [Bill]: Test" & @CRLF & _
        "Fro    qwm    [Hugo]      : Test" & @CRLF & _
        "From [Bill]: what time is it" & @CRLF & _
        "[Jill] logged on" & @CRLF & _
        "To [Bill]: Its 3:17 "

;$aArray = StringRegExp($sString, '\[.*](?=(.*Test))', 3) Thought this should work, but it doesn't like Regexbuddy shows :-(
$aArray = StringRegExp($sString, '(\[.*])(?=(.*Test))', 3)
_ArrayDisplay($aArray, '')

So long,

Mega

Yeah but i dont need to search

To [John]: Hey

From [bill]: Test

From [bill]: what time is it

[Jill] logged on

To [bill]: Its 3:17

I need to search logfile.txt, so I dont think thats going to work.

Link to comment
Share on other sites

Hi,

#include <array.au3>
$aArray = StringRegExp(FileRead(FileOpen(@ScriptDir & "\logfile.txt", 0)), '(\[.*])(?=(.*Test))', 3)
_ArrayDisplay($aArray, '')

So long,

Mega

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

Link to comment
Share on other sites

  • Moderators

HI,

#include <array.au3>
$sString = "To [John]: Hey" & @CRLF & _
        "From [Bill]: Test" & @CRLF & _
        "Fro    qwm    [Hugo]      : Test" & @CRLF & _
        "From [Bill]: what time is it" & @CRLF & _
        "[Jill] logged on" & @CRLF & _
        "To [Bill]: Its 3:17 "

;$aArray = StringRegExp($sString, '\[.*](?=(.*Test))', 3) Thought this should work, but it doesn't like Regexbuddy shows :-(
$aArray = StringRegExp($sString, '(\[.*])(?=(.*Test))', 3)
_ArrayDisplay($aArray, '')oÝ÷ Ù*%¢xz¿ªê-zØfz«Ê·ö·°¢¹"¨º´^Li­«az¸Z­ÛZ楢¶Øb±«­¢+Ø¥¹±Õ±ÐíÉÉä¹ÔÌÐì(ÀÌØíÍMÑÉ¥¹ôÅÕ½ÐíQ¼m)½¡¹tè!äÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½Ðíɽ´m   ¥±±tèQÍÐÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½Ðíɼ%ÅÝ´m!Õ½tèQÍÐÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½Ðíɽ´m   ¥±±tèÝ¡ÐÑ¥µ¥Ì¥ÐÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½Ðím)¥±±t±½½¸ÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½ÐíQ¼m  ¥±±tè%ÑÌÌèÄÜÅÕ½Ðì(ÀÌØíMÉ¡MÑÉ¥¹ôÌäíQÍÐÌäì(ÀÌØíÉÉäôMÑÉ¥¹IáÀ ÀÌØíÍMÑÉ¥¹°Ìä츨üÀäÈíl ¸¨ü¤ÀäÈít¸¨üÌäìµÀìÀÌØíMÉ¡MÑÉ¥¹°Ì¤)}ÉÉå¥ÍÁ±ä ÀÌØíÉÉä°ÌäìÌäì¤

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

@thmeger... you're working your RegExp rather hard... take a look at this

...

Hi Ron,

I thought he needs to know wether Test is in the string and if it is, then he needs the name. He doesn't need all the things/names between the [] that is way I made the lookahead.

So long,

Mega

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

Link to comment
Share on other sites

  • Moderators

Hi Ron,

I thought he needs to know wether Test is in the string and if it is, then he needs the name. He doesn't need all the things/names between the [] that is way I made the lookahead.

So long,

Mega

This is true, but you make a conditional statement after it:

Example of No Search word present:

#include <array.au3>
$sString = "To [John]: Hey" & @CRLF & _
        "From [Bill]: Teit" & @CRLF & _
        "Fro    qwm    [Hugo]      : Teat" & @CRLF & _
        "From [Bill]: what time is it" & @CRLF & _
        "[Jill] logged on" & @CRLF & _
        "To [Bill]: Its 3:17 "
$SearchString = 'Test'
$aArray = StringRegExp($sString, '.*?\[(.*?)\].*?' & $SearchString, 3)
If IsArray($aArray) Then
    _ArrayDisplay($aArray, '')
Else
    MsgBox(16, 'Error', 'No search pattern found')
EndIfoÝ÷ ØLZ^¡ø§¶¬¶¸§«­¢+Ø¥¹±Õ±ÐíÉÉä¹ÔÌÐì(ÀÌØíÍMÑÉ¥¹ôÅÕ½ÐíQ¼m)½¡¹tè!äÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½Ðíɽ´m   ¥±±tèQÍÐÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½Ðíɼ%ÅÝ´m!Õ½tèQÍÐÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½Ðíɽ´m   ¥±±tèÝ¡ÐÑ¥µ¥Ì¥ÐÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½Ðím)¥±±t±½½¸ÅÕ½ÐìµÀì
I1µÀì|($$ÅÕ½ÐíQ¼m  ¥±±tè%ÑÌÌèÄÜÅÕ½Ðì(ÀÌØíMÉ¡MÑÉ¥¹ôÌäíQÍÐÌäì(ÀÌØíÉÉäôMÑÉ¥¹IáÀ ÀÌØíÍMÑÉ¥¹°Ìä츨üÀäÈíl ¸¨ü¤ÀäÈít¸¨üÌäìµÀìÀÌØíMÉ¡MÑÉ¥¹°Ì¤)%%ÍÉÉä ÀÌØíÉÉä¤Q¡¸(%}ÉÉå¥ÍÁ±ä ÀÌØíÉÉä°ÌäìÌäì¤)±Í(%5Í   ½à ÄØ°ÌäíÉɽÈÌäì°Ìäí9¼ÍÉ ÁÑÑɸ½Õ¹Ìäì¤)¹%

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Hi,

ah okay. I see the difference. Mine was a bit to complicated, I guess. I made a condition to check first whether there is a Test an then look between the [].

Nevermind! :whistle:

You do not need to do this \] cause it is no metasign.

#include <array.au3>
$sString = "To [John]: Hey" & @CRLF & _
        "From [Bill]: Test" & @CRLF & _
        "Fro    qwm    [Hugo]      : Test" & @CRLF & _
        "From [Bill]: what time is it" & @CRLF & _
        "[Jill] logged on" & @CRLF & _
        "To [Bill]: Its 3:17 "
$SearchString = 'Test'
$aArray = StringRegExp($sString, '.*?\[(.*?)].*?' & $SearchString, 3)
If IsArray($aArray) Then
    _ArrayDisplay($aArray, '')
Else
    MsgBox(16, 'Error', 'No search pattern found')
EndIf

So long,

Mega

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

Link to comment
Share on other sites

  • Moderators

You do not need to do this \] cause it is no metasign.

Old habits die hard! :whistle:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...