locked
Search for and replace line of text RRS feed

  • General discussion

  • Hello,

    I need a powershell script to search a text file for a known line of text ('Line 2 Heading'), delete the following line, replace it with another line and save contents to a new file name.

    The line to replace starts with a 'tab'. The line to insert must start with a 'tab' also.

    Sample text file contents

    Line 1 Heading

           Line 1 text

    Line 2 Heading

           Line 2 text

    Line 3 Heading

           Line 3 text

    Required text file contents

    Line 1 Heading

           Line 1 text

    Line 2 Heading

           New text goes here

    Line 3 Heading

           Line 3 text

    Hope this is clear.

    Any help appreciated.

    Thanks,

    Mike

    • Changed type Bill_Stewart Monday, November 21, 2016 7:39 PM
    • Moved by Bill_Stewart Monday, November 21, 2016 7:39 PM This is not "scripts on demand"
    Friday, October 14, 2016 2:41 PM

All replies

  • You can probably do this pretty easily in PowerShell. What have you tried so far?

    -- Bill Stewart [Bill_Stewart]

    Friday, October 14, 2016 3:06 PM
  • Hi Bill,

    I'm a newby to Powershell so haven't got far..

    I can open the file, match the text above the line I want to replace and that's it.. oh' and save the new file.

    Mike


    Mike

    Monday, October 17, 2016 1:14 PM
  • So what's your specific question? What precisely isn't working?

    Also, keep in mind that this forum is for scripting questions rather than script requests. We're glad to help with specific questions, but we don't really have the resources to write scripts on request.

    We would suggest starting at the beginning, as you will be better off learning what you need to do rather than just having others do it for you.


    -- Bill Stewart [Bill_Stewart]

    Monday, October 17, 2016 2:30 PM