tags:

views:

89

answers:

5

UPDATE:

I did not realise that Thomas Levesque had posted a solution and worked on my own below one in the meantime. I’m not using C# / VS2010 for long but I must say that C# is unbelievably productive and I am kicking myself that I stuck with C++ and put off learning C# for so long.

 Stack<string> LdifUserStack = new Stack<string>();
          String LdifValidUser = null;

            var query2 = File.ReadAllLines(args[1]).ToList();

            foreach (var item in query2)
            {

                if ( item.Contains("Fullname") ) continue;

                LdifValidUser += item + System.Environment.NewLine;

                if ( LdifValidUser.Contains("GivenName:") && LdifValidUser.Contains("SN:") )
                {
                    LdifUserStack.Push(LdifValidUser);
                    LdifValidUser = null;
                }

                else if ( string.IsNullOrEmpty(item) )
                {
                    LdifValidUser = null;
                }

            }

END UPDATE:

I have a simple ldif file and I am trying to split on /r/n/r/n but no joy, whatever combination i use the split always occurs on each line instead.

  var query2 =

           from line in File.ReadAllLines(args[1])
           let LDIFRecord = line.Split(new string[] { "\r\n\r\n"},StringSplitOptions.None) 
           select LDIFRecord;

            foreach (var item in query2)
            {
                //do something


            }

 //Also tried line.Split(new string[] {"'\r\n','\r\n'"},StringSplitOptions.None)
 // sample ldif file
    dn: cn=Admin1,ou=abt,o=cach
    changetype: modify
    GivenName: Admin
    Fullname: Administrator for abt Server
    SN: Admin
    CN: Admin

    dn: cn=admin,ou=ters,o=cach
    changetype: modify
    GivenName: Administrator
    Fullname: cach Administrator
    SN: admin
    CN: admin

    dn: cn=Supervisor,o=cach
    changetype: modify
    SN: Supervisor
    CN: Supervisor
+1  A: 

File.ReadAllLines already splits the file into lines, so the individual lines don't contain "\r\n" sequences... What are you trying to do exactly ?


EDIT: OK, here's a solution

In your files, records are separated by a blank line. File.ReadAllLines returns all lines from the files, but doesn't separate the records. So you just need to "split" the array of lines based on blank lines.

Here's an extension method to split a sequence of items based on an arbitrary criteria:

public static class Extensions
{
    public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, Func<T, bool> isSeparator)
    {
        var list = new List<T>();
        foreach (var item in source)
        {
            if (isSeparator(item))
            {
                if (list.Count > 0)
                {
                    yield return list.AsReadOnly();
                }
                list = new List<T>();
            }
            else
            {
                list.Add(item);
            }
        }
        if (list.Count > 0)
        {
            yield return list.AsReadOnly();
        }
    }
}

In your case, you can use it like this:

var records = File.ReadAllLines(args[1]).Split(line => String.IsNullOrWhiteSpace(line));

Which can be shortened to:

var records = File.ReadAllLines(args[1]).Split(String.IsNullOrWhiteSpace);

Now, if you want to filter the records to keep only the ones that have the GivenName and SN attributes, you just have to use Where:

var records = File.ReadAllLines(args[1])
                  .Split(String.IsNullOrWhiteSpace)
                  .Where(rec => rec.Any(s => s.StartsWith("GivenName:")
                             && rec.Any(s => s.StartsWith("SN:"));
Thomas Levesque
Canacourse
See my updated answer for a solution
Thomas Levesque
@Thomas Levesque - Thanks for explanation and solution.
Canacourse
A: 

Why don't you use string.Trim() and check if the line is string.IsNullOrEmpty() and work on that premise instead of searching for the carriage return and new line, which you cannot guarantee will alway be as you expect depending on where the data comes from.

You can then start a new 'record' where you find a blank line.

Monkieboy
A: 

I see you will need to keep the file info into array of stuct of record. So why don't read each line in a loop fills a record's struct with 6 lines when skip 2 others (/r/r) and repeat it again?

Arseny
+1  A: 

Have you tried using the System.Environment.NewLine property instead of relying on an explicit '\r' and '\n'?

M_J_O_N_E_S
A: 

Try like this var query2 = File.ReadAllLines(args[1]).ToList();

Pramodh
This still resulted in query2 containing seperate lines.
Canacourse
ReadAllLines will splits the file into lines
Pramodh