score:0

Accepted answer

Here is what I came up with:

public static string TransformLog(string fileName)
{
    const string tab = "\t";

    var fileLines = File.ReadAllLines(fileName);

    var testAreas = fileLines
        .Where(l => l.StartsWith("RECORD" + tab))
        .Select(l => l.Split(tab.ToCharArray()).Skip(1).Take(3).ToArray())
        .GroupBy(l => l[0])
        .Select(g => new { g.Key, Enumerator = g.GetEnumerator() })
        .ToList();

    var sb = new StringBuilder();

    testAreas.ForEach(ta => sb.Append(ta.Key + tab + tab));

    sb.AppendLine();

    bool cont;

    do
    {
        cont = false;

        testAreas.ForEach(ta =>
                              {
                                  var hasNext = ta.Enumerator.MoveNext();
                                  sb.Append((hasNext ? ta.Enumerator.Current[1] + tab + ta.Enumerator.Current[2] + tab : tab + tab));
                                  cont |= hasNext;
                              });

        sb.AppendLine();

    } while (cont);

    return sb.ToString();
}

score:2

You can get the results in rows in one go with Linq (here I'm using the method notation):

string[] lines = File.ReadAllLines("input.txt");
var result =
    lines.Where(line => line.Substring(0, 6) == "RECORD")
         .Select(line => line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
         .GroupBy(columns => columns[1],
                  columns => columns[2] + "/" + columns[3])
         .Select(group => group.Key + " " + string.Join(", ", group.ToArray()));

Output:

DEVON 1/6748, 2/9123, 3/3723, 4/3732, 5/3723, 6/3723
JASON 1/7436, 2/9123, 3/9123
SHERRIE 1/6434, 2/6434, 3/9123

I think it's difficult to transpose the rows to columns without a standard Zip function though. Maybe this is good enough for you? If not, then you will probably have to do the last bit with a helper method that iterates over the separate IEnumerables.


Related Articles