簡體   English   中英

如何在txt文檔中搜索單詞並將該單詞寫到新文檔中進行報告?

[英]How do I search a txt document for a word and write that word to a new document for reporting?

我正在嘗試瀏覽日志.txt文件並查找錯誤消息的所有實例,然后使用C#將代碼將該錯誤行粘貼到新文檔中,並從本質上創建一個單獨的.txt文件,其中僅包含來自列出的日志文件。

我了解如何使用C#搜索文檔中的文本,但是提取這些錯誤消息(通常不超過1-2行)而又不將整個文檔的其余部分追加的最佳方法是什么?第一個錯誤實例?

編輯

日志文件在每行記錄事件,錯誤行的讀取方式如下:

Running install update (3)
ERROR: Running install update(3) (ERROR x34293) The system was unable to update the application, check that correct version is accessible by the system.
Running install update (4)

等等

請讓我知道這可不可以幫你。

就像是:

foreach(var line in File.ReadLines(filename)
                        .Where(l => l.Contains("ERROR MESSAGE")))
{
    // Log line
}

另外,如果您需要在線中的特定信息,則可以使用Regex來捕獲信息。 沒有更多信息,我無法提供更好的示例。

您可以通過RegEx模式搜索文件,該模式可以找到字符位置或行號(不記得了)。 然后,您可以獲取從RegEx返回的那部分。

這是我編寫的使用RegEx的“查找和替換”程序的代碼塊。 有一些嵌套的方法,但是您明白了……

int foundInThisFile;
string regExPattern = FindText;
System.Text.RegularExpressions.Regex regSearch = null;

if (IgnoreCase)
    regSearch = new System.Text.RegularExpressions.Regex(regExPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
else
    regSearch = new System.Text.RegularExpressions.Regex(regExPattern, System.Text.RegularExpressions.RegexOptions.Multiline);

            System.Text.RegularExpressions.MatchCollection regExMatches = regSearch.Matches(reader.ReadToEnd());

            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            found += regExMatches.Count;
            TotalMatches(new CountEventArgs(found));

            foundInThisFile = regExMatches.Count;
            MatchesInThisFile(new CountEventArgs(foundInThisFile));

            if (regExMatches.Count > 0)
            {
                foreach (System.Text.RegularExpressions.Match match in regExMatches)
                {
                    // The first "group" is going to be the entire regex match, any other "group" is going to be the %1, %2 values that are returned
                    // Index is the character position in the entire document
                    if (match.Groups.Count > 1)
                    {
                        // This means the user wants to see the grouping results
                        string[] groupsArray = new string[match.Groups.Count - 1];

                        for (int counter = 1; counter < match.Groups.Count; counter++)
                            groupsArray[counter - 1] = match.Groups[counter].Value;

                        int lineNumber = 0;
                        string actualLine = String.Empty;

                        GetLineNumberAndLine(localPath, match.Groups[0].Index, out lineNumber, out actualLine);

                        AddToSearchResults(localPath, lineNumber, actualLine, groupsArray);

                        NewSearchResult(new SearchResultArgs(new FindReplaceItem(localPath, lineNumber, actualLine, ConvertGroupsArrayToString(groupsArray))));
                    }
                    else
                    {
                        int lineNumber = 0;
                        string actualLine = String.Empty;

                        GetLineNumberAndLine(localPath, match.Groups[0].Index, out lineNumber, out actualLine);

                        AddToSearchResults(localPath, lineNumber, actualLine);

                        NewSearchResult(new SearchResultArgs(new FindReplaceItem(localPath, lineNumber, actualLine)));
                    }
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM