【问题标题】:Extract time-stamps from log file and save to new Excel file从日志文件中提取时间戳并保存到新的 Excel 文件
【发布时间】:2016-01-05 04:58:46
【问题描述】:

我想从本地机器中的文本格式的日志文件中获取时间戳值,并使用 VB 脚本将这些值保存到 Excel 文件中。

我的日志文件格式是:-

14.000.00.10 - - [07/Mar/2015:16:06:51 -0800] “GET /twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1.1”200 4523 14.000.00.10 - - [07/Mar/2015:16:10:02 -0800] “GET /mailman/listinfo/hsdivision HTTP/1.1”200 6291 14.000.00.10 - - [07/Mar/2015:16:11:58 -0800] “GET /twiki/bin/view/TWiki/WikiSyntax HTTP/1.1”200 7352 14.000.00.10 - - [07/Mar/2015:16:20:55 -0800] “GET /twiki/bin/view/Main/DCCAndPostFix HTTP/1.1”200 5253 14.000.00.10 - - [07/Mar/2015:16:23:12 -0800] “GET /twiki/bin/oops/TWiki/AppendixFileSystem?template=oopsmore&param1=1.12&param2=1.12 HTTP/1.1”

通过获取在多行中重复出现的 ID 值,我如何保存我的时间戳,例如[07/Mar/2015:16:23:12]从日志.txt文件到Excel文件?

我尝试编写此代码:

Set xl = CreateObject("Excel.Application")
xl.Visible = True

Set wb = xl.Workbooks.Add
Set ws = wb.Sheets(1)

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\access_log.txt")

strContents = objFile.ReadAll

objFile.Close

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.Pattern = "mailman"

Set colMatches = objRegEx.Execute(strContents)  

For Each Match in colMatches
    strReturnStr = "Match found at position "
    strReturnStr = strReturnStr & match.FirstIndex & ". Match Value is '"
    StrReturnStr = strReturnStr & match.value & "'." & "<BR>" & VBCrLf
    WScript.Echo(strReturnStr)
Next
wb.SaveAs "E:\access_og.csv", -4143, , , , False
wb.Close
xl.Quit

在 cmd 提示符下使用 cscript name.vbs 运行时,它会显示找到字符串的行号,然后 .csv 文件打开时出现错误“'access_og.csv' 的文件格式和扩展名不匹配。文件可能已损坏且不安全。"

问题还是没有解决:(

【问题讨论】:

    标签: excel vbscript scripting


    【解决方案1】:

    你能试试这个吗:-

    Dim objFSO, strTextFile, strData, strLine, arrLines
    CONST ForReading = 1
    'name of the text file
    strTextFile = "E:\access_log.txt"
    'Create a File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Open the text file - strData now contains the whole file 
    strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
    'Split the text file into lines
    arrLines = Split(strData,vbCrLf)
    Set objExcel = CreateObject("Excel.Application")
    'Mentioning the path of the excel sheet.
    Set objWorkbook = objExcel.Workbooks.Open("Your Excel File Path")
    'Mentioning the worksheet which is going to be used.
    Set objWorkSheet = objWorkbook.Worksheets("Sheet1")
    
    'This control will define the  view of  the excel sheet. Set it to "True" if you want to see the excel sheet. Set it to "False" if you don't want to view the excel sheet.
    
    objExcel.Application.Visible = True
    
    'We will assign a varaible called rowcount to mention the number of used rows.
    
    rowcount = objWorkSheet.usedrange.rows.count
    rowcount = rowcount + 1
    objExcel.Visible = True
    
    'Step through the lines
    For Each strLine in arrLines
     intLine = InStr(1,strLine,"mailman",1)
    
     if intLine > 0 Then
      objExcel.Cells(rowcount, 1).Value = strLine
      rowcount = rowcount + 1
     End IF
     objWorkbook.Save
    Next
    objWorkbook.Save
    objWorkbook.quit
    Set objFSO = Nothing
    result=Msgbox("Completed",0)
    

    在运行它之前,请确保您已经创建了与“Excel 文件路径”中给出的相同名称的 Excel 文件。

    【讨论】:

    • 我从过去 2 周开始一直在研究它!感谢您的贡献。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多