如何使用PowerShell删除文本文件中的特定项目包含内容?

如何使用PowerShell删除文本文件中的特定项目包含内容?,powershell,Powershell,我有一个这种格式的文本文件 Configuration ; ; Authors: James ; Created: 10/11/2018 ; ; Accepted Name James Class A2 Birthday 1 September 1982 Family Member 4 First Year Salary Number 100 USD Second Year Salary Number 150 USD Company

我有一个这种格式的文本文件

Configuration
;
;   Authors: James
;   Created: 10/11/2018
;
;   Accepted

Name
    James
Class
    A2
Birthday
    1 September 1982
Family Member
    4
First Year Salary Number
    100 USD
Second Year Salary Number
    150 USD
Company Name
    Unlimited Company
Total Salary Number
    1300 USD
ExpectedSalaryNumber
    FY:350 USD
    SY:450 USD
    TS:2000 USD
我想删除整个内容,如果头部包含薪水编号和薪水编号,还需要删除标题配置。。。。并在删除文件后输出该文件。我的期望输出文件如下

Name
    James
Class
    A2
Birthday
    1 September 1982
Family Member
    4
Company Name
    Unlimited Company
我试过了,但它只是输出了我删除的内容。 任何人都可以帮我。 非常感谢你

$name = $false 
& { 
  switch -regex -file .\Configuration.TXT {
    '^*Salary Number*, ^*SalaryNumber*' { $name = $true; continue }
    '^\s' { if ($name) { $_.Trim() }}
    '^\S' { if ($name) { return } }
  }
} | Remove-Item | Out-File .\Output.txt
更新的格式文件 本例仍然使用switch,但这次它使用switch来确定如何处理每一行,之后它只输出那些感兴趣的行(非工资、非标题):

# These variables are changed to true if the line is a header or a salary
$header = $false
$salary = $false
Get-Content .\Configuration.TXT | ForEach-Object {
    # The header flag is reset each time as the header checks are made first
    $header = $false
    # The $salary variable is only reset back to false when a new header is reached, provided it does not contain Salary\s*Number

    # Use switch to find out what type of line it is
    switch -regex ($_)
    {
        '^Configuration' # Beginning with Configuration 
        {
            $header = $true
        }
        '^;' # Beginning with semicolon 
        {
            $header = $true
        }
        # Data lines begin with a space
        # move to the next line - do not alter $salary variable
        '^\s'  
        {
            continue
        }
        # If Salary Number is found set the flag
        'Salary\s*Number' {
            $salary = $true
        }
        # This is only reached once it has been determined the line is 
        # not a header
        # not a salary header line
        # not a data line 
        # i.e. only headers that are not Salary
        # this resets the flag and makes lines eligible for output
        default {
            $salary = $false
        }

    }
    # Only output lines that are not Salary and not headers
    if ($salary -eq $false -and $header -eq $false) {
        $_
    }
} | Out-File .\Output.txt

抱歉,我不得不删除我的答案,因为它不起作用。切换不是正确的方法,没关系。我仍在努力解决这个问题:-将此与“添加内容”结合使用,将行添加到最终输出文件中。我认为这是另一种情况:我目前没有Windows计算机来测试此功能,请告诉我它是否有效!它返回空的输出文件:现在应该可以了,我没有每次都重置$header变量。你知道代码是如何工作的吗?干杯-没有电脑测试它真的很难!如果你满意这个答案,你能接受吗。还添加了大量注释来解释it是如何处理的。我也学会了如何使用开关,所以我们都得到了一些东西:是的,我学会了。非常感谢你!!
# These variables are changed to true if the line is a header or a salary
$header = $false
$salary = $false
Get-Content .\Configuration.TXT | ForEach-Object {
    # The header flag is reset each time as the header checks are made first
    $header = $false
    # The $salary variable is only reset back to false when a new header is reached, provided it does not contain Salary\s*Number

    # Use switch to find out what type of line it is
    switch -regex ($_)
    {
        '^Configuration' # Beginning with Configuration 
        {
            $header = $true
        }
        '^;' # Beginning with semicolon 
        {
            $header = $true
        }
        # Data lines begin with a space
        # move to the next line - do not alter $salary variable
        '^\s'  
        {
            continue
        }
        # If Salary Number is found set the flag
        'Salary\s*Number' {
            $salary = $true
        }
        # This is only reached once it has been determined the line is 
        # not a header
        # not a salary header line
        # not a data line 
        # i.e. only headers that are not Salary
        # this resets the flag and makes lines eligible for output
        default {
            $salary = $false
        }

    }
    # Only output lines that are not Salary and not headers
    if ($salary -eq $false -and $header -eq $false) {
        $_
    }
} | Out-File .\Output.txt