Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# search string in txt file

Tags:

I want to find a string in a txt file if string compares, it should go on reading lines till another string which I'm using as parameter.

Example:

CustomerEN //search for this string ... some text which has details about the customer id "123456" username "rootuser" ... CustomerCh //get text till this string 

I need the details to work with them otherwise.

I'm using linq to search for "CustomerEN" like this:

File.ReadLines(pathToTextFile).Any(line => line.Contains("CustomerEN")) 

But now I'm stuck with reading lines (data) till "CustomerCh" to extract details.

like image 338
nukleos Avatar asked Oct 12 '12 09:10

nukleos


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


2 Answers

If your pair of lines will only appear once in your file, you could use

File.ReadLines(pathToTextFile)     .SkipWhile(line => !line.Contains("CustomerEN"))     .Skip(1) // optional     .TakeWhile(line => !line.Contains("CustomerCh")); 

If you could have multiple occurrences in one file, you're probably better off using a regular foreach loop - reading lines, keeping track of whether you're currently inside or outside a customer etc:

List<List<string>> groups = new List<List<string>>(); List<string> current = null; foreach (var line in File.ReadAllLines(pathToFile)) {     if (line.Contains("CustomerEN") && current == null)         current = new List<string>();     else if (line.Contains("CustomerCh") && current != null)     {         groups.Add(current);         current = null;     }     if (current != null)         current.Add(line); } 
like image 76
Rawling Avatar answered Oct 01 '22 21:10

Rawling


You have to use while since foreach does not know about index. Below is an example code.

int counter = 0; string line;  Console.Write("Input your search text: "); var text = Console.ReadLine();  System.IO.StreamReader file =     new System.IO.StreamReader("SampleInput1.txt");  while ((line = file.ReadLine()) != null) {     if (line.Contains(text))     {         break;     }      counter++; }  Console.WriteLine("Line number: {0}", counter);  file.Close();  Console.ReadLine(); 
like image 32
Ekk Avatar answered Oct 01 '22 20:10

Ekk