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


PowerShell交流中心分类: Powershell基础如何用判断两个分隔符间的内容,如不符合则删除分隔符间的全部行
0
cloudlands asked 8年 ago

例如1.txt
==============================================================================
2015-09-02 05:05:36  UserID:0, UserName:Apple
USER:25174
ID:44178
DO:44717

==============================================================================
2015-09-02 05:05:36  UserID:0, UserName:Tom
USER:258
ID:4414
DO:417,广东省
SHARE:AA2DFDS,74101

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

==============================================================================
 
如当UserName:Tom时,删除“==============”间的所有行。

0 Answers
1
Mooser Lee 管理员 answered 8年 ago
# 读入原始文件
$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'

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