locked
How to extract values between delimiters in a multi-line textfile using PowerShell RRS feed

  • Question

  • I have a large text file written to an array wherein each line contains a part number I am trying to extract.

    The part number can always be found between the second and third colon in each line, simplified example below:

    -start-
    [0]www.amazon.com:Jan-4-2020:PN-1234:connector:US
    [1]www.dell.com:Jan-4-2020:PN-1735:cable:US:UK
    [2]www.blackbox.com:Jan-4-2020:PN-0874:harness:US:UK:CA
    [3]www.samsung.com:Jan-4-2020:PN-9930:connector:JN:CN
    ...
    [2047]www.phillips.com:Jan-4-2020:PN-1239:cable:US:UK:JN:CA
    -end-

    My attempted code that failed: 

     foreach ($item in $arrayParts) 
    { $partNum = "{0}:{1}:" -f $str.Split(':') }

    Any help appreciated, thanks. JB


    Tuesday, January 5, 2021 9:08 PM