Jump to content

Extracts the left of the comma-terminated string to the right of the specified string in the file


i2i8
 Share

Recommended Posts

I have a file:

PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] Exit C_GetAttributeValue(0x0000) 
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] Enter C_GetAttributeValue  hSession:0x19801d30,  hObject:0x198010b0
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] {
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258]   pTemplate[0].type:0x00000102,  pTemplate[0].ulValueLen:0x00000014, pTemplate[0].pValue:[095012F0EC7DDFC6BF80B4736986FD1DEF81B1DD]
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] }
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] Exit C_GetAttributeValue(0x0000) 
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] Enter C_FindObjects  hSession:0x19801d30, ulMaxObjectCount:0x0001
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] Exit C_FindObjects(0x0000) *pulObjectCount:0x00000000
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] Enter C_FindObjectsFinal  hSession:0x19801d30 
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] Exit C_FindObjectsFinal(0x0000) 
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:258] Enter C_Login  hSession:0x19801d30, userType:0x0001 PIN:11111111, ulPinLen=0x8 
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] Exit C_Login(0x0000)  
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] Enter C_FindObjectsInit  hSession:0x19801d30, ulCount:0x0001
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] {
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274]   pTemplate[0].type:0x00000000,  pTemplate[0].ulValueLen:0x00000004, pTemplate[0].pValue:[03000000]
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] }
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] Exit C_FindObjectsInit(0x0000) 
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] Enter C_FindObjects  hSession:0x19801d30, ulMaxObjectCount:0x0001
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] Exit C_FindObjects(0x0000) *pulObjectCount:0x00000001
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] Enter C_GetAttributeValue  hSession:0x19801d30,  hObject:0x197fa440
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] {
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274]   pTemplate[0].type:0x00000100,  pTemplate[0].ulValueLen:0x00000004, pTemplate[0].pValue:[00000000]
PID=0x15ac, TID=0x070c[2021-04-11 17:54:53:274] }

I want to get: userType:0x0001 PIN:11111111, in this line, PIN: after the content,PIN: The length of the right character is not determined, but ends with a comma

Another way to describe it is this:

userType:0x0001 PIN:Content,

I want to get Content between PIN: and ,

I really hope you can help me, thank you very much

Link to comment
Share on other sites

10 minutes ago, Danp2 said:

Do you know if the desired string can appear in the file more than once? I imagine you'll get a bunch of regex recommendations. In the mean time, take a look at _StringBetween in the help file.

Only once.

Link to comment
Share on other sites

Link to comment
Share on other sites

7 minutes ago, Nine said:
#include <Constants.au3>

Opt( "MustDeclareVars", 1 )

Local $sContent = StringRegExp(FileRead("Test.txt"), "PIN:([^,]*)", 1)[0]
MsgBox ($MB_SYSTEMMODAL, "", $sContent)

try this.  I do not know if userType... before is important or not.

It's amazing that you successfully solved my problem. Thank you again. Thank you.
In addition, I also used my own way to solve, but it seems that the code is a little bit not concise, could you help me to perfect it?

; Open the file for reading and store the handle to a variable.
Local $hFileOpen = FileOpen($CSP_Log, $FO_READ)
Local $searchw = "userType"
Local $iLine = 0, $sLine = '', $oMatchContentLine
If $hFileOpen = -1 Then 
    MsgBox(0,'ERROR','Unable to open file for reading.')
    Exit 1
EndIf
; find the line that has the search string
While 1
    $iLine += 1
    $sLine = FileReadLine($hFileOpen)
    If @error = -1 Then ExitLoop
    If StringInStr($sLine, $searchw) Then    
        For $i = $iLine To $iLine
            $oMatchContentLine = FileReadLine($hFileOpen, $i)
            local $test = _StringBetween ($oMatchContentLine, 'userType:0x0001 PIN:', ',')
            _ArrayDisplay($test, "Default Search")
        Next
        ExitLoop
    EndIf
WEnd
    FileClose($hFileOpen)

 

Link to comment
Share on other sites

56 minutes ago, Nine said:
#include <Constants.au3>

Opt( "MustDeclareVars", 1 )

Local $sContent = StringRegExp(FileRead("Test.txt"), "PIN:([^,]*)", 1)[0]
MsgBox ($MB_SYSTEMMODAL, "", $sContent)

try this.  I do not know if userType... before is important or not.

If the contents of the regex appear multiple times, and each time the contents are returned differently, how do I extract the contents and write them into Notepad separately?

Link to comment
Share on other sites

29 minutes ago, i2i8 said:

If the contents of the regex appear multiple times, and each time the contents are returned differently, how do I extract the contents and write them into Notepad separately?

Do you mean the PIN:content appears multiple times in the same file ? (you said earlier only once) 

Or you have multiple files to read and each time, append the single content into a text file ?

Please explain exactly what you want to achieve.

Link to comment
Share on other sites

1 hour ago, Nine said:

Do you mean the PIN:content appears multiple times in the same file ? (you said earlier only once) 

Or you have multiple files to read and each time, append the single content into a text file ?

Please explain exactly what you want to achieve.

I have just observed the contents of the file, and both of the two situations you mentioned exist:
The first one, PIN: the same content between and, but it appears multiple times, so I only need once;
The second, PIN: the content between and is not the same, different content appears many times, then, I need to read out each different content, respectively write a backupinfo.txt.
BackupInfo.txt rendering format, such as:
PIN 1:11111111
PIN 2:22222222
PIN 3:33333333

Link to comment
Share on other sites

CSP_Log.log like this:

PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_Login  hSession:0x19281d30, userType:0x0001 PIN:11111111, ulPinLen=0x8 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_Login(0x0000)  
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_FindObjectsInit  hSession:0x19281d30, ulCount:0x0001
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] {
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782]   pTemplate[0].type:0x00000000,  pTemplate[0].ulValueLen:0x00000004, pTemplate[0].pValue:[03000000]
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] }
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_FindObjectsInit(0x0000) 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_Login  hSession:0x19281d30, userType:0x0001 PIN:11111111, ulPinLen=0x8 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_Login(0x0000)  
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_FindObjectsInit  hSession:0x19281d30, ulCount:0x0001
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] {
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782]   pTemplate[0].type:0x00000000,  pTemplate[0].ulValueLen:0x00000004, pTemplate[0].pValue:[03000000]
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] }
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_FindObjectsInit(0x0000) 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_Login  hSession:0x19281d30, userType:0x0001 PIN:2222222, ulPinLen=0x8 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_Login(0x0000)  
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_FindObjectsInit  hSession:0x19281d30, ulCount:0x0001
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] {
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782]   pTemplate[0].type:0x00000000,  pTemplate[0].ulValueLen:0x00000004, pTemplate[0].pValue:[03000000]
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] }
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_FindObjectsInit(0x0000) 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_Login  hSession:0x19281d30, userType:0x0001 PIN:33333333, ulPinLen=0x8 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_Login(0x0000)  
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_FindObjectsInit  hSession:0x19281d30, ulCount:0x0001
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] {
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782]   pTemplate[0].type:0x00000000,  pTemplate[0].ulValueLen:0x00000004, pTemplate[0].pValue:[03000000]
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] }
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_FindObjectsInit(0x0000) 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_Login  hSession:0x19281d30, userType:0x0001 PIN:33333333, ulPinLen=0x8 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_Login(0x0000)  
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_FindObjectsInit  hSession:0x19281d30, ulCount:0x0001
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] {
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782]   pTemplate[0].type:0x00000000,  pTemplate[0].ulValueLen:0x00000004, pTemplate[0].pValue:[03000000]
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] }
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_FindObjectsInit(0x0000) 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_Login  hSession:0x19281d30, userType:0x0001 PIN:44444444, ulPinLen=0x8 
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_Login(0x0000)  
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Enter C_FindObjectsInit  hSession:0x19281d30, ulCount:0x0001
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] {
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782]   pTemplate[0].type:0x00000000,  pTemplate[0].ulValueLen:0x00000004, pTemplate[0].pValue:[03000000]
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] }
PID=0x170c, TID=0x1760[2021-04-11 21:31:15:782] Exit C_FindObjectsInit(0x0000)

The result I want to achieve is:

Backupinfo.txt

PIN 1:11111111
PIN 2:22222222
PIN 3:33333333
PIN 4:44444444

 

Link to comment
Share on other sites

Give this a try --

#include <Array.au3>

Opt( "MustDeclareVars", 1 )

Local $aPins = StringRegExp(FileRead("Test.txt"), "PIN:([^,]*)", $STR_REGEXPARRAYGLOBALMATCH)

_ArrayDisplay($aPins) ; Duplicates present

$aPins = _ArrayUnique($aPins, Default, Default, Default, $ARRAYUNIQUE_NOCOUNT)
_ArrayDisplay($aPins) ; Duplicates removed

 

Link to comment
Share on other sites

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

Opt( "MustDeclareVars", 1 )

Local $aContent = StringRegExp(FileRead("Test.txt"), "(?s)PIN:(\w+\b)(?!.+PIN:\1\b)", 3)
;_ArrayDisplay($aContent)
_FileWriteFromArray("Backupinfo.txt", $aContent, Default, Default, @CRLF)

That will also write the contents into the text file...

If PIN:content is only composed of digits, you can replace \w with \d (if needed).

Edited by Nine
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...