Вопрос

I have the following data

SBA SUBJECT1 3138L3BK0|31.4|44.4
SBA SUBJECT1 3138L3BK1|31.4|44.4
SBA SUBJECT1 3138L3BK2|31.4|44.4
SBA SUBJECT1 3138L3BK0|31.4|44.4
SBA SUBJECT2 3138L3BK3|31.4|44.4
SBA SUBJECT2 3138L3BK3|31.4|44.4
SBA SUBJECT2 3138L3BK3|31.4|44.4
SBA SUBJECT2 3138L3BK4|31.4|44.4

I need the following result

SBA SUBJECT 1, Count = 3
SBA Subject 2, Count = 2

The logic is.

  1. Take the first column in the pipe delimited row.
  2. Separate the string into 2 parts based on the last space
  3. Group by first part and return count of unique items in the second part

I have the following code so far

$p= import-csv filename.txt -delimiter "|" -Header ("cusip")
$p | Group {$_.cusip.Substring(0,$_.cusip.LastIndexOf(" "))}

But I can't seem to get the unique count.

Это было полезно?

Решение 2

Here's a solution using a regex and a hash table:

$ht=@{} 

get-content filename.txt | 
 foreach {
   $_ -match '^(\S+\s\S+)\s([^|]+)' > $null
   $ht[$Matches[1]] += @($Matches[2])
   }
$ht.keys | foreach {
 [PSCustomObject]@{Name= $_;Count=($ht[$_] | Get-Unique).count}
  } | ft -auto

Другие советы

Looks like mjolinor got to it before me, but it's a slightly different approach.

gc FileName.txt | ?{$_ -match "^(SBA SUBJECT(?:1|2)).*?\|([^|]+?)\|(.*)"} | %{
    New-Object PSObject -Property @{
        CUSIP=$Matches[1]
        Col1=$Matches[2]
        Col2=$Matches[3]
    }
}|Group CUSIP|FT Name,Count -AutoSize
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top