回答如何用判断两个分隔符间的内容,如不符合则删除分隔符间的全部行


# 读入原始文件
$fileContent = Get-Content .\1.txt -Raw

$filteredContent = @()

# 先根据段落分隔符将文件若干份
$sections = [regex]::Matches($fileContent ,'=+') 

for ($index=1; $index -le $sections.Count -1;$index++)
{
    # 提取分割符中间的内容
    $section = $sections[$index]
    $startIndex = $sections[$index-1].Index+$sections[$index-1].Length
    $length = $sections[$index].Index - $startIndex
    $sectionContent = $fileContent.Substring($startIndex,$length)

    # 此时 $sectionContent 中的内容如下:

    #        2015-09-02 05:05:36  UserID:0, UserName:Tom
    #        USER:8712
    #        ID:4417 7477
    #        JAVA:44711,上海市
    #        SHARE:AA2DDDFDS,74101

    # 让我们用正则表达式截取UserName的信息
    $sectionContent -match "UserName:(?<UserName>.*)\b" | Out-Null
    $userName = $Matches.UserName
    # 此时 $userName 中的值可能为Apple,Tom 等。
    
    #核心需求: 针对userName过滤判断
    if($userName -ne 'Tom') 
    {
        $filteredContent += $sectionContent
    }
}


# 向$filteredContent 结果集中前后加入两个空元素
# 来确保结果集中文档的开始和结尾也存在分隔符。
$filteredContent = @('') + $filteredContent + @('')

# $result 将会保存结果,并将结果输出到文件中
$result = $filteredContent -join $sections[0].Value
$result | Out-File 'result.txt'

# 思路大概是这样,在给出的实例文件中测试通过
# 具体环境中可能需要微调

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。