Powershell string regular expression help

I have a powershell script that loops through a folder and takes the last line of about 12 log files. The last line of each file is basically similar to the one below however the end where it says X were rejected might be different.  

[Wed Oct 02 08:41:53 EDT 2013]Planning Outline data store load process finished. 2 data records were read, 2 data records were processed, 2 were accepted for loading (verify actual load with Essbase log files), 0 were rejected.

What I am looking for is some way in powershell to somehow seperate only the last part where it says X were rejected. and either store it to a variable or write it to a text file.

Thanks
LVL 35
Joseph DalyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
If you get the last line into a variable, we match it against a pattern.  I'm think to match any letters, numbers, or spaces between the last comma and the period at the end of the line.  Then we make use of the $matches automatic variable.  Here's an example.
$lastline = "[Wed Oct 02 08:41:53 EDT 2013]Planning Outline data store load process finished. 2 data records were read, 2 data records were processed, 2 were accepted for loading (verify actual load with Essbase log files), 0 were rejected."
$lastline -match ", ([\w ]+)\."
$matches[1]

Open in new window

0
Joseph DalyAuthor Commented:
Ok so that looks like it may have worked. I have two hopefully simple questions for you.

1. Can you explain what your match regexp does so I can hopefully understand the processl.

2. I added your lines to a simple script which seems to work and exports the text as i expect however for each object it returns true in the powershell window.

powershell window
My script is below.

$string = get-content $fullpath | select-object -last 1
$string -match ", ([\w ]+)\."
$string = $matches[1]
"$name">> $filename
"-------------------------------------------------" >> $filename
"$string" >> $filename
"" >> $filename
}

Open in new window

0
footechCommented:
The regex pattern ", ([\w ]+)\." is looking for:
,  - a comma
 - a space
() - creates a capturing group
[] - match any of the characters inside
\w - shorthand character class which matches any alphanumeric character
+ - repeated one or more times
\. - a period

So the entire pattern would match ", 0 were rejected.", but the capturing group would just be "0 were rejected".

The output from a using the -match operator will be $true or $false depending on whether a match was found or not.  If you don't want any output you can either pipe it to Out-Null or assign it to a variable.
($string -match ", ([\w ]+)\.") | Out-Null

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joseph DalyAuthor Commented:
Thanks Im going to have to start brushing up on my regular expressions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.