Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a text file and I just want to grab the lines of the file that ends with a certain character such as "<\a>" I am struggling with the syntax appreciate any help.

So if the there is a sentence in the file such as => " VALUE123 is good <\a>"
it will print that to console because it ends in <\a>




This is a sample text code.
In this file there are special lines to be extracted <\a>
Only special characters are to be extracted.
Select the lines of code please <\a>
This is a test <\a>


So the file would be read and then the following lines:

In this file there are special lines to be extracted <\a>
Select the lines of code please <\a>
This is a test <\a>

Would be written to console because the line ends in <\a>


Im thinking something like
string line = File.ReadAllText("Sample.txt");
if ( line.Contains(<\a>))
//code to write only selected information to console
Posted
Updated 24-Sep-15 13:32pm
v3
Comments
PIEBALDconsult 24-Sep-15 16:20pm    
I was going to say "RegularExpressions" until I saw that you are looking at HTML.
http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html
Member 12009796 24-Sep-15 16:26pm    
Okay thanks I will have a look.
Zoltán Zörgő 24-Sep-15 16:25pm    
It is not quite clear. Please add some sample to your post before you get some unusable answers.
Member 12009796 24-Sep-15 16:35pm    
I have given an example if that makes things more clearer
Zoltán Zörgő 24-Sep-15 16:59pm    
A more complex example please. Paste several lines from a real source and highlight (using formatting for example) what you want to extract.

1 solution

C#
foreach (string line in File.ReadLines("Sample.txt"))
{
  if (line.EndsWith("<\a>"))
    Console.WriteLine(line);
}

or
C#
foreach (string line in File.ReadLines("Sample.txt").Where(line => line.EndsWith("<\a>")))
{
  Console.WriteLine(line);
}

or (a bit forced):
C#
File.ReadLines("Sample.txt").Where(line => line.EndsWith("<\a>")).All(l => { Console.WriteLine(l); return true; });


But the inclusion test could be more sophisticated depending on how in-exactly the <\a> must be matched.
 
Share this answer
 
Comments
George Jonsson 24-Sep-15 22:27pm    
This solution should do the job. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900