locked
Split('.') or alternative way to parse text lines into objects RRS feed

  • Question

  • Hi,

    Firstly, im taking some text from log file.

    Add-Member -InputObject $Information -MemberType NoteProperty -Name PXEcontent -Value ($LastLine.Replace( '"',' ' )).Substring(158,36)
    Getting result from variable:

    PS C:\WINDOWS\system32> ($LastLine.Replace( '"',' ' )).Substring(158,36)
     08:55:24.686-120  date= 10-16-2017

    Then im spliting it with soplit and convertfrom-csv command:

    $Information.PXEcontent.Split('.') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "Date"
    
    

    Results:

    S C:\WINDOWS\system32> $Information.PXEcontent.Split('.') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "Date"

    Notrequired  Time     Notrequired2   Date     
    ----------- ----     ------------ ----     
                     08:55:24                       
    686-120                    date=        10-16-2017

    But why on these objects are some spaces? Should i use another command to split it? If i would like to get all information in one line like this:

    Monday, October 16, 2017 8:05 AM

Answers

  • When parsing text I recommend you look into regular expressions. Sure, there's a learning curve but the end result is much cleaner (no need for those pesky NotRequired properties). Also, since you're clearly trying to add a date to your properties, why not make it an actual datetime property? Consider this example:

    $LogLine = 'time= 08:55:24.686-120  date= 10-16-2017'
    If ($LogLine -match 'time= (?<Time>.{8}).+date= (?<Date>.{10})')
    {
       $LogDate = $Matches['Time','Date'] -join ' '
       Add-Member -InputObject $Information -MemberType NoteProperty -Name PXEDate -Value (Get-Date $LogDate)
    }

    The first line represents a portion of the line from the PXE log file. If the line matches the regular expression the Time and Date are joined by a space and added as a property of type DateTime by utilizing the Get-Date command (this will only work if your culture setting align with the date format in the text).


    MicaH

    itmicah.wordpress.com


    • Edited by MicaH_Z Monday, October 16, 2017 9:22 AM
    • Marked as answer by TechSpark0 Monday, October 16, 2017 9:59 AM
    Monday, October 16, 2017 9:21 AM

All replies

  • Hi,

    Firstly, im taking some text from log file.

    Add-Member -InputObject $Information -MemberType NoteProperty -Name PXEcontent -Value ($LastLine.Replace( '"',' ' )).Substring(158,36)
    Getting result from variable:

    PS C:\WINDOWS\system32> ($LastLine.Replace( '"',' ' )).Substring(158,36)
     08:55:24.686-120  date= 10-16-2017

    Then im spliting it with soplit and convertfrom-csv command:

    $Information.PXEcontent.Split('.') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "Date"
    

    Results:

    S C:\WINDOWS\system32> $Information.PXEcontent.Split('.') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "Date"

    Notrequired  Time     Notrequired2   Date     
    ----------- ----     ------------ ----     
                     08:55:24                       
    686-120                    date=        10-16-2017

    But why on these objects are some spaces? Should i use another command to split it? If i would like to get all information in one line like this:

    Im sorry, found solution with replace command by myself...

    :)

    PS C:\WINDOWS\system32> $A = $Information.PXEcontent.Replace('.', ' ') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "sh", "sh2", "Date"
    PS C:\WINDOWS\system32> $A

    Notrequired  :
    Time         : 08:55:24
    Notrequired2 : 686-120
    sh           :
    sh2          : date=
    Date         : 10-16-2017

    Monday, October 16, 2017 8:09 AM
  • When parsing text I recommend you look into regular expressions. Sure, there's a learning curve but the end result is much cleaner (no need for those pesky NotRequired properties). Also, since you're clearly trying to add a date to your properties, why not make it an actual datetime property? Consider this example:

    $LogLine = 'time= 08:55:24.686-120  date= 10-16-2017'
    If ($LogLine -match 'time= (?<Time>.{8}).+date= (?<Date>.{10})')
    {
       $LogDate = $Matches['Time','Date'] -join ' '
       Add-Member -InputObject $Information -MemberType NoteProperty -Name PXEDate -Value (Get-Date $LogDate)
    }

    The first line represents a portion of the line from the PXE log file. If the line matches the regular expression the Time and Date are joined by a space and added as a property of type DateTime by utilizing the Get-Date command (this will only work if your culture setting align with the date format in the text).


    MicaH

    itmicah.wordpress.com


    • Edited by MicaH_Z Monday, October 16, 2017 9:22 AM
    • Marked as answer by TechSpark0 Monday, October 16, 2017 9:59 AM
    Monday, October 16, 2017 9:21 AM