none
Script to find and replace a section of lines in a text file RRS feed

  • Question

  • Hi, I been trying to create a Powershell or VBscript script to find and replace a section of lines in a text file. I know how to find and replace specified text with New Text, but this only works for text on one line.


    #Search for specified text and replace it with specified replacement text

    $strConnectionFile = "C:\Temp\Connection.txt"

    (Get-Content $strConnectionFile) | ForEach-object {$_ -replace "OldText", "NewText"} | Set-Content $strConnectionFile

     

    The issue I’m having is that I need to search for the occurrence of 6 lines of text and then replace them with 2 lines of ‘New text’ as below?

    when the script runs it needs to finds the first instance of the following lines

    </tr>
    <tr>
    </tr>
    <tr>
    </tr>
    <tr>

    and this is to be replaced with just 2 lines

    </tr>
    <tr>

     

    I need to do this for 1000+ users so any help with the command syntax I should use is much appreciated

    Thanks,

    Paj B





    • Edited by Paj B Thursday, September 8, 2016 7:18 AM my initial request changed to a question
    Wednesday, September 7, 2016 3:04 PM

Answers

  • Hello Paj,

    you can do this, by reading the entire text as a single string:

    Get-Content text.txt -ReadCount 0

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, September 8, 2016 9:12 AM

All replies