Reputation: 302
I have the following text contents (save in a .txt file)
Region2 Region3 Region4 Region5 Region6
Mod Fwd Dir TOP SYS SECURITY ZONING BOTTOM FCC DIS FCC ENA
Eng Use/Total Use/Total Use/Total Use/Total Use/Total Use/Total
--- --- ------ ---------- --------- ------------ --------- --------- ---------
1 1 INPUT 20/407 1/407 0/2852 8/407 0/0 0/0
1 1 OUTPUT 0/25 0/25 0/140 0/25 0/12 1/25
1 2 INPUT 20/407 1/407 0/2852 8/407 0/0 0/0
1 2 OUTPUT 0/25 0/25 0/140 0/25 0/12 1/25
1 3 INPUT 20/407 1/407 0/2852 4/407 0/0 0/0
1 3 OUTPUT 0/25 0/25 0/140 0/25 0/12 1/25
What I Need to get is the zoning details only and put that in another text file so the end result should look like this
Region3 - Zoning
Use/Total
0/2852
0/140
0/2852
0/140
0/2852
0/140
I am struggling to read the format correctly any help would be appreciated.
Upvotes: 0
Views: 138
Reputation: 302
Ok so a new day and a fresh mind, thanks for all your comments. This is how I resolved the issue (the thing here is that it will take into account if the zoning column moves to a different location)
Here is the code
int headerCount = 1;
int zoneNumber = 0;
string[] lines = File.ReadAllLines(@"C:\temp\tcam\test.txt");
foreach (string line in lines)
{
headerCount++;
if (headerCount == 6)
{
string[] zonelocation = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
int zoneCount = 0;
string lastVal = "";
foreach (var x in zonelocation)
{
zoneCount++;
if (lastVal.ToLower() == "top" && x.ToLower() == "sys") zoneCount--;
if (x.ToLower() == "zoning") zoneNumber = zoneCount - 1;
lastVal = x;
}
}
if (headerCount > 8)
{
string[] zoneinfo = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
if (zoneinfo.Any())
{
string[] zoneData = zoneinfo[zoneNumber].Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"Fwd Eng {zoneinfo[1]}, Direction {zoneinfo[2]}, Used {zoneData[0]}, Total {zoneData[1]}");
}
}
}
Here is the output
Fwd Eng 1, Direction INPUT, Used 0, Total 2852
Upvotes: 0
Reputation: 771
To read all the lines from the file use File.ReadAllLines.
If the format is always the same, I would skip the header, using the Linq extension Skip(). Then split the fields on space with this override of String.Split() that allow to filter out empty strings.
like:
foreach (var line in File.ReadLines("yourfile.txt").Skip(4))
{
var fields = line.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries);
//do something with the fields
}
In alternative if a tab separator is used, you could use: line.Split('\t');
Then you retrieve the field you need from the fields array, so for the 6th field:
var myField = fields[5]
You can put all the lines you have to write to a string array (or list), then write everything with File.WriteAllLines:
File.WriteAllLines("output.txt", myOutputLines);
Complete solution
Here is a possible solution that put all together using LINQ and IEnumerables, to streamline the file conversion without loading everything into memory (to allow to process efficiently large files)
var inputLines = File.ReadLines("yourfile.txt").Skip(4);
var outputHeader = new[] { "Region3 - Zoning", "Use/Total" };
var outputLines = inputLines.Select(line => line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)[5]);
File.WriteAllLines("output.txt", outputHeader.Concat(outputLines));
Upvotes: 1