Derek
Derek

Reputation: 8630

Stream Reader Readline detect newline characters

I'm trying to remove any "new line" characters from each line of text in my log file.

Below is an example entry that I am reading in with a Stream Reader :-

<![LOG[Raising event:
[SMS_CodePage(850), SMS_LocaleID(2057)]
instance of SoftDistProgramStartedEvent
{
    AdvertisementId = "000216F6";
    ClientID = "GUID:B55C2757-CBAE-468E-B54F-46CAF2ECF68F";
    CommandLine = "\"C:\\WINNT\\system32\\cscript.exe\" /nologo Shutdown_Desktops_Overnight.vbs";
    DateTime = "20130211080211.784000+000";
    MachineName = "DWD*****";
    PackageName = "0000073C";
    ProcessID = 2516;
    ProgramName = "Shutdown Desktops Overnight";
    SiteCode = "S00";
    ThreadID = 3640;
    UserContext = "NT AUTHORITY\\SYSTEM";
    WorkingDirectory = "C:\\WINNT\\system32\\CCM\\Cache\\0000073C.1.System\\";
};
]LOG]!><time="08:02:11.800+000" date="02-11-2013" component="execmgr" context="" type="1" thread="3640" file="event.cpp:522">

In the actual Logfile this is displayed as one line in the file, with the "New Line Characters" replaced with a square.

I'm using the following code to read in the log entries :-

using (StreamReader sr = new StreamReader(@"C:\Documents and Settings\riversd\Desktop\Logfile2.log"))
        {
            string Line;

            while ((Line = sr.ReadLine()) != null)
            {

            }
        }

The issue is that when the StreamReader reads this entry from the txt file it breaks at :-

"<![LOG[Raising event:"

I need to remove all new line characters in this entry, on the fly. I don't want to read the entire file into memory and then remove them, I'd rather deal with each log as I read it.

Is it possible?

Upvotes: 0

Views: 25922

Answers (3)

Dave Kidder
Dave Kidder

Reputation: 1848

The call to Replace isn't working because of this detail from the MSDN doc of StreamReader.ReadLine:

A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed.

So if you're going to use StreamReader.ReadLine to read in the input, you could build up a new string with StringBuilder.Append (not AppendLine) as StreamReader.ReadLine implicitly removes the new line characters.

var filePath = "text.txt";
StringBuilder sb = new StringBuilder();

using (StreamReader reader = new StreamReader(filePath))
{
    while (!reader.EndOfStream)
    {
        sb.Append(reader.ReadLine());
    }
}

File.WriteAllText(filePath, sb.ToString());

Upvotes: 10

New Bee
New Bee

Reputation: 1011

i dont know if anyone else was having exactly this issue. here is the code i used to fix this issue.

      using (System.IO.FileStream File = new System.IO.FileStream(e.FullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
            {
                using (System.IO.StreamReader Reader = new System.IO.StreamReader(File, Encoding.Default))
                {
                    String CompleteData = Reader.ReadToEnd();
                    foreach (String Line in CompleteData.Split(new char[] { (char)13 }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (Line.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)[0].Contains("Raising event"))
                        {
                             //Do Stuff
                        }
                    }
                    Reader.Close();
                }
                File.Close();
            }

For some reason i had to do this because just using streamreader would throw an exception saying that the file is in use from another process.

It might help someone else at a later date..

Upvotes: 1

VladL
VladL

Reputation: 13043

sr.ReadLine().Replace(Environment.NewLine, String.Empty);

EDIT: In case the end of line is not \r\n but \n you can use regex:

Line = Regex.Replace(sr.ReadLine(), @"(\r\n|\n)", String.Empty);

Upvotes: 1

Related Questions