You need to parse a file, so, the starting point is not LINQ (this is implementation detail), but rather defining the grammar of the file to the needed detail level to allow implememting a good-enough parser.
The grammar in your case looks to me somethin like:
(
{...}
= 0-n repetitions,
[...]
= optional,
a | b
= a or b,
'...'
= literal text)
file : { line } .
line : { ws } [ input | output | pin | data ] rest .
rest : [ comment ] EOL .
comment : { ws } '#' { NOT_EOL } .
ws : SPACE_NOT_EOL .
input : 'input' { ws } '=' { ws } number .
output : 'output' { sw } '=' { ws } number .
pin : 'pin' ws { ws } 'class' ws { ws } 'direction' ws { ws } 'no' .
data : word ws { ws } number ws { ws } word ws { ws } number .
number : DIGIT { DIGIT } .
word : WORDCHAR { WORDCHAR } .
Implementing:
Regex scan = new Regex(
@"^\s*(?:(\w+)\s*(?:=\s*\w+|(\w+)\s+(\w+)\s+(\w+)))?\s*(?:[#].*)?$",
RegexOptions.Multiline);
var lines = scan.Matches(File.ReadAllText(@"..\..\data.txt"))
.Cast<Match>()
.Where(m=>m.Groups[1].Success)
foreach (var m in lines)
{
switch (m.Groups[1].Value)
{
case "input": case "output": case "pin":
break;
default:
Console.WriteLine("{0,-16} {1,4} {2,-6} {3,4}",
m.Groups[1].Value,
m.Groups[2].Value,
m.Groups[3].Value,
m.Groups[4].Value);
break;
}
}
With input file
# Header
input = 12
output = 4
# Data
pin class direction no # Data Title
io 1 up 0
io 3 rught 1
cb 6 up 2
io 1 up 0
# End of data
Results in
io 1 up 0
io 3 rught 1
cb 6 up 2
io 1 up 0
Cheers
Andi