Link to home
Start Free TrialLog in
Avatar of Skale

asked on

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


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

User generated image
Avatar of 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?

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.

Avatar of Rikin Shah
Rikin Shah
Flag of India image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial