Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3635174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:52:15+00:00 2026-05-19T00:52:15+00:00

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

  • 0

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; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:52:16+00:00Added an answer on May 19, 2026 at 12:52 am

    There might be a simpler solution, but this is at least working if you really want to use LINQ:

    var foldercontent = Directory.GetFiles(pathA)
                        .Select(filename => new
                        {
                            Filename = filename,
                            Lines = File.ReadAllLines(filename)
                        })
                        .SelectMany(file => file.Lines.Select((line, idx) => new
                        {
                            LineNumber = idx + 1,
                            Text = line,
                            FileName = file.Filename
                        }));
    
    var pattern = File.ReadAllLines(pathB);
    
    var result = from fileLine in foldercontent
                 where pattern.Any(p => fileLine.Text.IndexOf(p, StringComparison.CurrentCultureIgnoreCase) != -1)
                 select fileLine;
    
    foreach (var match in result)
    {
        System.Diagnostics.Debug.WriteLine("File: {0} LineNo: {1}: Text: {2}", match.FileName, match.LineNumber, match.Text);
    }
    

    Or if you want, you can combine that into one LINQ query (but thats not very readable i think):

    var result = from fileLine in (Directory.GetFiles(pathA)
                        .Select(filename => new
                        {
                            Filename = filename,
                            Lines = File.ReadAllLines(filename)
                        })
                        .SelectMany(file => file.Lines.Select((line, idx) => new
                        {
                            LineNumber = idx + 1,
                            Text = line,
                            FileName = file.Filename
                        })))
                    where File.ReadAllLines(pathB).Any(p => fileLine.Text.IndexOf(p, StringComparison.CurrentCultureIgnoreCase) != -1)
                    select fileLine;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a text file like this one: http://www.gutenberg.org/files/17921/17921-8.txt Does anyone has
I have one folder named test located at www.mydomain.com/abc/files/test. I need to change the
I have hashmap and its keys are like folder/1.txt,folder/2.txt,folder/3.txt and value has these text
In a project we have text files looking like this: mv A, R3 mv
For testing purposes I need to create sets of text files that have similar
I have many text files in a folder. What I can do now is
Hey everybody, this is what I have going on. I have two text files.
I have two text files bala.txt and bala1.txt bala.txt contains text line by line
I have two cgi files in the same cgi-bin folder. One complex 300hundread lines
I'm desperately trying to solve this one. I have a bunch of files stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.