Help splitting file based on matching characters

First let me start out saying I have very very little experience with C# and programming in general. I started a C# course recently and came across some sample code while googling and thought I might be able to manipulate it to work for me.

I have a somewhat large file (270mb) that I'm trying to split into multiple text files based on the starting 3 characters on each line. I've managed to get it to sort and spit the file based on either the first, second, or third character, but I can't get it to match all 3 and output to a text file.

Here is the code I'm using..

namespace SplitFiles { class SplitWithGroups { static void Main() { string[] fileA = System.IO.File.ReadAllLines(@"C:/test/test.txt"); // Group the names var lineCodeQuery = from line in fileA let n = line.Split(',') group line by n[0][0] into g select g; // Create a new file for each line created. foreach (var g in lineCodeQuery) { // Create the new file name. string fileName = @"C:/test/test" + g.Key + ".txt"; // Output to display. Console.WriteLine(g.Key); // Write file. using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName)) { foreach (var item in g) { sw.WriteLine(item); // Output to console for example. Console.WriteLine("{0}", item); } } } // Keep console window open in debug mode. Console.WriteLine("Files have been written. Press any key to exit"); Console.ReadKey(); } } } 

by HenrysFourDoor via /r/csharp

Leave a Reply