我试图解析具有分配地址和解除分配地址的文本文件
示例文件
02/01/18:09:36:48[INFO]::Allocation (FUNC)Get the values of Items -> Item :233272976
02/01/18:09:36:48[INFO]::Allocation (FUNC)Get the values of Items -> Item :233273480
02/01/18:09:36:48[INFO]::Allocation (FUNC)Get the values of Items -> Item :233273648
02/01/18:09:36:49[INFO]::Allocation (FUNC)Get the values of Items -> Item :233270456
02/01/18:09:36:49[INFO]::Allocation (FUNC)Get the values of Items -> Item :233270120
02/01/18:09:36:49[INFO]::Allocation (FUNC)Get the values of Items -> Item :233273816
02/01/18:09:36:49[INFO]::Allocation (FUNC)Get the values of Items -> Item :233273984
02/01/18:09:36:49[INFO]::Allocation (FUNC)Get the values of Items -> Item :233274152
02/01/18:09:36:49[INFO]::Allocation (FUNC)Get the values of Items -> Item :233269952
02/01/18:09:36:49[INFO]::Allocation (FUNC)Get the values of Items -> Item :233270624
02/01/18:09:36:50[INFO]::Allocation (FUNC)Get the values of Items -> Item :233270288
02/01/18:09:36:50[INFO]::Allocation (FUNC)Get the values of Items -> Item :233274992
02/01/18:09:36:50[INFO]::Allocation (FUNC)Get the values of Items -> Item :195645608
02/01/18:09:36:51[INFO]::Allocation (FUNC)Get the values of Items -> Item :195645944
02/01/18:09:36:51[INFO]::Allocation (FUNC)Get the values of Items -> Item :195646616
02/01/18:09:36:51[INFO]::Allocation (FUNC)Get the values of Items -> Item :195646952
02/01/18:09:36:51[INFO]::Allocation (FUNC)Get the values of Items -> Item :195648128
02/01/18:09:36:54[INFO]::Allocation (FUNC)Get the values of Items -> Item :195638384
02/01/18:09:36:54[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :233272976
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :233270120
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :233273984
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195646112
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195648800
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195647288
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195644096
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195646448
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195644600
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195646784
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195646280
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195648632
02/01/18:09:36:55[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195648128
02/01/18:09:37:00[INFO]::Deallocation (FUNC)Delete the values of Item -> Item :195638384
这是我试图解析的程序
class ReadFromFile1
{
static Dictionary<int, int> IndexesTobeRemoved;
static List<string> linesList;
static string strFileName = @"C:\LogFile.Log";
static void Main()
{
linesList = File.ReadAllLines(strFileName).ToList();
IndexesTobeRemoved = new Dictionary<int, int>();
bool bRet = false;
int iAllocationIndex = 0;
do
{
bRet = RepeatIfMatchFound(ref iAllocationIndex);
} while (bRet);
foreach (KeyValuePair<int, int> item in IndexesTobeRemoved)
{
linesList[item.Value] = " ";
}
File.WriteAllLines(strFileName, linesList.ToArray());
}
private static bool RepeatIfMatchFound(ref int iAllocationIndex)
{
bool bRet = false;
for (; iAllocationIndex < linesList.Count - 1; ++iAllocationIndex )
{
int iTempIndex = iAllocationIndex;
if (IndexesTobeRemoved.ContainsValue(iTempIndex)) continue;
string line = linesList[iAllocationIndex];
string strAllocationLine = line;
string strPointerId = string.Empty;
// Search For Allocation
if (line.Contains("Allocation"))
{
int iIndex = line.IndexOf("Item :");
strPointerId = line.Substring(iIndex + 6, line.Length - 1 - (iIndex + 5));
for (int iDeAllocationIndex = iAllocationIndex; iDeAllocationIndex < linesList.Count - 1; ++iDeAllocationIndex)
{
if (IndexesTobeRemoved.ContainsValue(iDeAllocationIndex)) continue;
string strDeallocationLine = linesList[iDeAllocationIndex];
if (strDeallocationLine.Contains("Deallocation"))
{
if (strDeallocationLine.Contains(strPointerId))
{
Console.WriteLine("Found {0} AllocIndex {1} DeAllocIndex {2}", strPointerId, iAllocationIndex+1, iDeAllocationIndex+1);
IndexesTobeRemoved.Add(iAllocationIndex, iAllocationIndex);
IndexesTobeRemoved.Add(iDeAllocationIndex, iDeAllocationIndex);
bRet = true;
break;
}
}
}
}
if (bRet) break;
}
return bRet;
}
}
关于解析 4 MB 文件,花了6个多小时才继续,我在这里遗漏了什么?
感谢您的帮助。
答案 0 :(得分:0)
感谢他LasseVågsætherKarlsen的gist link,重新编写了该程序,速度要快得多。
public struct stReferenceMatch
{
public bool bAllocated;
public bool bDeAllocated;
}
class ReadFromFile1
{
static string strFileName = @"C:\LogFile.Log";
static void Main()
{
List<string> linesList = File.ReadAllLines(strFileName).ToList();
Dictionary<int, stReferenceMatch> IndexesTobeRemoved = new Dictionary<int, stReferenceMatch>();
foreach (string line in linesList)
{
bool bAlloc, bDeAlloc;
FindAllocDeAllocInString(line, out bAlloc, out bDeAlloc);
if (bAlloc || bDeAlloc)
{
int iIndex = line.IndexOf("Item :");
string strPointerId = line.Substring(iIndex + 6, line.Length - 1 - (iIndex + 5));
int iPos = Convert.ToInt32(strPointerId);
IndexesTobeRemoved.TryGetValue(iPos, out stReferenceMatch refMatch);
if (bAlloc) refMatch.bAllocated = true;
if (bDeAlloc) refMatch.bDeAllocated = true;
}
}
using (var writer = new StreamWriter(@"C:\output_new.txt"))
{
foreach (var line in linesList)
{
bool bAlloc, bDeAlloc;
FindAllocDeAllocInString(line, out bAlloc, out bDeAlloc);
if (bAlloc || bDeAlloc)
{
int iIndex = line.IndexOf("Item :");
string strPointerId = line.Substring(iIndex + 6, line.Length - 1 - (iIndex + 5));
int iPos = Convert.ToInt32(strPointerId);
IndexesTobeRemoved.TryGetValue(iPos, out stReferenceMatch refMatch);
if (refMatch.bAllocated && refMatch.bDeAllocated)
continue;
}
else
{
writer.WriteLine(line);
}
}
}
}
private static bool FindAllocDeAllocInString(string line, out bool bAlloc, out bool bDeAlloc)
{
// Search For Allocation & DeAllocation
bAlloc = line.Contains("Allocation");
bDeAlloc = line.Contains("Deallocation");
return bAlloc || bDeAlloc;
}
}