none
Split String with Powershell RRS feed

  • Question

  • I  am trying to write a script that will read four line of text from a log file:

    File(s) copied                            : 6883
    Directory(ies) created                    : 168
    Amount of copied byte(s)                  : 1 GB (1 847 906 695 Byte(s))
    Estimated copy bitrate                    : 104349.828 KB/s
    Global copy duration                      : 17.294 

    I only need to split the line so it return the data after ': ' I have tried various solutions and I just can't get it. I will then pass the returned data to a variable for inclusion in a csv file but 


    Select-String -Path .\~CopyData_Apps.log -Pattern 'Global copy duration                      : '
    Powershell returns the entire line : "Global copy duration                      : 17.294 "
    Tuesday, February 19, 2019 11:35 AM

Answers

  • Assumed the format of the log file is consistent you could replace the colons with equal signs and use ConvertFrom-StringData to create a hashtable from the log entries like this:

    Get-Content -Path 'path to your log file' | 
        ForEach-Object{
            $_ -replace ':','='
        } |
        ConvertFrom-StringData |
            Where-Object -Property Keys -EQ -Value 'Global copy duration'


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, February 19, 2019 12:18 PM

All replies

  • For each line split on the ":" character. Say

    $Items = $Line -Split ":"

    Then $Items[0] will be the first element of the array, the string you don't want. $Items[1] will be the data to export to your csv.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Tuesday, February 19, 2019 12:00 PM
    Moderator
  • Assumed the format of the log file is consistent you could replace the colons with equal signs and use ConvertFrom-StringData to create a hashtable from the log entries like this:

    Get-Content -Path 'path to your log file' | 
        ForEach-Object{
            $_ -replace ':','='
        } |
        ConvertFrom-StringData |
            Where-Object -Property Keys -EQ -Value 'Global copy duration'


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, February 19, 2019 12:18 PM
  • ... or you could use a little more sophisticated pattern and extract only the match like this:
    Select-String -Path '.\~CopyData_Apps.log' -Pattern '(?<=Global copy duration\s+:\s*)(\S+)' | 
        Select-Object -ExpandProperty Matches | 
            Select-Object -ExpandProperty Value

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, February 19, 2019 12:37 PM
  • Get-Content can read groups of lines.

    Get-Content test1.log -ReadCount 4 -TotalCount 4

    Use this in a pipeline with ConvertFrom-StringData to generate objects in the pipeline.


    \_(ツ)_/


    • Edited by jrv Tuesday, February 19, 2019 12:52 PM
    Tuesday, February 19, 2019 12:51 PM