Home > Blockchain >  Powershell, RegEx, separate a text with colon but do not consider drive letter and date/time
Powershell, RegEx, separate a text with colon but do not consider drive letter and date/time

Time:10-10

Regex is a closed book for me, I don't understand it (yet). I am still familiarising myself with the subject. However, I want to finish a script before the end of my contract period.

I have this text:

E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log:107:time=08/10/2021 20:38:24
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log:108:sw!version=3.2.1.0 ()
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log:109:os_text_encoding=cp1250
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log:110:!snapshot?!=unset

I want to separate this, into three parts.

First part: everything before line number 105, e.g. E:\migrationls_da_itm_open_CLASS_Server_20211008_194217_305_session.log

Second part: the line number, e.g. 105

Last part: everything after line number 105, e.g. time=08/10/2021 20:38:24

At first glance I would use the colon as a separator, but I want to keep the drive letter E:\, also the time 20:38:24 would be separated. My second thought was, separate with the line number, so ':105:'. But the line number can vary, one-digit, two-digit, .... and I need the line number itself. Another point, the file extension '.log' can change or not be present.

Can you give me a hint?

CodePudding user response:

This regex might do it:

The regular expression ((.*:\\[^:]*):([0-9]*):(.*)) will look at the (3) items, and match each of them:

  1. (.*:\\[^:]*): this matches your path inlcuding the :\, ending in a :
  2. ([0-9]*): this matches the linenumber, ending in a :
  3. (.*) this matched the rest of the line

CodePudding user response:

Regex you can use (.*?):([0-9] ):(.*)

$text = @'
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log:107:time=08/10/2021 20:38:24
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log:108:sw!version=3.2.1.0 ()
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log:109:os_text_encoding=cp1250
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log:110:!snapshot?!=unset
'@ -split '\r?\n'

$pattern = '(.*?):([0-9] ):(.*)'

$text | Where-Object { $_ -match $pattern } | ForEach-Object {
    [PSCustomObject]@{
        Path   = $Matches[1]
        LineNo = $Matches[2]
        Line   = $Matches[3]
    }
}

Output

Path                                                                     LineNo Line
----                                                                     ------ ----
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log 107    time=08/10/2021 20:38:24
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log 108    sw!version=3.2.1.0 ()   
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log 109    os_text_encoding=cp1250 
E:\migration\ls_da_itm_open_CLASS_Server_20211008_194217_305_session.log 110    !snapshot?!=unset 
  • Related