Last active
March 5, 2021 01:02
-
-
Save nathan130200/0696b569e233ad31f2b4c1b3de862feb to your computer and use it in GitHub Desktop.
Line Text Comparision Library for TSV files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var lines1 = File.ReadAllLines(args[0]); | |
var lines2 = File.ReadAllLines(args[1]); | |
var mode = args.Length >= 3 ? args[2] : string.Empty; | |
if (!string.IsNullOrEmpty(mode) && mode.StartsWith("--")) | |
mode = mode.Substring(2); | |
var isReverse = args.Length >= 4 ? (bool.TryParse(args[3], out var _isReverse) ? _isReverse : | |
(args[3]?.Equals("--reverse") == true || args[3]?.Equals("-r") == true)) : false; | |
if (isReverse) | |
{ | |
var temp = lines2; | |
lines2 = lines1; | |
lines1 = temp; | |
} | |
switch (mode) | |
{ | |
case "intersect": | |
Dump(lines1.Intersect(lines2)); | |
break; | |
case "except": | |
Dump(lines1.Except(lines2)); | |
break; | |
case "exclude": | |
{ | |
var result = lines1.Concat(lines2) | |
.Distinct(); | |
var temp = new List<string>(); | |
foreach (var item in lines1) | |
{ | |
if (!lines2.Any(x => x.Equals(item, StringComparison.OrdinalIgnoreCase))) | |
temp.Add(item); | |
} | |
foreach (var item in lines2) | |
{ | |
if (!lines1.Any(x => x.Equals(item, StringComparison.OrdinalIgnoreCase))) | |
temp.Add(item); | |
} | |
Dump(temp.Distinct()); | |
} | |
break; | |
default: | |
{ | |
var result = new List<string>(); | |
foreach (var line1 in lines1) | |
{ | |
foreach (var line2 in lines2) | |
{ | |
if (!line1.Contains(line2) || !line1.Equals(line2, StringComparison.OrdinalIgnoreCase)) | |
{ | |
if (!result.Contains(line1)) | |
result.Add(line1); | |
} | |
else if (!line2.Contains(line1) || !line2.Equals(line1, StringComparison.OrdinalIgnoreCase)) | |
{ | |
if (!result.Contains(line2)) | |
result.Add(line2); | |
} | |
} | |
} | |
Dump(result); | |
} | |
break; | |
} | |
} | |
static void Dump<T>(IEnumerable<T> collection) | |
{ | |
foreach (var item in collection) | |
Console.WriteLine(item); | |
} | |
static void Dump<T>(List<T> collection) | |
{ | |
foreach (var item in collection) | |
Console.WriteLine(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment