英文:
measure/count logins & file transfers from csv data
问题
我有类似以下的数据;
username method ip uri-stem
ABC\bob1 RETR 192.45.00.00 file.txt
- USER 10.00.00.01 bob2
ABC\bob2 STOR 10.00.00.01 file1234.txt
- USER 192.45.00.00 bob1
ABC\bob1 RETR 192.45.00.00 file2.txt
并且我需要它看起来像这样
user Ip loginCount FilesIn FilesOut
bob1 192.45.00.00 5 3 3
bob2 00.00.00.00 3 2 2
英文:
I have data that looks like this;
username method ip uri-stem
ABC\bob1 RETR 192.45.00.00 file.txt
- USER 10.00.00.01 bob2
ABC\bob2 STOR 10.00.00.01 file1234.txt
- USER 192.45.00.00 bob1
ABC\bob1 RETR 192.45.00.00 file2.txt
& I need it to look like this
user Ip loginCount FilesIn FilesOut
bob1 192.45.00.00 5 3 3
bob2 00.00.00.00 3 2 2
#Key
RETR = FilesOut
STOR = FilesIn
USER = loginCount
This is what I have so far for formatting into the first bit of data
$inputFile = ".\u_ex230201.log"
$inputHeaders = "date","time","c-ip","c-port","cs-username","s-sitename","s-computername","cs-host","s-ip","s-port","cs-method","cs-uri-stem","sc-status","sc-win32-status","sc-substatus","sc-bytes","cs-bytes","time-taken","x-session","x-fullpath","x-debug"
$codes = "USER","RETR","STOR"
Import-Csv $inputfile -Header $inputHeaders -Delimiter ' ' |
ForEach-Object {
$code = $_.'cs-method'
if ($codes -contains $code){
[PSCustomObject]@{
username = $_.'cs-username'
method = $_.'cs-method'
ip = $_.'c-ip'
uri-stem = $_.'cs-uri-stem'
Results = [int]$_.Results
}
}
} | export-csv .\export
答案1
得分: 0
以下是您要翻译的内容:
You want a pivot table. Try this
<!-- begin snippet: js hide: false console: true babel: false -->
$csv = @"
username method ip uri-stem
ABC\bob1 RETR 192.45.00.00 file.txt
- USER 10.00.00.01 bob2
ABC\bob2 STOR 10.00.00.01 file1234.txt
- USER 192.45.00.00 bob1
ABC\bob1 RETR 192.45.00.00 file2.txt
"@
#remove spaces at beginning of line
$csv = $csv -replace ".* -","-"
#replace spaces with comma
$csv = $csv -replace " +",","
$table = $csv | ConvertFrom-Csv
foreach($row in $table)
{
$row.username = $row.username -replace "^.*\\",""
if($row.username -eq "-") { $row.username = $row.'uri-stem' }
}
#$table
$groups = $table | Group-Object -Property username
$pivotTable = [System.Collections.ArrayList]::new()
foreach($row in @($groups))
{
$newRow = New-Object -TypeName psobject
$newRow | Add-Member -NotePropertyName user -NotePropertyValue $row.Name
$firstGroup = $row.Group | Select-Object -first 1
$newRow | Add-Member -NotePropertyName IP -NotePropertyValue $firstGroup.IP
$FilesOut = @($row.Group | Where-Object {$_.method -eq "RETR"})
$newRow | Add-Member -NotePropertyName FilesOut -NotePropertyValue $FilesOut.Count
$FilesIn = @($row.Group | Where-Object {$_.method -eq "STOR"})
$newRow | Add-Member -NotePropertyName FilesIn -NotePropertyValue $FilesIn.Count
$loginCount = @($row.Group | Where-Object {$_.method -eq "USER"})
$newRow | Add-Member -NotePropertyName loginCount -NotePropertyValue $loginCount.Count
$pivotTable.Add($newRow) | Out-Null
}
$pivotTable | Format-Table
<!-- end snippet -->
Output
<!-- begin snippet: js hide: false console: true babel: false -->
user IP FilesOut FilesIn loginCount
---- -- -------- ------- ----------
bob1 192.45.00.00 2 0 1
bob2 10.00.00.01 0 1 1
<!-- end snippet -->
请注意,我只翻译代码部分,不包括输出部分。
英文:
You want a pivot table. Try this
<!-- begin snippet: js hide: false console: true babel: false -->
$csv = @"
username method ip uri-stem
ABC\bob1 RETR 192.45.00.00 file.txt
- USER 10.00.00.01 bob2
ABC\bob2 STOR 10.00.00.01 file1234.txt
- USER 192.45.00.00 bob1
ABC\bob1 RETR 192.45.00.00 file2.txt
"@
#remove spaces at beginning of line
$csv = $csv -replace ".* -","-"
#replace spaces with comma
$csv = $csv -replace " +",","
$table = $csv | ConvertFrom-Csv
foreach($row in $table)
{
$row.username = $row.username -replace "^.*\\",""
if($row.username -eq "-") { $row.username = $row.'uri-stem' }
}
#$table
$groups = $table | Group-Object -Property username
$pivotTable = [System.Collections.ArrayList]::new()
foreach($row in @($groups))
{
$newRow = New-Object -TypeName psobject
$newRow | Add-Member -NotePropertyName user -NotePropertyValue $row.Name
$firstGroup = $row.Group | Select-Object -first 1
$newRow | Add-Member -NotePropertyName IP -NotePropertyValue $firstGroup.IP
$FilesOut = @($row.Group | Where-Object {$_.method -eq "RETR"})
$newRow | Add-Member -NotePropertyName FilesOut -NotePropertyValue $FilesOut.Count
$FilesIn = @($row.Group | Where-Object {$_.method -eq "STOR"})
$newRow | Add-Member -NotePropertyName FilesIn -NotePropertyValue $FilesIn.Count
$loginCount = @($row.Group | Where-Object {$_.method -eq "USER"})
$newRow | Add-Member -NotePropertyName loginCount -NotePropertyValue $loginCount.Count
$pivotTable.Add($newRow) | Out-Null
}
$pivotTable | Format-Table
<!-- end snippet -->
Output
<!-- begin snippet: js hide: false console: true babel: false -->
user IP FilesOut FilesIn loginCount
---- -- -------- ------- ----------
bob1 192.45.00.00 2 0 1
bob2 10.00.00.01 0 1 1
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论