测量/计算登录次数和文件传输次数从CSV数据中。

huangapple go评论56阅读模式
英文:

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 = @&quot;
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
&quot;@
#remove spaces at beginning of line
$csv = $csv -replace &quot;.* -&quot;,&quot;-&quot;
#replace spaces with comma
$csv = $csv -replace &quot; +&quot;,&quot;,&quot;
$table = $csv | ConvertFrom-Csv


foreach($row in $table)
{
   $row.username = $row.username -replace &quot;^.*\\&quot;,&quot;&quot;
   if($row.username -eq &quot;-&quot;) { $row.username = $row.&#39;uri-stem&#39; }
}
#$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 &quot;RETR&quot;})
   $newRow | Add-Member -NotePropertyName FilesOut -NotePropertyValue $FilesOut.Count
   $FilesIn = @($row.Group | Where-Object {$_.method -eq &quot;STOR&quot;})
   $newRow | Add-Member -NotePropertyName FilesIn -NotePropertyValue $FilesIn.Count
   $loginCount = @($row.Group | Where-Object {$_.method -eq &quot;USER&quot;})
   $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 -->

huangapple
  • 本文由 发表于 2023年2月27日 06:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定