简体   繁体   中英

Compare two string values in a text file C#

i need help to compare two string values in a text file that are splited by line and also count to that compared string. i have tried some code but not got it. it takes only one value to compare.How to pass second value.

so the Contains Method returning Bool value how i can count the compared values.

Text file like this:

172.16.50.70,[17/Aug/2017:08:36:36,GET /login2.php HTTP/1.0,200,1575,-,"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"

172.16.50.70,[17/Aug/2017:08:36:37,GET /objections/login.php HTTP/1.0,200,1988,-,"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"

39.51.52.189,[17/Aug/2017:08:36:37,GET /objections/css/style.css HTTP/1.1,200,6198,googleusercontent.com/search?q=cache:aiou.edu.pk/objections/ssearch.php,"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.39 Safari/537.36"

CODE

string Files = System.IO.File.ReadAllText(@"C:\info.txt");
string[] SplitByLine = Files.Split('\n');
foreach (var item in SplitByLine)
{
    string[] OneRowSplit = item.Split(',');
    GetIP = OneRowSplit[0];
    LapInfo = OneRowSplit.Last();
    string Filess = System.IO.File.ReadAllText(@"C:\info.txt");
    string[] SplitByLines = Filess.Split('\n');
    string ads = SplitByLines.Contains(GetIP & LapIngo).count(); // this line of code is not valid its only for telling i want some thing like this contain method take only one parameter and i also want to count comapred values
}

Creating a distinct list of IP and browser combinations is just some lines LINQ magic away:

string[] lines = File.ReadAllLines("somefile.txt");
IEnumerable<string> fingerPrints = lines.Select(c => $"{c.Split(",").First()} 
{c.Split(",").Last()}").Distinct();

You now have an enumerable of strings where each entry unique/distinct. You can write these into another file, or use them to update the original file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM