Reputation: 37
I've got two files
"Database.txt" contains the following names:
"Slave.txt" contains the following names:
Cat
Panda
I want to compare the "Slave.txt" with "Database.txt" and create third file with:
2. Cat 4. Panda
(Cat and Panda from Slave.txt find in Database.txt)
My code:
static void Main(string[] args)
{
String directory = @"C:\Users\user\Desktop\";
String[] linesA = File.ReadAllLines(Path.Combine(directory, "Database.txt"));
String[] linesB = File.ReadAllLines(Path.Combine(directory, "Slave.txt"));
IEnumerable<String> onlyB = linesB.Intersect(linesA);
File.WriteAllLines(Path.Combine(directory, "Result.txt"), onlyB);
}
works only on Database.txt structure like:
Dog
Cat
Mouse
Panda
Bear
without line numbers. Is there somethink instead .Intersect to find only part of string, not full string?
Upvotes: 3
Views: 2229
Reputation: 1731
you can use Linq like this:
static void Main(string[] args)
{
String directory = @"C:\Users\user\Desktop\";
String[] linesA = File.ReadAllLines(Path.Combine(directory, "Database.txt"));
String[] linesB = File.ReadAllLines(Path.Combine(directory, "Slave.txt"));
IEnumerable<String> onlyB = linesA.Where(x=>linesB.Contains(x.Substring(x.IndexOf(". "+1))));
File.WriteAllLines(Path.Combine(directory, "Result.txt"), onlyB);
}
Upvotes: 1
Reputation: 13209
A very simple approach is with Any
from Linq. It is only checking if any part of a line in B is contained in any line of A no matter the case.
var onlyB = linesA.Where(a => linesB.Any(b => a.ToLower().Contains(b.ToLower())));
Note: Updated to shows lines from A instead of lines from B.
Upvotes: 4
Reputation: 15616
Here's a test method that I wrote and tested:
private string[] Matcher()
{
string[] file1 = { "1. Dog","2. Cat","3. Mouse","4. Panda","5. Bear" };
string[] file2 = { "Cat", "Panda" };
string[] file3 = file1.Where(d => {
foreach(string matcher in file2)
{
if(Regex.Match(d, @"^\d+\.\s+"+matcher + "$").Length > 0)
{
return true;
}
}
return false;
}).ToArray<string>();
return file3;
}
I suppose you have line or item numbers before the records on file1. This will try to match for : a number combination, a dot and the desired values with a regular expression, and when it matches an element on the list, it'll take that element to the file3
array.
And it will discard Sabre Cat when you are searching only for Cat.
Upvotes: 0