Mutley
Mutley

Reputation: 77

Combine text file lines c#

I'm having difficulty combining some lines in a comma delimited text file if the first field is the same. This is my file:

Area 1 Chiller Grids aB aN and a1 a6,test,1,Ground Floor Framing
Area 1 Chiller Grids aB aN and a1 a6,test,2,Monorails West
Area 1 Chiller Grids aB aN and a1 a6,test,6,Roof Framing West
Area 1 Chiller Grids aB aN and a1 a6,test,11,Bullnose West
Area 2 Office Grids aN zA and a1 a6,101806,3,First Floor Framing East
Area 2 Office Grids aN zA and a1 a6,101806,5,Mezz Floor Framing
Area 2 Office Grids aN zA and a1 a6,101806,7,Roof Framing East
Area 2 Office Grids aN zA and a1 a6,101806,9,First Floor Ceiling
Area 3 Bridge Grids yA yE and a1 10x,101807,4,Link Floor Framing
Area 3 Bridge Grids yA yE and a1 10x,101807,8,Link Roof Framing
Area 3 Bridge Grids yA yE and a1 10x,101807,10,Bridge Catwalks
Area 3 Bridge Grids yA yE and a1 10x,101807,13,Stair 1

I need the output to be this:

Area 1 Chiller Grids aB aN and a1 a6,test,1-2-6-11,Ground Floor Framing-Monorails West-Roof Framing West-Bullnose West
Area 2 Office Grids aN zA and a1 a6,101806,3-5-7-9,First Floor Framing East-Mezz Floor Framing-Roof Framing East-First Floor Ceiling
Area 3 Bridge Grids yA yE and a1 10x,101807,4-8-10-13,Link Floor Framing-Link Roof Framing-Bridge Catwalks-Stair 1

How can I go about this? I've tried some linq code I found on here but it doesn't quite get what I want and I'm not sure how to make it do it:

                var a = System.IO.File.ReadAllLines(@"X:\Steelcad_Project_Files\" + dir + "\\manager1.txt");
                var b = System.IO.File.ReadAllLines(@"X:\Steelcad_Project_Files\" + dir + "\\manager.txt");

                var query =
                    from bline in b
                    let parts = bline.Split(',')
                    group parts[2] by parts[0] into bg
                    join aline in a on bg.Key equals aline
                    select aline + "," + string.Join("-", bg.ToArray());

                System.IO.File.WriteAllLines(@"X:\Steelcad_Project_Files\" + dir + "\\result.txt", query.ToArray());

Where the manager1 file only has the first field in it, like so:

Area 1 Chiller Grids aB aN and a1 a6
Area 2 Office Grids aN zA and a1 a6
Area 3 Bridge Grids yA yE and a1 10x

I would like, if possible, to only use 1 file though. Any help would be much appreciated. Thanks

EDIT: Is it possible to sort the lines based on line 0 and then line 2? If it was 1-11-15-2-6 can it be rearranged to 1-2-6-11-15 but keeping all the Area 1's together etc?

Upvotes: 0

Views: 2606

Answers (2)

Shiv Kumar
Shiv Kumar

Reputation: 9799

Try this

var results = from l in File.ReadAllLines(@"..\..\TextFile1.txt")
              let fields = l.Split(new[] { ',' })
              group fields by fields[0] into g
              select (g.Key + "," +
              String.Join("-", g.Select(l => l[2]).ToArray()) +
              "," +
              String.Join("-", g.Select(l => l[3]).ToArray()));

Upvotes: 0

Elian Ebbing
Elian Ebbing

Reputation: 19057

var output = File.ReadAllLines(@"D:\\manager.txt")
    .Select(line => line.Split(','))
    .GroupBy(line => line[0] + "," + line[1])
    .Select(group =>
         group.Key + "," + 
         String.Join("-", group.Select(line => line[2]).ToArray()) + "," +
         String.Join("-", group.Select(line => line[3]).ToArray())
    );

Upvotes: 3

Related Questions