C# Combining lines

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-04-07T14:16:43Z Indexed on 2010/04/07 14:33 UTC
Read the original article Hit count: 217

Filed under:
|

Hey everybody, this is what I have going on. I have two text files. Umm lets call one A.txt and B.txt.

A.txt is a config file that contains a bunch of folder names, only 1 listing per folder.

B.txt is a directory listing that contains folders names and sizes. But B contains a bunch of listing not just 1 entry.

What I need is if B, contains A. Take all lines in B that contain A and write it out as A|B|B|B ect....

So example:

A.txt:
Apple
Orange
Pear XBSj
HEROE

B.txt:
Apple|3123123
Apple|3434
Orange|99999999
Orange|1234544
Pear|11
Pear|12
XBSJ|43949
XBSJ|43933

Result.txt:
Apple|3123123|3434
Orange|99999999|1234544
Pear|11|12
XBSJ|43949|43933

This is what I had but it's not really doing what I needed.

string[] combineconfig = File.ReadAllLines(@"C:\a.txt");
        foreach (string ccline in combineconfig)
        {
            string[] readlines = File.ReadAllLines(@"C:\b.txt");
            if (readlines.Contains(ccline))
            {
                foreach (string rdlines in readlines)
                {
                    string[] pslines = rdlines.Split('|');
                    File.AppendAllText(@"C:\result.txt", ccline + '|' + pslines[0]);
                }
            }

I know realize it's not going to find the first "if" because it reads the entire line and cant find it. But i still believe my output file will not contain what I need.

© Stack Overflow or respective owner

Related posts about c#

Related posts about text-processing