2

我有一个包含大约 100K 行的文本文件。现在我想从文本文件中搜索一个字符串。如果该字符串存在,那么我想获取它存在的行号。最后,我需要文本文件中所有出现的带有行号的字符串。

* 尝试的普通方法 * 我们可以逐行读取整个文本文件。保持每次读取后增加的计数器变量。如果我找到了我的字符串,那么我将返回计数器变量。这种方法的局限性在于,我必须逐个遍历所有 100K 行来搜索字符串。这会降低性能。

* 快速方法(需要帮助)* 有什么方法可以直接将我带到我的搜索字符串所在的行,如果找到我可以返回它所在的行号。

* 例子 *

考虑以下数据存在于文本文件中。(说只有 5 行存在)

在此处输入图像描述

现在我想搜索一个字符串说“Pune”。现在在搜索之后,它应该返回我存在字符串“pune”的行号。在这种情况下,它出现在第 2 行。我应该得到“2”作为输出。我想用他们的行号搜索所有出现的“pune”

4

3 回答 3

1

我使用了 Me How 的代码示例的衍生产品,通过一个包含约 10,000 个文件的列表来搜索字符串。另外,由于我的 html 文件有可能在多行中包含字符串,并且我想要一个交错的输出,所以我对其进行了一些更改并添加了单元格插入片段。我只是在学习,但这正是我所需要的,我希望它可以帮助其他人。

Public Sub ReadTxtFile()

    Dim start As Date
    start = Now

    Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    Dim oFS As Object

    Dim filePath As String

    Dim a, b, c, d, e As Integer
    a = 2
    b = 2
    c = 3
    d = 2
    e = 1

    Dim arr() As String

    Do While Cells(d, e) <> vbNullString

            filePath = Cells(d, e)

            ReDim arr(5000) As String
            Dim i As Long
            i = 0

            If oFSO.FileExists(filePath) Then

                On Error GoTo Err

                Set oFS = oFSO.OpenTextFile(filePath)
                Do While Not oFS.AtEndOfStream
                    arr(i) = oFS.ReadLine
                    i = i + 1
                Loop
                oFS.Close
            Else
                MsgBox "The file path is invalid.", vbCritical, vbNullString
                Exit Sub
            End If

            For i = LBound(arr) To UBound(arr)
                If InStr(1, arr(i), "Clipboard", vbTextCompare) Then
                    Debug.Print i + 1, arr(i)
                    Cells(a + 1, b - 1).Select
                    Selection.Insert Shift:=xlDown
                    Cells(a, b).Value = i + 1
                    Cells(a, c).Value = arr(i)
                    a = a + 1
                    d = d + 1
                End If
            Next
            a = a + 1
            d = d + 1
    Loop

    Debug.Print DateDiff("s", start, Now)

    Exit Sub

Err:
    MsgBox "Error while reading the file.", vbCritical, vbNullString
    oFS.Close
    Exit Sub

End Sub
于 2014-09-05T20:30:32.467 回答
0

以下片段可以替换为:

 Dim arr() As String
    Dim i As Long
    i = 0

    If oFSO.FileExists(filePath) Then

        On Error GoTo Err

        Set oFS = oFSO.OpenTextFile(filePath)
        Do While Not oFS.AtEndOfStream
        ReDim Preserve arr(0 To i)
            arr(i) = oFS.ReadLine                        'to save line's content to array
            'If Len(oFSfile.ReadLine) = 0 Then Exit Do   'to get number of lines only
            i = i + 1
        Loop
        oFS.Close
    Else
        MsgBox "The file path is invalid.", vbCritical, vbNullString
        Exit Sub
    End If
于 2014-03-26T07:50:27.397 回答
0

这是另一种应该很快起作用的方法。它使用 shell 执行 FINDSTR 命令。如果您发现 cmd 框闪烁,请在互联网上搜索如何禁用它。提供了两个选项:一个将返回后跟冒号的行号和包含关键字的行的文本。另一个只会返回行号。

不确定你想对结果做什么,所以我只是把它们放在一个消息框中。


Option Explicit
'Set reference to Windows Script Host Object Model
Sub FindStrings()
    Const FindStr As String = "Pune"
    Const FN As String = "C:\users\ron\desktop\LineNumTest.txt"
    Dim WSH As WshShell
    Dim StdOut As Object
    Dim S As String

Set WSH = New WshShell

Set StdOut = WSH.Exec("cmd /c findstr /N " & FindStr & Space(1) & FN).StdOut
Do Until StdOut.AtEndOfStream
    S = S & vbCrLf & StdOut.ReadLine
    'If you want ONLY the line number, then
    'S = S & vbCrLf & Split(StdOut.ReadLine, ":")(0)
Loop
S = Mid(S, 2)

MsgBox (S)

End Sub

于 2016-02-21T03:43:05.670 回答