0

I have a 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
Total Salary Number
    1300 USD
ExpectedSalaryNumber
    FY:350 USD
    SY:450 USD
    TS:2000 USD

I want to remove the whole content if the head contain of "Salary Number" and "SalaryNumber", also remove the title "Configuration.... " and output the file after remove it. My expectation output file is like this

Name
    James
Class
    A2
Birthday
    1 September 1982
Family Member
    4
Company Name
    Unlimited Company

I tried this, but it just output the content that I remove. Anyone can help me please. Thank you so much.

$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

Updated Format File

ExpectedSalaryNumber
        FY:350 USD
        (White Space)
        SY:450 USD
        (White Space)
        TS:2000 USD
Cheries
  • 834
  • 1
  • 13
  • 32

1 Answers1

1

This example still uses switch, but this time it uses it to determine what to do with each line, after which it outputs only those lines of interest (non-salary, non-header):

# 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
mjsqu
  • 5,151
  • 1
  • 17
  • 21
  • I don't have a Windows computer to test this on at the moment, let me know if it works! – mjsqu Nov 12 '19 at 09:23
  • It return empty output file :( – Cheries Nov 12 '19 at 09:29
  • That should now work, I wasn't resetting the `$header` variable each time. Do you follow how the code works? – mjsqu Nov 12 '19 at 09:34
  • 1
    Cheers - pretty tough without a computer to test it on! Could you accept the answer if you're happy with it. Added heaps of comments too to explain how it processes. I learnt how to use `switch` too, so we both gained something :) – mjsqu Nov 12 '19 at 09:41
  • Yes, I did. Thank you so much!! – Cheries Nov 12 '19 at 09:50
  • Hi @mjsqu . I have one more problem with this question. If I have a format file like those (I put the format file above), it only remove the head, not include the content. If you don't mind can help me please. Thanks – Cheries Nov 13 '19 at 06:48
  • Sorry, please start a new question. – mjsqu Nov 13 '19 at 10:52
  • Hi @mjsqu. I did it. Thank you. I can fixed it. If you don't mind, please take a look to my other question here https://stackoverflow.com/q/58688974/11076819 . Thank you! – Cheries Nov 14 '19 at 05:53