1

So I need to remove a few blocks of txt in multiple txt files. The files look like this:

Header
Value
Data
   <DefaultValue>
       sdf
         asdfsdf
       asfkfkf
   </DefaultValue>
Data3
Data2
   <DefaultValue>
       sdfdffff
          asdfsasdfddf
       asfkf
   </DefaultValue>**
Dat
End

I need to remove the block of lines that begin with DefaultValue and end with /DefaultValue

The result should look like this:

Header
Value
Data
Data3
Data2
Dat
End

Unfortunately it is not a properly formed XML file so XML node removal won't work.

Any suggestions?

1 Answer 1

2

I used this to get around the need to add a third party Join-String function:

$filePath = '.\Desktop\new  3.txt'
$text = ""
Get-Content $filePath | % {$text += $_ + "NEWLINE"}
$text = $text -Replace "<Default.+?DefaultValue>",""
$text.Replace("NEWLINE","`r`n")

To yield:

Header
Value
Data

Data3
Data2
   **
Dat
End

To make this repetitive, you'd want to do something like this:

Get-ChildItem -Path C:\logs\ -Filter "*.txt" | % {
    $text = ""
    Get-Content $_.FullName | % {$text += $_ + "NEWLINE"}
    $text = $text -Replace "<Default*DefaultValue>",""
    $text.Replace("NEWLINE","`r`n") | Out-File $_.FullName -Force
}

Good luck, back your data up, and check back for more elegant answers from another user.

Sign up to request clarification or add additional context in comments.

Comments

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.