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
4
  • Sorry, had to delete my answer as it didn't work. switch isn't the right technique to use. Commented Nov 12, 2019 at 9:04
  • It's okay. I'm still struggle with this problem :( Commented Nov 12, 2019 at 9:04
  • stackoverflow.com/questions/33511772/… - Use this in combination with Add-Content to add lines to the final output file. Commented Nov 12, 2019 at 9:07
  • It different case I think :( Commented Nov 12, 2019 at 9:12

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

8 Comments

I don't have a Windows computer to test this on at the moment, let me know if it works!
It return empty output file :(
That should now work, I wasn't resetting the $header variable each time. Do you follow how the code works?
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 :)
Yes, I did. Thank you so much!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.