Detect CR LF to each line

HuaMin Chen
HuaMin Chen used Ask the Experts™
on
Hi,
Using these
            string text = File.ReadAllText( @File0 );
            var matches = Regex.Matches( text, @"(?<line>.*?)((?<eol>\r\n)|\r|\n)|(?<line>.+?)\z" ).Cast<Match>();

            foreach( var m in matches )
            {
                if( m.Groups["eol"].Success )
                {
                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                    {
                        sw.WriteLine("GOOD: {m.Groups[line]}");
                        sw.WriteLine("");
                    }
                }
                else
                {
                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                    {
                        sw.WriteLine("BAD:  {m.Groups[line]}");
                        sw.WriteLine("");
                    }
                }
            }

Open in new window

I cannot show which line is having the problem. What to adjust, in above, as I want to show the relevant line, which is not having either CR or LF, at the end of it, for every line within the given text file?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
I am assuming that the language you are using is C#?
You want to show which lines is missing a CR or LF, is that correct?
Please upload a sample file showing the different conditions to test with.
HuaMin ChenProblem resolver

Author

Commented:
Please refer to the sample file attached.
t8.txt
Fernando SotoRetired
Distinguished Expert 2017

Commented:
The file you posted only has 3 lines and each line contains a CR LF on them. Can not test code if it does not have all the conditions you want to test for.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2014

Commented:
var matches = Regex.Matches(text, @"(?!$)(?<line>[^\r\n]*)((?<eol>\r\n)|\r|\n|)");
foreach (Match m in matches)
{
    if (m.Groups["eol"].Success)
    {
        using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
        {
            sw.WriteLine($"GOOD: {m.Groups["line"]}");
            sw.WriteLine("");
        }
    }
    else
    {
        using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
        {
            sw.WriteLine($"BAD:  {m.Groups["line"]}");
            sw.WriteLine("");
        }
    }
}

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
Sorry, I get
Error	1	Unexpected character '$'	C:\dp9\Validate_File2\Validate_File1\Program.cs	286	38	Validate_File1
Error	2	Unexpected character '$'	C:\dp9\Validate_File2\Validate_File1\Program.cs	294	38	Validate_File1
Error	3	Invalid expression term ''	C:\dp9\Validate_File2\Validate_File1\Program.cs	286	38	Validate_File1
Error	4	) expected	C:\dp9\Validate_File2\Validate_File1\Program.cs	286	39	Validate_File1
Error	5	; expected	C:\dp9\Validate_File2\Validate_File1\Program.cs	286	57	Validate_File1
Error	6	; expected	C:\dp9\Validate_File2\Validate_File1\Program.cs	286	61	Validate_File1
Error	7	Invalid expression term ')'	C:\dp9\Validate_File2\Validate_File1\Program.cs	286	65	Validate_File1
Error	8	; expected	C:\dp9\Validate_File2\Validate_File1\Program.cs	286	65	Validate_File1
Error	9	Invalid expression term ''	C:\dp9\Validate_File2\Validate_File1\Program.cs	294	38	Validate_File1
Error	10	) expected	C:\dp9\Validate_File2\Validate_File1\Program.cs	294	39	Validate_File1
Error	11	; expected	C:\dp9\Validate_File2\Validate_File1\Program.cs	294	57	Validate_File1
Error	12	; expected	C:\dp9\Validate_File2\Validate_File1\Program.cs	294	61	Validate_File1
Error	13	Invalid expression term ')'	C:\dp9\Validate_File2\Validate_File1\Program.cs	294	65	Validate_File1
Error	14	; expected	C:\dp9\Validate_File2\Validate_File1\Program.cs	294	65	Validate_File1

Open in new window

due to the two WriteLine lines below

            var matches = Regex.Matches(text, @"(?!$)(?<line>[^\r\n]*)((?<eol>\r\n)|\r|\n|)");
            foreach (Match m in matches)
            {
                if (m.Groups["eol"].Success)
                {
                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                    {
                        sw.WriteLine($"GOOD: {m.Groups["line"]}");
                        sw.WriteLine("");
                    }
                }
                else
                {
                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                    {
                        sw.WriteLine($"BAD:  {m.Groups["line"]}");
                        sw.WriteLine("");
                    }
                }
            }

Open in new window

Top Expert 2014

Commented:
That's C#6.
Downgrading it:
1:var matches = Regex.Matches(text, @"(?!$)(?<line>[^\r\n]*)((?<eol>\r\n)|\r|\n|)");
2:foreach (Match m in matches)
3:{
4:    if (m.Groups["eol"].Success)
5:    {
6:        using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
7:        {
8:            sw.WriteLine("GOOD: " + m.Groups["line"]);
9:            sw.WriteLine("");
10:        }
11:    }
12:    else
13:    {
14:        using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
15:        {
16:            sw.WriteLine("BAD:  " + m.Groups["line"]);
17:            sw.WriteLine("");
18:        }
19:    }
20:}

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
Many thanks. How to make the message clearer, to also mention either CR or LF is missing?
Top Expert 2014
Commented:
You could put groups for each possibility
((?<eol>\r\n)|(?<nolf>\r)|(?<nocr>\n)|)

Open in new window

and check each one
if (m.Groups["eol"].Success)
...
else if (m.Groups["nolf"].Success)
...

Open in new window

You could also simplify the regex:
var matches = Regex.Matches(text, @"(?!$)(?<line>[^\r\n]*)(?<eol>\r?\n?)");
foreach (Match m in matches)
{
    switch (m.Groups["eol"].Value)
    {
        case "\r\n":
            output.Add("GOOD: " + m.Groups["line"]);
            break;
        case "\r":
            output.Add("BAD (no LF): " + m.Groups["line"]");
            break;
        case "\n":
            output.Add("BAD (no CR): " + m.Groups["line"]");
            break;
        case "":
            output.Add("BAD (none): " + m.Groups["line"]");
            break;
    }

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
Using your codes, I run against these 2 files
https://app.box.com/s/vrjzvd8ra47p4gaiwit3nvdkf2syghe6
https://app.box.com/s/uotyuo782xdchu5dueb2fym2jqmj64y8

but I get the messages saying no LF, while I can see CR and LF to each line. Why?
Top Expert 2014

Commented:
When I run it, I get a "GOOD" result for each line of the two files.
How do you load them?
HuaMin ChenProblem resolver

Author

Commented:
Here are the codes, I've used to run against the files, including the current two files in above.
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Xml;
using System.Globalization;
//using Ionic.Zip;
using System.Net;
using System.Net.Http;
using System.Collections.Generic;
using System.Data.OleDb;
//using Microsoft.VisualBasic;
using System.Text; // This was needed to make ASCIIEncoding function
using System.Security.Cryptography; // This was needed to make everything related to MD5 function

namespace Validate_File1
{
    class Program
    {
        static void Main(string[] args)
        {
            string File0, File1, Call_Sign, Line0, ftx_Line1="", eqd_Line1="";
            int Pos0, Pos1, Pos2, Pos3, Pos4, Pos5, Pos6, Pos7, Pos8, Pos9, Pos10, Pos11, Pos12, Pos13, Pos14, Pos15,Sep_Count;
            bool UNB_Exist, UNZ_Exist, Voy_Ref0, Voy_Ref1,gid_Line,ftx_Line,eqd_Line;
            bool allowappend = true;

            File0 = "C:/cmp3g/CODECO_ERROR_0/" + args[0] + "/" + args[1];
            //File1 = "C:/cmp3g/CODECO_ERROR_0/Error" + Format(Now, "mmss") + ".txt"
            File1 = "C:/cmp3g/CODECO_ERROR_0/Error.txt";

            Sep_Count = 0;
            UNB_Exist = false; UNZ_Exist = false; Voy_Ref0 = false; Voy_Ref1 = false; gid_Line = false; ftx_Line = false; eqd_Line = false;
            try
            {
                using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                {
                    sw.WriteLine("Within file - '" + args[1] + "', for client code '" + args[0] + "':");
                    sw.WriteLine("");
                }

                using (StreamReader sr = new StreamReader(@File0, Encoding.Default))
                {
                    while ((Line0 = sr.ReadLine()) != null)
                    {
                        Pos0 = Line0.IndexOf("'");
                        Pos1 = Line0.IndexOf("'", Pos0 + 1);

                        if (Pos0 > -1 && Pos1 > -1)
                            Sep_Count = 2;

                        Line0 = Line0.Trim();
                        if (Line0 != "")
                        {
                            Voy_Ref0 = false; Voy_Ref1 = false;

                            if (Line0.IndexOf("UNB+") > -1)
                                UNB_Exist = true;

                            if (Line0.IndexOf("UNZ+") > -1)
                                UNZ_Exist = true;

                            if (Line0.IndexOf("GID+") > -1)
                                gid_Line = true;

                            Pos0 = Line0.IndexOf("FTX+");
                            if (Pos0 > -1)
                            {
                                Pos1 = Line0.IndexOf("'", Pos0 + 1);
                                ftx_Line = true;
                                if (Sep_Count >= 2)
                                    ftx_Line1 = Line0.Substring(Pos0, Pos1 - Pos0);
                                else
                                    ftx_Line1 = Line0;
                                eqd_Line = false;
                            }

                            Pos0 = Line0.IndexOf("EQD+");
                            if (Pos0 > -1)
                            {
                                Pos1 = Line0.IndexOf("'", Pos0 + 1);
                                if (ftx_Line)
                                {
                                    eqd_Line = true;
                                    if (Sep_Count >= 2)
                                        eqd_Line1 = Line0.Substring(Pos0, Pos1 - Pos0);
                                    else
                                        eqd_Line1 = Line0;

                                }
                            }
                            Pos0 = Line0.IndexOf("RFF+AHI:");
                            if (Pos0 > 0)
                            {
                                Pos1 = Line0.IndexOf("'", Pos0 + 8);

                                if (Pos1 - Pos0 - 8 > 15)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.WriteLine("IML Job No is too long; at most only 15 characters are allowed to have, to such line below.");
                                        sw.WriteLine(Line0);
                                        sw.WriteLine("");
                                    }
                                }
                            }

                            Pos0 = Line0.IndexOf("RFF+BM:");
                            if (Pos0 > 0)
                            {
                                Pos1 = Line0.IndexOf("'", Pos0 + 7);

                                if (Pos1 - Pos0 - 7 > 17)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.WriteLine("BL No is too long; at most only 17 characters are allowed to have, to such line below.");
                                        sw.WriteLine(Line0);
                                        sw.WriteLine("");
                                    }
                                }
                            }

                            Pos0 = Line0.IndexOf("SEL+");
                            if (Pos0 > -1)
                            {
                                Pos1 = Line0.IndexOf("+", Pos0 + 4);
                                Pos2 = Line0.IndexOf("'", Pos0 + 4);
                                if (Pos1 < 0 || Pos1 > Pos2)
                                    Pos1 = Pos2;

                                if (Pos1 - Pos0 - 4 > 10)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.WriteLine("Seal Number is too long; at most only 10 characters are allowed to have, to such line below.");
                                        sw.WriteLine(Line0);
                                        sw.WriteLine("");
                                    }
                                }

                                if (Pos1 - Pos0 - 4 == 0)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.WriteLine("Missing Seal Number, to such line below.");
                                        sw.WriteLine(Line0);
                                        sw.WriteLine("");
                                    }
                                }
                            }

                            Pos0 = Line0.IndexOf("TDT+20+");
                            if (Pos0 > -1)
                            {
                                Pos1 = Line0.IndexOf("+", Pos0 + 7);

                                if (Pos1 - Pos0 - 7 > 5)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.Write("Voyage Reference is invalid and too long. ");
                                        Voy_Ref0 = true;
                                    }
                                }

                                Pos2 = Line0.IndexOf("+", Pos1 + 1);
                                Pos3 = Line0.IndexOf("+", Pos2 + 1);
                                Pos4 = Line0.IndexOf("+", Pos3 + 1);
                                Pos5 = Line0.IndexOf("+", Pos4 + 1);
                                Pos6 = Line0.IndexOf("+", Pos5 + 1);
                                Pos7 = Line0.IndexOf(":103", Pos6 + 1);

                                Call_Sign = Line0.Substring(Pos6 + 1, Pos7 - Pos6 - 1);

                                if (Call_Sign.Length > 5 || Call_Sign.IndexOf(" ") > 0)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.Write("Call Sign can be invalid.");
                                        Voy_Ref1 = true;
                                    }
                                }

                                if (Voy_Ref0 || Voy_Ref1)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.WriteLine(" To such line below.");
                                        sw.WriteLine(Line0);
                                        sw.WriteLine("");
                                    }

                                }
                            }

                            Pos0 = Line0.IndexOf("LOC+147+");
                            if (Pos0 > -1)
                            {
                                Pos1 = Line0.IndexOf(":", Pos0 + 8);

                                if (Pos1 - Pos0 - 8 > 6)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.WriteLine("Unit Stowage Location in LOC segment is too long; at most only 6 characters are allowed to have, to such line below.");
                                        sw.WriteLine(Line0);
                                        sw.WriteLine("");
                                    }
                                }

                            }

                            Pos0 = Line0.IndexOf("REF+BM:");
                            if (Pos0 > -1)
                            {
                                Pos1 = Line0.IndexOf(":", Pos0 + 7);

                                if (Pos1 - Pos0 - 7 > 17)
                                {
                                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                                    {
                                        sw.WriteLine("BL number is too long; at most only 17 characters are allowed to have, to such line below.");
                                        sw.WriteLine(Line0);
                                        sw.WriteLine("");
                                    }
                                }

                            }

                            //Pos0 = Line0.IndexOf("\n");
                            //if (Pos0 <= 0)
                            //{
                            //    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                            //    {
                            //        sw.WriteLine("No line feed to the line:.");
                            //        sw.WriteLine(Line0);
                            //        sw.WriteLine("");
                            //    }
                            //}

                            //Pos0 = Line0.IndexOf("\r");
                            //if (Pos0 <= 0)
                            //{
                            //    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                            //    {
                            //        sw.WriteLine("No carriage return to the line:.");
                            //        sw.WriteLine(Line0);
                            //        sw.WriteLine("");
                            //    }
                            //}
                        }
                    }
                }

                if (!UNB_Exist)
                {
                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                    {
                        sw.WriteLine("No UNB line exists!");
                        sw.WriteLine("");
                    }

                }

                if (!UNZ_Exist)
                {
                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                    {
                        sw.WriteLine("No UNZ line exists!");
                        sw.WriteLine("");
                    }

                }

                if (!gid_Line && ftx_Line && eqd_Line)
                {
                    using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                    {
                        sw.WriteLine("No GID line is existing prior to FTX and EQD lines below!");
                        sw.WriteLine(ftx_Line1);
                        sw.WriteLine(eqd_Line1);
                        sw.WriteLine("");
                    }

                }

                string text = File.ReadAllText(@File0);
                var matches = Regex.Matches(text, @"(?!$)(?<line>[^\r\n]*)(?<eol>\r?\n?)");
                foreach (Match m in matches)
                {
                    switch (m.Groups["eol"].Value)
                    {
                        case "\r\n":
                            //output.Add("GOOD: " + m.Groups["line"]);
                            //break;
                        case "\r":
                            using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                            {
                                sw.WriteLine("BAD (no LF): " + m.Groups["line"]);
                                sw.WriteLine("");
                            }
                            break;
                        case "\n":
                            using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                            {
                                sw.WriteLine("BAD (no CR): " + m.Groups["line"]);
                                sw.WriteLine("");
                            }
                            break;
                        case "":
                            using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                            {
                                sw.WriteLine("BAD (none): " + m.Groups["line"]);
                                sw.WriteLine("");
                            }
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                {
                    sw.WriteLine("Error happens due to file - '" + File1 + "' with detail:  " + e.StackTrace);
                    sw.WriteLine("");
                }
                
            }
            finally
            {

            }
        }
    }
}

Open in new window

Top Expert 2014
Commented:
Here's why:
                        case "\r\n":
                            //output.Add("GOOD: " + m.Groups["line"]);
                            //break;
                        case "\r":

Open in new window

This doesn't mean "Do nothing if there's \r\n".
It means "Do with \r\n the same thing than with \r".
If you want to do nothing, you have to put a break instruction.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial