var rx = new Regex(@"(?[^\r\n]*)(?\r\n|\r|\n|$)"); foreach (Match m in rx.Matches(str31)) { var line0 = m.Groups["line"].Value; var eol0 = m.Groups["eol"].Value; switch (eol0) { case "\r\n": // Proper CRLF – no issue break; case "\n": if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("It is missing CR for the line below:"); sw.WriteLine(line0); // ✅ Only the actual line sw.WriteLine(); b_err3 = true; break; case "\r": if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("It is missing LF for the line below:"); sw.WriteLine(line0); // ✅ sw.WriteLine(); b_err3 = true; break; case "": if (!string.IsNullOrEmpty(line0)) { if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("The line below is missing both LF and CR:"); sw.WriteLine(line0); // ✅ sw.WriteLine(); b_err3 = true; } break; } }