-notmatch with ...(3 points)

时间:2016-12-14 12:00:39

标签: powershell

我的PowerShell CSV工具存在一个奇怪的问题。我试着写一个小的支票,过滤掉某些名字和字符。这些名称/字符位于如下文本文件中:

XXX
nana
YYY
...
DDD

我做的检查是这样的:

$reader = [System.IO.File]::OpenText($fc_file.Text)
try {
    for() {
        $line = $reader.ReadLine()
        if ($line -eq $null) { break }
        # process the line
        Import-Csv $tempfile -Delimiter $delimeter -Encoding $char |
            where {$_.$fc_suchfeld -notmatch $line} |
            Export-Csv $tempstorage -Delimiter $delimeter -Encoding $char -NoTypeInfo

直到3点的线条才能很好地工作。此时几乎所有行都被删除。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

-match运算符执行正则表达式匹配。 .是正则表达式中的元字符,匹配除换行符之外的任何字符。因此,正则表达式...匹配具有至少3个字符的任何行。如果你想使用$fc_file中的行作为文字字符串匹配,你需要转义它们:

... | where {$_.$fc_suchfeld -notmatch [regex]::Escape($line)} | ...

或进行通配符匹配:

... | where {$_.$fc_suchfeld -notlike "*$line*"} | ...
相关问题