labase
labase

Reputation: 3

How to extract a string from an array powershell

I need to extract a string from the following $parse.

 $parse = select-string -path .\xxx.log "Error" -allmatches –simplematch -context 1 

Example of string contain:

 Start : Error : billing 1150116682 not found - exit. Source : /mnt/xxx/roo/foo/aaa/115565841_yyyyy.pdf
=================================================================================================================================================== 

I need extract only Source : /mnt/xxx/roo/foo/aaa/115565841_yyyyy

how can I do it ?

thx

Upvotes: 0

Views: 5214

Answers (2)

Bill_Stewart
Bill_Stewart

Reputation: 24525

Don't use -SimpleMatch because that prevents use of a regular expression, which we can use to extract the needed substring. Here is an example:

$s = "Start : Error : billing 1150116682 not found - exit. Source : /mnt/xxx/roo/foo/aaa/115565841_yyyyy.pdf"
$s | Select-String "Error : .* Source : (.*)" | ForEach-Object {
  $_.Matches[0].Groups[1].Value -replace '\.pdf$', ''
}

The above outputs the string /mnt/xxx/roo/foo/aaa/115565841_yyyyy.

Upvotes: 1

Prientus
Prientus

Reputation: 733

You can do a string split like this:

$array = $parse.split(':') $lastItem = $array[-1]

And then get the item you want from the list. Assuming you know where what you need is.

Upvotes: 0

Related Questions