I want to skip lines that i will read in the text file, as you can see below the line 0, line 1 and line 2 is skip (first three lines of text file) then it follows a read this (3 read this) then next is (4 skip this)
this is the the pattern:
(first 3 lines of text file is skipped then followed by (3) read this then (4) skip this till the end of text file) rinse and repeat for other text files, i need help with this kind of pattern.
Line 0 - Skip This
Line 1 - Skip This
Line 2 - Skip This
Line 3 - Read This
Line 4 - Read This
Line 5 - Read This
Line 6 - Skip This
Line 7 - Skip This
Line 8 - Skip This
Line 9 - Skip This
Line 10 - Read This
Line 11 - Read This
Line 12- Read This
Line 13 - Skip This
Line 14 - Skip This
Line 15- Skip This
Line 16- Skip This
Line 17 - Read This and so on
Code that i tried.
string[] Lines = File.ReadAllLines(f.File);
foreach (string line in Lines.Skip(3))
{
try
{
string[] readLineSplit = line.Split('|');
if (readLineSplit.Length > 1)
{
var id = readLineSplit[0].ToString();
var flagvalue = readLineSplit[1].ToString();
var status = readLineSplit[2].ToString();
Lines.Skip(4).ToString();
Console.WriteLine("ID {0}, Value {1}, Status {2}", id, flagvalue, status)
}
}
catch (FileNotFoundException error)
{
Console.WriteLine(error);
}
}
Rather than reading in all the lines and then attempting to extract the ones that you need, you can instead just read the lines that you want without storing the unneeded ones in memory:
var lines = new List<string>();
var counter = 0;
using (var sr = new StreamReader(filename)) {
string line;
while ((line = sr.ReadLine()) != null) {
//ignore the first 3 lines
if (counter >= 3) {
//now work out where in the pattern of 7 (3 read, 4 skip) we are
var mod = (counter - 3) % 7;
//if the first 3 of the 7, keep it
if (mod < 3)
lines.Add(line);
}
counter++;
}
}