Reputation: 590
Have a document as shown below:
7/30/2015 9:15:24 PM
7/30/2015 9:18:45 PM
7/31/2015 7:13:26 AM
7/31/2015 9:22:46 PM
8/01/2015 9:26:07 AM
This is a dynamically created file. As you can see it's a mixture of AM and PM, a single date either can have so many entries or just one entry. What I need to do is that need to find how many entries are there for one date and find the recent one for each date based on time and write those into a file, say date.txt.
I tried Measure-Latest but it is getting me the one which is latest among all the entries. Can anyone please help me on this.? Any help would be really appreciated.
Upvotes: 1
Views: 81
Reputation: 58931
I would use the static ParseExact Datetime method to parse the date and store them in an array:
$entries = @(
'7/30/2015 9:15:24 PM',
'7/30/2015 9:18:45 PM',
'7/31/2015 7:13:26 AM',
'7/31/2015 9:22:46 PM',
'8/01/2015 9:26:07 AM'
)
$usCulture = New-Object system.globalization.cultureinfo("en-US")
$parseFormat = 'M/dd/yyyy h:mm:ss tt'
$dates = $entries | % {[Datetime]::ParseExact($_, $parseFormat , $usCulture)}
The output of $dates
is now:
PS D:\> $dates
Donnerstag, 30. Juli 2015 21:15:24
Donnerstag, 30. Juli 2015 21:18:45
Freitag, 31. Juli 2015 07:13:26
Freitag, 31. Juli 2015 21:22:46
Samstag, 1. August 2015 09:26:07
Now you can use group
and sort
to get your desired output. For example, how many entries for each date:
PS D:\> $dates | group { $_.ToShortDateString() } -NoElement
Count Name
----- ----
2 30.07.2015
2 31.07.2015
1 01.08.2015
And the recent one for each date:
$recentDates = $dates | sort { $_.Ticks } -Descending | group { $_.ToShortDateString() } | % {$_.Group | select -first 1}
$entries | where { [Datetime]::ParseExact($_, $parseFormat , $usCulture) -in $recentDates}
Output:
7/30/2015 9:18:45 PM
7/31/2015 9:22:46 PM
8/01/2015 9:26:07 AM
Upvotes: 2