Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello I read a line from a text file and is having trouble coming up with a solution to eliminate the last 8 characters to console and reading only certain sentences within the text file.

For instance my text file says

" SAMPLE DATA INFO"
"Aligning the value P0454 uniformed>Circuit Break<caution">
"Aligning the value P0334 uniformed>Line Wire<caution">
"Aligning the value P0123 uniformed>BREAK SYSTEM<caution">
"Aligning the value U0234 uniformed>VALUE DATA<caution">
" UNRELATED DATA"

I just want the info P0454 P0334 P0123 U0234 and everything between uniformed and caution print to console

So my output should be
P0454 Circuit Break
P0334  Line Wire
P0123  BREAK SYSTEM
U0234  VALUE DATA


Right now I have

C#
foreach(string line in filetext)

if line.StartsWith("Aligning")

Console.Writeline(line.substring(20));


But this reads the the number plus the rest of information which I don't want. Appreciate any ideas if possible.
Posted
Updated 25-Sep-15 8:44am
v2
Comments
[no name] 25-Sep-15 14:19pm    
All a "little bit" brute force. To proceed like this, than search for "< caution >" and to determine the "end of the string" you are interesting in.
Member 12009796 25-Sep-15 15:42pm    
I was also thinking using two different substrings to find the positions on the line I want.
BillWoodruff 25-Sep-15 16:17pm    
Sure, you can do this, and you already have good answers here. But, may I suggest that a better long-range solution is to get your data into XML format, and then you can read it into an object-graph.

Learn about, and use, a regular expression (see the Regex[^] class)
It looks like you're going to want a pattern like:
^Aligning the value (\S+) .* >(.+)<

The two "capture groups" of matching lines can be extracted to the output.
First create the Regex:
C#
using System.Text.RegularExpressions;
private readonly static Regex Extractor = new Regex(@"^Aligning the value (\S+) .* >(.+)<", RegexOptions.Compiled);
This can be done once and "compiled" for efficiency.
Then process the lines:
C#
foreach(string line in filetext)
{
  Match m = Extractor.Match(line);
  if (m.Success)
  {
    Console.Writeline("{0} {1}", m.Groups[1], m.Groups[2]);
  }
}

(And, yes, the indexing into Groups is one-based!)
 
Share this answer
 

I'd use Regex with named capture groups like this.


C#
 string text = @""" SAMPLE DATA INFO""
""Aligning the value P0454 uniformed>Circuit Break<caution"">
""Aligning the value P0334 uniformed>Line Wire<caution"">
""Aligning the value P0123 uniformed>BREAK SYSTEM<caution"">
""Aligning the value U0234 uniformed>VALUE DATA<caution"">
"" UNRELATED DATA""";
          Regex regex = new Regex(
     "^\"Aligning the value (?<ReferenceNo>.*?) .*?>(?<Caution>.*?)<",
   RegexOptions.Multiline
   | RegexOptions.CultureInvariant
   | RegexOptions.Compiled
   );
 
            MatchCollection matches = regex.Matches(text);
            foreach (Match match in matches)
            {
                Console.WriteLine(
                    "Reference: {0} Caution : {1}",
                    match.Groups["ReferenceNo"].Value,
                    match.Groups["Caution"].Value);
            }
 
Share this answer
 

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