0

Consider to my previous question with this awesome answer https://stackoverflow.com/a/58815537/11076819 I have a problem once I have text file with this format.

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
ExpectedSalaryNumber
    FY:350 USD

    SY:450 USD

    TS:2000 USD

In the part of ExpectedSalaryNumber, The content do not remove because there is a white space which are

 FY:350 USD

 SY:450 USD

 TS:2000 USD

Anyone can help me to fix this problem? Thank you so much for helping me.

Cheries
  • 834
  • 1
  • 13
  • 32

1 Answers1

1

This the answer from my previous answer combine @JosefZ answer

# 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
shareeditflag
Cheries
  • 834
  • 1
  • 13
  • 32