TRIM SPLIT Powershell cmdlet

Hi,

I have a csv file where for each of the rows, i need to extract only the last few words of the text.

input string

,Unrecoverable Fault : 006 - Duplicate Transaction - UFM ID : 13715030397201 UFMErrorCode : 3003 

Expected output string

UFM ID : 13715030397201 UFMErrorCode : 3003 

I need to extract all the info which starts with UFM ID, Any ideas, how this can be done. I need to do for each and every row in the csv file.

Thanks,

September 1st, 2015 9:19pm

Split on the - character and take the last element.
Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 10:31pm

Hi,

well, this sounds to me like a fine day for regular expressions :)

Let's see ...

# A Small Demo to paste to console:
$s = ",Unrecoverable Fault : 006 - Duplicate Transaction - UFM ID : 13715030397201 UFMErrorCode : 3003"
$s | Select-String 'UFM ID : [\w\W]*' -AllMatches | Select -Expand Matches | Select -Expand Value

# Doing it with an entire file:
Get-Content "C:\temp\example.txt" | | Select-String 'UFM ID : [\w\W]*' -AllMatches | Select -Expand Matches | Select -Expand Value

Cheers,
Fred

September 2nd, 2015 3:11am

This works for me

$d = 'UFM ID'    #  the text to delimit the extracted portion of the string stored in $s 
$s = ',Unrecoverable Fault : 006 - Duplicate Transaction - UFM ID : 13715030397201 UFMErrorCode : 3003'
#
# split $s into 2 parts left and right of $d and then return $d with the rightmost part appended to it 
$d + $($s -split $d,2)[1] 

  • Proposed as answer by LarryWeiss 6 hours 25 minutes ago
Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 6:45am

One way:

$text = ',Unrecoverable Fault : 006 - Duplicate Transaction - UFM ID : 13715030397201 UFMErrorCode : 3003 '

$text -replace '.+(UFM ID :.+)','$1'

UFM ID : 13715030397201 UFMErrorCode : 3003 

September 2nd, 2015 7:02am

This works for me

$d = 'UFM ID'    #  the text to delimit the extracted portion of the string stored in $s 
$s = ',Unrecoverable Fault : 006 - Duplicate Transaction - UFM ID : 13715030397201 UFMErrorCode : 3003'
#
# split $s into 2 parts left and right of $d and then return $d with the rightmost part appended to it 
$d + $($s -split $d,2)[1] 

  • Proposed as answer by LarryWeiss Thursday, September 03, 2015 12:45 AM
Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 10:44am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics