compare two text files using linq?

Posted by bala3569 on Stack Overflow See other posts from Stack Overflow or by bala3569
Published on 2010-12-21T12:52:14Z Indexed on 2010/12/21 13:54 UTC
Read the original article Hit count: 232

Filed under:
|

I have 4 text files in one folder and a pattern.txt to compare these text files..In pattern.txt i have

insert
update
delete
drop

I need to compare this text file with those four text files and if these patterns matches any line in that text files i have to write those lines in another log file...i had read those files using linq..i need to compare those files and write in a text file with line number..here is my code

var foldercontent = Directory.GetFiles(pathA)
                    .Select(filename => File.ReadAllText(filename))
                    .Aggregate(new StringBuilder(),
                    (sb, s) => sb.Append(s).Append(Environment.NewLine),
                    sb => sb.ToString());

 var pattern =  File.ReadAllLines(pathB).Aggregate(new StringBuilder(),
                    (sb, s) => sb.Append(s).Append(Environment.NewLine),
                    sb => sb.ToString());

using (var dest = File.AppendText(Path.Combine(_logFolderPath, "log.txt")))
            {
      //dest.WriteLine("LineNo : " + counter.ToString() + " : " + "" + line);
            }

EDIT I have already used c# to compare two text files but i need this in linq

while ((line = file.ReadLine()) != null)
{
if (line.IndexOf(line2, StringComparison.CurrentCultureIgnoreCase) != -1)
{
dest.WriteLine("LineNo : " + counter.ToString() + " : " + " " + line.TrimStart());
}
counter++;
}
file.BaseStream.Seek(0, SeekOrigin.Begin);
counter = 1; 

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ