Avatar of Skale
Skale
 asked on

How to delete between specific lines from text file in C#

Hello,

I've one text file and i'd like to delete lines between two specific lines then i'd like to add new lines between these two lines. I really need an expert support for correct approach how to do that.

Any help would be great! and thanks.

Start string:

ifctngroup.begin (                  $G_mnt_nlin                   )                           ! $G_mnt_nlin

Open in new window


End String:

ifctngroup.end (                    $G_mnt_nlin                   )                           ! $G_mnt_nlin

Open in new window


SampleData
C#

Avatar of undefined
Last Comment
Rikin Shah

8/22/2022 - Mon
Kimputer

If there will be 5 lines deleted, do you mean to replace it with ONE new line each time, or the same (in this case, 5) empty lines?
Are you sure this file contains only spaces? Not tabs?

Bill Prew

I'm assuming you have some skills in C# since you are asking in that group.

Conceptually this feels fairly straight forward.

- open the input file for reading
- open the output file for writing
- read each line from input file, and write it to output file until you hit the "start" text match
- write the new lines you want to insert to output file
- continue reading the input file looking for the "end" text match, not writing them anywhere
- once you see the "end" text match, go back to reading lines from input and writing them to output

There are several ways you can implement this, setting a flag once you see "start" or "end", or a mode switch etc, or just moving on to different sections of logic and looping, that's often a personal preference.


»bp
ASKER CERTIFIED SOLUTION
Rikin Shah

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23