在数组中存储重复数据删除的非ascii字符列表

时间:2015-01-24 13:22:27

标签: arrays powershell ascii

我有很多包含扩展ascii字符的文本文件。我想构建一个这些字符的重复数据删除列表,例如:

á
ö
¿

我能够读取文件并删除基本的ascii字符。但是,由于我是Powershell的新手,我如何将每一行分成字符,将它们与现有的字符列表进行比较,并输出一个重复数据删除的非基本ascii字符列表?

$files = Get-ChildItem "C:\Users\me\Desktop\ascii" -filter "*.txt"
Foreach ($file in $files) {
    $newfile = @()

    Get-Content  $file.fullname | Foreach-Object  {
        $newfile += [string]([char[]]$_ | where-object {[int]$_ -lt 127})
    }
    Write-Host $newfile
}

[编辑1]到达那里......

$files = Get-ChildItem "C:\Users\me\Desktop\ascii" -filter "*.txt"
$array = @()

Foreach ($file in $files) {
    Get-Content  $file.fullname | Foreach-Object  {
        $line  = [string]([char[]]$_ | where-object {[int]$_ -lt 127}) -split '\s+' | Foreach {
            If ($array -notcontains $_) {
                $array.Add($_)
            }
        }
    }   
}

[编辑2]这个^^^适用于PS 4但不适用于2(我的服务器上有)?有没有帮助写这个PS2兼容版本?

[编辑3]我发现使用$array = @()在V2和V4上工作正常: - )

2 个答案:

答案 0 :(得分:1)

也许我错过了一些东西,但如果您要构建扩展 ASCII字符列表,那么不应该检查更大的值是否超过127?

这应该为您提供所有文件的扩展ASCII字符列表,没有重复:

Get-ChildItem 'C:\Users\me\Desktop\ascii' -filter '*.txt' |
  Get-Content |
  % { [char[]]$_ } |
  ? { [int]$_ -gt 127 } |
  select -Unique

答案 1 :(得分:0)

有任何帮助吗? (需要V3)

$string = 'áTestáöö¿'

$ht = @{}

$basic,$extended = ([char[]]$string).Where({[int]$_ -lt 127},'Split')
$extended | foreach {$ht[$_] += $null}

[string]$basic

$ht.keys
T e s t
¿
ö
á