Get-Content file*.txt|
? { $_ -match '^(?<date>\d\d/\d\d/\d\d\d\d) (?<name>\b\D+?\b) .* (?<action>started|stopped) (?:after|in) (?<time>\d+) (?<units>.*)' } |
% {
$matches.Remove(0)
New-Object PsObject -Property $matches
}
It generates the object based on the match groups found (and also goes thru all files matching the file pattern).
|
group-object Name, action, units |
% {
$Name = $_.Name
$_.Group | measure-object -average -sum time | select @{n='Group'; e={$Name}}, Average, Sum, Count
}
That is, of course, a very simplified report.
I will have 200 objects with the properties of date , time etc ?Yes.
will they all be in one file ?That depends on additional code. What I posted above only submits the objects to the pipeline, and by default will be sent to your screen. If you wanted it sent to a file, then you add a command that will write to a file. Typically you'd use Export-CSV, so that later you could read the file back in and do additional processing. And whether you create one file or two hundred is also up to you - it all depends on what you need.
$x = @"
...
"@ -split "`n"
You forgot the match against the space between date and time portion.08/14/2013 08:18 AM - DRIVE W: Create Profile Index - Finished in 7 secs
I'm not clear whether you want to have "Name" being "Create", "Profile" or both. As-is, you capture "Create Profile" (or, more precise, anything between the drive and "Index").$regex = "^(?<date>(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d) (?:\d\d:\d\d (AM|PM)) - DRIVE (?<drive>\w): (?<name>\w+ \w+) Index - Finished \w+ (?<duration>\d+) (?<unit>secs|mins|hours)"
Using spaces instead of \s makes it more restrictive and fragile, but much better to read and match manually.
# Only for test:
$x = @"
08/14/2013 08:17 AM - DRIVE W: Create Profile Index - Started on
08/14/2013 08:18 AM - DRIVE W: Folders: 159 08/14/2013 08:18 AM - DRIVE W: Profiles Indexed: 574
08/14/2013 08:18 AM - DRIVE W: Profiles Removed: 0
08/14/2013 08:18 AM - DRIVE W: Create Profile Index - Finished in 7 secs
08/14/2013 08:18 AM - DRIVE W: Create Text Index - Started on
08:18 AM - DRIVE W: = 499 Files in database 08/14/2013
08:18 AM - DRIVE W: - 0 Ignored per rules 08/14/2013
08:18 AM - DRIVE W: - 0 Ignored per format 08/14/2013
08:18 AM - DRIVE W: - 0 Skipped bad records 08/14/2013
08:18 AM - DRIVE W: = 0 Files qualified 08/14/2013
08:18 AM - DRIVE W: = 499 Needed indexing 08/14/2013
08:18 AM - DRIVE W: - 0 Skipped old BADFILEs 08/14/2013
08:18 AM - DRIVE W: - 0 Skipped new BADFILEs 08/14/2013
08:18 AM - DRIVE W: - 0 Skipped open errors 08/14/2013
08:18 AM - DRIVE W: - 0 Skipped other fails 08/14/2013
08:18 AM - DRIVE W: + 376 Indexed actual files (42,818,863 bytes)
08/14/2013 08:18 AM - DRIVE W: + 123 Indexed cached copies (143,293,346 bytes)
08/14/2013 08:18 AM - DRIVE W: = 499 Indexed successfully (186,112,209 bytes)
08/14/2013 08:18 AM - DRIVE W: Create Text Index - Finished in 17 secs
"@ -split "`n"
$regex = "^(?<date>(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d) (?:\d\d:\d\d (AM|PM)) - DRIVE (?<drive>\w): (?<name>\w+ \w+) Index - Finished \w+ (?<duration>\d+) (?<unit>secs|mins|hours)"
$x | # instead of Get-Content file*.txt |
? { $_ -match $regex } |
% {
$matches.Remove(0)
New-Object PsObject -Property $matches
} |
group-object Drive, Name, units |
% {
$Name = $_.Name
$_.Group | measure-object -average -sum duration | select @{n='Group'; e={$Name}}, Average, Sum, Count
}
Open in new window
You said you have the regex pattern, so feel free to post it (set it as $pattern). My example above assumes named captures - you can adjust accordingly. It doesn't store things in an array, but creates an object with the appropriate properties. You can store the entire output of objects as an array if you wish (just add $array = at the very beginning), or continue processing with the pipeline.