0

I have to make a script in C# to delete 'LF' from a file.

The structure of the file like this :

line1 'CR''LF'
line2 'CR''LF'
line3 'LF'
some_text_of_line_3 'CR''LF'

I want to have this :

line1 'CR'
line2 'CR'
line3 some_text_of_line_3 'CR'

Thank you !

Thomas
  • 49
  • 3

2 Answers2

2

If the file is not that large (i.e. we can read it into memory):

 using System.IO;

 ...

 String path = @"c:\MyFile.txt";

 // Read file, remove LF (which is "\n")
 string text = File.ReadAllText(path).Replace("\n", ""); 

 // Write text back
 File.WriteAllText(path, text);
Dmitry Bychenko
  • 149,892
  • 16
  • 136
  • 186
1

You can try using a Regex expression for this:

string myFile= File.ReadAllText(@"c:\FileName.txt");
myFile= Regex.Replace(myFile, @"LF", "AnythingYouwantHere");
File.WriteAllText(@"c:\FileName.txt", myFile);

Hope this helps you.

SmailxCoder
  • 63
  • 10
  • I don't explain that it's a csv file, not txt..sorry my bad – Thomas Aug 01 '19 at 13:13
  • @SmailxCoder. No, he wants to replace the line feed character (ASCII: 10 c#: \n) from the end of lines. These are print control characters. This is different than the character literal 'LF'. – JazzmanJim Aug 01 '19 at 13:58
  • @JazzmanJim Okay i understand now, at first i didn't understand what he really wants, thats why i gave him an answer equivalent to what i did understand, Thank you – SmailxCoder Aug 01 '19 at 14:00
  • @SmailxCoder. No problem. This is the type of issue us old coders ran into in the 80's and 90's. :) – JazzmanJim Aug 01 '19 at 14:08