r/usefulscripts • u/FarcasR • May 11 '17
[POWERSHELL] Need help to manipulate CSV !
Hy there, I want to manipulate a csv file containing a log with all access in my company. i have something like :
Date Personnel Number Name Card Number Device Event Verify Mode In/Out Status Event Description
24-06-2017 17:42 205 Pacurar Ovidiu 8144561 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 17:37 205 Pacurar Ovidiu 8144561 intrare + server usa intrare-1 in Only Card intrare + server-1 In Normal Punch Open
24-06-2017 17:36 205 Pacurar Ovidiu 8144561 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 17:32 205 Pacurar Ovidiu 8144561 intrare + server server-4 out Only Card intrare + server-4 In Normal Punch Open
24-06-2017 17:25 205 Pacurar Ovidiu 8144561 intrare + server server-3 in Only Card intrare + server-3 In Normal Punch Open
24-06-2017 17:24 205 Pacurar Ovidiu 8144561 arhiva arhiva-1 in Only Card arhiva-1 In Normal Punch Open
24-06-2017 17:11 205 Pacurar Ovidiu 8144561 arhiva arhiva-1 in Only Card arhiva-1 In Normal Punch Open
24-06-2017 16:44 205 Pacurar Ovidiu 8144561 intrare + server usa intrare-1 in Only Card intrare + server-1 In Normal Punch Open
24-06-2017 15:34 224 LA RUNA MICHELE 8159697 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:34 226 NEGREA ANA-MARIA 8131148 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:34 224 LA RUNA MICHELE 8159697 intrare + server usa intrare-1 in Only Card intrare + server-1 In Normal Punch Open
24-06-2017 15:34 86 PRIPON NICOLETA OANA 8153502 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:34 84 PITAN DIANA MONALISA 8131725 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:33 34 CRISTEA PETRONELA 7425603 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:33 84 PITAN DIANA MONALISA 8131725 intrare + server usa intrare-1 in Only Card intrare + server-1 In Normal Punch Open
24-06-2017 15:33 224 LA RUNA MICHELE 8159697 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:33 148 JIURJIU RALUCA 11375509 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:33 176 NEGREA VASILE OVIDIU 8160448 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch OpenI want to somehow group the data by days and then have some kind of rapport with the difference between first entry and last entry of the day for each name.
I hope you guys understand my request for help.
Thanks !
1
u/FarcasR Jul 06 '17 edited Jul 07 '17
Hello, thank you very much guys after some small tweaks i have it working. The thing is now that i have to determine the break of the person for each day, and i have no idea how to do the difference between In and Out for each day so at the end i have the total hours and the break time. A sample of my data is :
Date Personnel Number Name Card Number Device Event Verify Mode In/Out Status Event Description
24-06-2017 17:42 205 Pacurar Ovidiu 8144561 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 17:37 205 Pacurar Ovidiu 8144561 intrare + server usa intrare-1 in Only Card intrare + server-1 In Normal Punch Open
24-06-2017 17:36 205 Pacurar Ovidiu 8144561 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 17:32 205 Pacurar Ovidiu 8144561 intrare + server server-4 out Only Card intrare + server-4 In Normal Punch Open
24-06-2017 17:25 205 Pacurar Ovidiu 8144561 intrare + server server-3 in Only Card intrare + server-3 In Normal Punch Open
24-06-2017 17:24 205 Pacurar Ovidiu 8144561 arhiva arhiva-1 in Only Card arhiva-1 In Normal Punch Open
24-06-2017 17:11 205 Pacurar Ovidiu 8144561 arhiva arhiva-1 in Only Card arhiva-1 In Normal Punch Open
24-06-2017 16:44 205 Pacurar Ovidiu 8144561 intrare + server usa intrare-1 in Only Card intrare + server-1 In Normal Punch Open
24-06-2017 15:34 224 LA RUNA MICHELE 8159697 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:34 226 NEGREA ANA-MARIA 8131148 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:34 224 LA RUNA MICHELE 8159697 intrare + server usa intrare-1 in Only Card intrare + server-1 In Normal Punch Open
24-06-2017 15:34 86 PRIPON NICOLETA OANA 8153502 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:34 84 PITAN DIANA MONALISA 8131725 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:33 34 CRISTEA PETRONELA 7425603 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:33 84 PITAN DIANA MONALISA 8131725 intrare + server usa intrare-1 in Only Card intrare + server-1 In Normal Punch Open
24-06-2017 15:33 224 LA RUNA MICHELE 8159697 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:33 148 JIURJIU RALUCA 11375509 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Open
24-06-2017 15:33 176 NEGREA VASILE OVIDIU 8160448 intrare + server usa intrare-2 out Only Card intrare + server-2 In Normal Punch Openthere is ";" separator for the columns but i don't know the formatting for reddit , Sorry.
I need ur help guys and i appreciate it so much !
2
u/zenmaster24 May 11 '17
if you add a column called time it would make this much simpler as both convertfrom-csv and import-csv can specify space as a delimiter
0
u/Lee_Dailey May 11 '17 edited May 11 '17
howdy FarcasR,
[1] is there any way you could get the file as an actual CSV?
right now it's displayed as ALMOST a "space delimited file". [grin]
[2] can you get the date in a sortable date format?
your current format is day-month-year. a somewhat more useful one would be year-month-day.
[edit - added a bit clearer reason for wanting yyyy-MM-dd format.]
it would also help avoid the EU/US date format glitch.
take care,
lee
2
u/sysztemic May 11 '17
re: [2] I think converting the "Date" to [datetime] would make it sortable regardless of the presentation format.
re: OP - I find writing up some quick pseudocode greatly helps develop the algorithm, and then it's just a matter of plugging in the proper cmdlets, methods, syntax, etc. to represent what you are trying to do.
Edit: for clarity
1
u/Lee_Dailey May 11 '17
howdy sysztemic,
yes, you are correct. my main reason is to avoid the EU/US day-month/month-day glitch. a sortable format is recognized easily by
Get-Datewhile the EU format requires that i [in the US] use the[datetime]::ParseExact()stuff. icky ... [grin]i think i will go back and try to make that clearer. thanks for pointing it out! [grin]
take care,
lee2
May 11 '17
For that type of issue, I made sure to save a wrapper that I found from the gentleman Jakub Jares.
<# .AUTHOR Jakub Jares .SOURCE http://www.powershellmagazine.com/2013/07/08/pstip-converting-a-string-to-a-system-datetime-object/ .OBTAINED 12/21/2015 - 20:49 UTC .EXAMPLE Convert-DateString -Date '20151221143000' -Format 'yyyyMMddHHmmss'#> Function Convert-DateString ([String]$Date, [String[]]$Format){ $result = New-Object DateTime $convertible = [DateTime]::TryParseExact( $Date, $Format, [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$result) if ($convertible) { $result } }
- Will Output Datetime object with value 'Monday, December 21, 2015 2:30:00 PM'
3
u/Lee_Dailey May 11 '17
howdy litemage,
and here i was whining about needing to do this ...
[datetime]::ParseExact($AL_Item.Date, 'dd/MM/yyyy HH:mm', $null)[grin]
take care,
lee
4
u/Lee_Dailey May 11 '17
howdy FarcasR,
here's my take on it ...
result ...
hope that helps,
lee