Monday, February 24, 2020

search text file using c# and display the line number and the complete line that contains the search keyword


Sample Input:

just a sample line
another sample line
first matching Order line
not a match
...here's a Order  match
Order 123

foreach (var match in File.ReadLines(@"c:\LogFile.txt")
                          .Select((text, index) => new { text, lineNumber = index+ 1 })
                          .Where(x => x.text.Contains("Order ")))
{
    Console.WriteLine("{0}: {1}", match.lineNumber, match.text);
}



Output:

3: first matching Order line
5: ...here's a Order  match
6: Order 123

No comments:

Change default Port on Next.js app

 If any other app or process is running on port 3000 , you will get this error in your terminal Port 3000 is already in use. error Command f...