Home > Enterprise >  Find pattern in file powershell
Find pattern in file powershell

Time:12-03

I have this pattern thats composed by a quantity, a description and a price. Please not that the numbers and text can change everytime.

I need to use powershell to find and save the results. So far I got this... the pattern format is what is driving me crazy.

$example_line = '3.00 CEBICHE CORVINA PI   26,805.00'
$pattern= '\d.\d\d \D'
$results = $example_line | Select-String $pattern -AllMatches
$results.Matches.Value

Any help is appreciated.

Thanks in advance !

CodePudding user response:

The proper pattern is

$pattern= '\d.\d\d \D* \d*,\d*.\d*'

CodePudding user response:

Code:

$example_line = '3.00 CEBICHE CORVINA PI   26,805.00'

# Expression Pattern
$pattern = '(\d\.\d\d) (\D ?) (\d ,\d\d\d\.\d\d)'

# Use the -match operator to match the string against the pattern
$match = $example_line -match $pattern

# If the match is successful, extract the three captured groups
if ($match) {
    $quantity = $matches[1]
    $description = $matches[2]
    $price = $matches[3]

    # Print the extracted values
    Write-Output "Quantity: $quantity"
    Write-Output "Description: $description"
    Write-Output "Price: $price"
}
  • Related