从另一个文本文件中查找字符串

时间:2016-03-15 15:56:06

标签: parsing vbscript split

尝试遍历我的error.log文件并匹配另一个Map.log(文本)文件中的字符串。我认为在脚本在error.log文件中的第一行与另一个Map.log文件中的每一行匹配后,我的循环不正确。完成后,它应移至error.log中的下一行并与Map.log中的所有内容进行比较等。

我需要这样做的原因是error.log文件没有足够的信息来查明问题的根源,但它确实包含日期和时间。我已经手动将这些内容与Map.log文件中的信息进行匹配,以查看具体原因,但Map.log文件中可能有数千行。这样可以解决这个问题。基本上,我想匹配map.log中error.log的日期/时间,并提取相应的" FcId:#"。理想的结果是:

3/14/2016 1:20:35 PM: FcId: 98766
3/14/2016 1:20:39 PM: FcId: 46253
On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")   

Set ERROR_logfilename = fso.OpenTextFile(fso.GetParentFolderName(WScript.ScriptFullName) & "\Error.log")
Set MAP_logfilename = fso.OpenTextFile(fso.GetParentFolderName(WScript.ScriptFullName) & "\Map.log")

'==============================
'This section pulls all of the date and time strings in the error.log file
'for matching in the maps.log file (everything before the first comma). 
Do While Not ERROR_logfilename.AtEndOfStream
  arrStr = Split(ERROR_logfilename.ReadLine, ",")
  strError = arrStr(0)
  '==============================
  Do While Not MAP_logfilename.AtEndOfStream
    strLogItem = MAP_logfilename.ReadLine

    If InStr(strLogItem, strError) Then
      WScript.Echo strLogItem
    End If
  Loop
Loop

ERROR_logfilename.Close
MAP_logfilename.Close

error.log文件包含以下行:

3/14/2016 1:20:35 PM,Warning in List.
3/14/2016 1:20:39 PM,Warning in List.

map.log文件包含以下行:

3/14/2016 1:20:34 PM,key_id: 1 FcId: 987766 ac_id: 987763 prim_1_eftv_dttm: 8/11/2014 prim_2_eftv_dttm: 8/11/2014  
3/14/2016 1:20:34 PM,key_id: 1 FcId: 987766 svr_id: 2872158 br_FcId: 987764 eftv_dttm: 8/11/2014  term_dttm: 3/14/2016  top_md_qty: 1470 btm_md_qty: 1551
3/14/2016 1:20:34 PM,Record count: 2
3/14/2016 1:20:34 PM,List.calcLength() Calculated :  FcId: 987766 currentDate: 8/11/2014 Length: 81 with 1 webo(s).
3/14/2016 1:20:35 PM,RESULT for FcId: 987766 GnId: 2585 lbrFcId:: 987764 SeqId: 1 Length: 81 Top: 1470 Btm: 1551 on Date: 8/11/2014
3/14/2016 1:20:35 PM,Date Range for completion: 987766 : 8/11/2014 12:00:00 AM - 3/14/2016 12:00:00 AM
3/14/2016 1:20:35 PM,Processed record: 11 Id: 987766
3/14/2016 1:20:35 PM,End: 
3/14/2016 1:20:35 PM,Begin: 
3/14/2016 1:20:36 PM,key_id: 1 FcId: 29910 ac_id: 29908 prim_1_eftv_dttm: 12/1/1998 prim_2_eftv_dttm: 12/1/1998  
3/14/2016 1:20:36 PM,key_id: 2 FcId: 29910 ac_id: 29908 prim_1_eftv_dttm: 10/1/2000 prim_2_eftv_dttm: 10/1/2000  
3/14/2016 1:20:36 PM,key_id: 1 FcId: 29910 svr_id: 350499 br_FcId: 29909 eftv_dttm: 12/1/1998  term_dttm: 10/1/2000  top_md_qty: 1270 btm_md_qty: 1350
3/14/2016 1:20:36 PM,key_id: 2 FcId: 29910 svr_id: 350500 br_FcId: 29909 eftv_dttm: 12/1/1998  term_dttm: 10/1/2000  top_md_qty: 1390 btm_md_qty: 1560
3/14/2016 1:20:36 PM,key_id: 3 FcId: 29910 svr_id: 350501 br_FcId: 29909 eftv_dttm: 12/1/1998  term_dttm: 3/14/2016  top_md_qty: 1620 btm_md_qty: 1800
3/14/2016 1:20:36 PM,key_id: 4 FcId: 29910 svr_id: 350502 br_FcId: 29909 eftv_dttm: 12/1/1998  term_dttm: 3/14/2016  top_md_qty: 1840 btm_md_qty: 2020
3/14/2016 1:20:36 PM,Record count: 2
3/14/2016 1:20:36 PM,List.calcLength() Calculated :  FcId: 29910 currentDate: 12/1/1998 Length: 750 with 1 webo(s).
3/14/2016 1:20:36 PM,RESULT for FcId: 29910 GnId: 3997 lbrFcId:: 29909 SeqId: 1 Length: 82.51 Top: 1270 Btm: 1352.51 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2584 lbrFcId:: 29909 SeqId: 2 Length: 106.27 Top: 1352.51 Btm: 1458.78 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3998 lbrFcId:: 29909 SeqId: 3 Length: 122.22 Top: 1458.78 Btm: 1581 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3999 lbrFcId:: 29909 SeqId: 4 Length: 48.43 Top: 1581 Btm: 1629.43 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2588 lbrFcId:: 29909 SeqId: 5 Length: 76.06 Top: 1629.43 Btm: 1705.49 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2592 lbrFcId:: 29909 SeqId: 6 Length: 34.81 Top: 1705.49 Btm: 1740.3 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2594 lbrFcId:: 29909 SeqId: 7 Length: 16.43 Top: 1740.3 Btm: 1756.73 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2595 lbrFcId:: 29909 SeqId: 8 Length: 25.27 Top: 1756.73 Btm: 1782 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2597 lbrFcId:: 29909 SeqId: 9 Length: 43.5 Top: 1782 Btm: 1825.5 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2598 lbrFcId:: 29909 SeqId: 10 Length: 19.97 Top: 1825.5 Btm: 1845.47 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2599 lbrFcId:: 29909 SeqId: 11 Length: 19.53 Top: 1845.47 Btm: 1865 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2600 lbrFcId:: 29909 SeqId: 12 Length: 8.67 Top: 1865 Btm: 1873.67 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 4007 lbrFcId:: 29909 SeqId: 13 Length: 43.33 Top: 1873.67 Btm: 1917 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2603 lbrFcId:: 29909 SeqId: 14 Length: 45.24 Top: 1917 Btm: 1962.24 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2605 lbrFcId:: 29909 SeqId: 15 Length: 19.76 Top: 1962.24 Btm: 1982 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3989 lbrFcId:: 29909 SeqId: 16 Length: 14.98 Top: 1982 Btm: 1996.98 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2607 lbrFcId:: 29909 SeqId: 17 Length: 15.82 Top: 1996.98 Btm: 2012.8 on Date: 12/1/1998
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3990 lbrFcId:: 29909 SeqId: 18 Length: 7.2 Top: 2012.8 Btm: 2020 on Date: 12/1/1998
3/14/2016 1:20:37 PM,Date Range for completion: 29910 : 12/1/1998 12:00:00 AM - 10/1/2000 12:00:00 AM
3/14/2016 1:20:37 PM,Record count: 2
3/14/2016 1:20:37 PM,List.calcLength() Calculated :  FcId: 29910 currentDate: 10/1/2000 Length: 400 with 1 webo(s).
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3999 lbrFcId:: 29909 SeqId: 1 Length: 9.43 Top: 1620 Btm: 1629.43 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2588 lbrFcId:: 29909 SeqId: 2 Length: 76.06 Top: 1629.43 Btm: 1705.49 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2592 lbrFcId:: 29909 SeqId: 3 Length: 34.81 Top: 1705.49 Btm: 1740.3 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2594 lbrFcId:: 29909 SeqId: 4 Length: 16.43 Top: 1740.3 Btm: 1756.73 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2595 lbrFcId:: 29909 SeqId: 5 Length: 25.27 Top: 1756.73 Btm: 1782 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2597 lbrFcId:: 29909 SeqId: 6 Length: 43.5 Top: 1782 Btm: 1825.5 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2598 lbrFcId:: 29909 SeqId: 7 Length: 19.97 Top: 1825.5 Btm: 1845.47 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2599 lbrFcId:: 29909 SeqId: 8 Length: 19.53 Top: 1845.47 Btm: 1865 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2600 lbrFcId:: 29909 SeqId: 9 Length: 8.67 Top: 1865 Btm: 1873.67 on Date: 10/1/2000
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 4007 lbrFcId:: 29909 SeqId: 10 Length: 43.33 Top: 1873.67 Btm: 1917 on Date: 10/1/2000
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 2603 lbrFcId:: 29909 SeqId: 11 Length: 45.24 Top: 1917 Btm: 1962.24 on Date: 10/1/2000
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 2605 lbrFcId:: 29909 SeqId: 12 Length: 19.76 Top: 1962.24 Btm: 1982 on Date: 10/1/2000
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 3989 lbrFcId:: 29909 SeqId: 13 Length: 14.98 Top: 1982 Btm: 1996.98 on Date: 10/1/2000
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 2607 lbrFcId:: 29909 SeqId: 14 Length: 15.82 Top: 1996.98 Btm: 2012.8 on Date: 10/1/2000
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 3990 lbrFcId:: 29909 SeqId: 15 Length: 7.2 Top: 2012.8 Btm: 2020 on Date: 10/1/2000
3/14/2016 1:20:38 PM,Date Range for completion: 29910 : 10/1/2000 12:00:00 AM - 3/14/2016 12:00:00 AM
3/14/2016 1:20:38 PM,Processed record: 12 Id: 29910
3/14/2016 1:20:38 PM,End: 
3/14/2016 1:20:38 PM,Begin: 
3/14/2016 1:20:38 PM,key_id: 1 FcId: 987765 ac_id: 987763 prim_1_eftv_dttm: 8/11/2014 prim_2_eftv_dttm: 8/11/2014  
3/14/2016 1:20:38 PM,key_id: 1 FcId: 987765 svr_id: 2872159 br_FcId: 987764 eftv_dttm: 8/11/2014  term_dttm: 3/14/2016  top_md_qty: 1750 btm_md_qty: 1791
3/14/2016 1:20:38 PM,Record count: 2
3/14/2016 1:20:38 PM,List.calcLength() Calculated :  FcId: 987765 currentDate: 8/11/2014 Length: 41 with 1 webo(s).
3/14/2016 1:20:39 PM,RESULT for FcId: 987765 GnId: 2585 lbrFcId:: 987764 SeqId: 1 Length: 41 Top: 1750 Btm: 1791 on Date: 8/11/2014
3/14/2016 1:20:39 PM,Date Range for completion: 987765 : 8/11/2014 12:00:00 AM - 3/14/2016 12:00:00 AM
3/14/2016 1:20:39 PM,Processed record: 13 Id: 987765
3/14/2016 1:20:39 PM,End: 
3/14/2016 1:20:39 PM,Begin: 
3/14/2016 1:20:39 PM,key_id: 1 FcId: 46253 ac_id: 46251 prim_1_eftv_dttm: 12/1/1991 prim_2_eftv_dttm: 12/1/1991  
3/14/2016 1:20:39 PM,key_id: 2 FcId: 46253 ac_id: 46251 prim_1_eftv_dttm: 6/1/1998 prim_2_eftv_dttm: 6/1/1998  
3/14/2016 1:20:39 PM,key_id: 1 FcId: 46253 svr_id: 133009 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 890 btm_md_qty: 911
3/14/2016 1:20:39 PM,key_id: 2 FcId: 46253 svr_id: 133010 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 911 btm_md_qty: 968
3/14/2016 1:20:39 PM,key_id: 3 FcId: 46253 svr_id: 133011 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 968 btm_md_qty: 1036
3/14/2016 1:20:39 PM,key_id: 4 FcId: 46253 svr_id: 133013 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1036 btm_md_qty: 1070
3/14/2016 1:20:39 PM,key_id: 5 FcId: 46253 svr_id: 133015 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1090 btm_md_qty: 1120
3/14/2016 1:20:39 PM,key_id: 6 FcId: 46253 svr_id: 133017 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1120 btm_md_qty: 1172
3/14/2016 1:20:39 PM,key_id: 7 FcId: 46253 svr_id: 133018 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1172 btm_md_qty: 1220
3/14/2016 1:20:39 PM,key_id: 8 FcId: 46253 svr_id: 133021 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1240 btm_md_qty: 1320
3/14/2016 1:20:39 PM,key_id: 9 FcId: 46253 svr_id: 133024 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1320 btm_md_qty: 1380
3/14/2016 1:20:39 PM,key_id: 10 FcId: 46253 svr_id: 133026 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1400 btm_md_qty: 1430
3/14/2016 1:20:39 PM,key_id: 11 FcId: 46253 svr_id: 133027 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1430 btm_md_qty: 1450
3/14/2016 1:20:39 PM,key_id: 12 FcId: 46253 svr_id: 133030 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1450 btm_md_qty: 1510
3/14/2016 1:20:39 PM,key_id: 13 FcId: 46253 svr_id: 133031 br_FcId: 46252 eftv_dttm: 10/16/1991  term_dttm: 3/14/2016  top_md_qty: 1510 btm_md_qty: 1540
3/14/2016 1:20:39 PM,Record count: 2
3/14/2016 1:20:39 PM,List.calcLength() Calculated :  FcId: 46253 currentDate: 12/1/1991 Length: 650 with 1 webo(s).
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2584 lbrFcId:: 46252 SeqId: 1 Length: 40.86 Top: 890 Btm: 930.86 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 3998 lbrFcId:: 46252 SeqId: 2 Length: 99.04 Top: 930.86 Btm: 1029.9 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 3999 lbrFcId:: 46252 SeqId: 3 Length: 55.1 Top: 1029.9 Btm: 1085 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2588 lbrFcId:: 46252 SeqId: 4 Length: 81.13 Top: 1085 Btm: 1166.13 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2592 lbrFcId:: 46252 SeqId: 5 Length: 59.17 Top: 1166.13 Btm: 1225.3 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2594 lbrFcId:: 46252 SeqId: 6 Length: 62.35 Top: 1225.3 Btm: 1287.65 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2597 lbrFcId:: 46252 SeqId: 7 Length: 141.25 Top: 1287.65 Btm: 1428.9 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 4007 lbrFcId:: 46252 SeqId: 8 Length: 15.7 Top: 1428.9 Btm: 1444.6 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2603 lbrFcId:: 46252 SeqId: 9 Length: 54.36 Top: 1444.6 Btm: 1498.96 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2605 lbrFcId:: 46252 SeqId: 10 Length: 29.7 Top: 1498.96 Btm: 1528.66 on Date: 12/1/1991
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 3989 lbrFcId:: 46252 SeqId: 11 Length: 11.34 Top: 1528.66 Btm: 1540 on Date: 12/1/1991

2 个答案:

答案 0 :(得分:1)

您可能需要对其进行调整,以便选择正确的换行符。

'===================================
On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")

Set ERROR_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Error.log")
Set MAP_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Map.log")


errorContents = Split(ERROR_logfilename.ReadAll, vbCrLf) 'get the contents and split on the line feed
mapContents = Split(MAP_logfilename.ReadAll, vbCrLf)

ERROR_logfilename.Close 'close the file
MAP_logfilename.Close

For i = 0 To UBound(errorContents) - 1 'loop through error log contents
    arrStr = Split(errorContents(i), ",")
    strError = arrStr(0)
    For j = 0 To UBound(mapContents) - 1 'loop through map log contents
        If InStr(mapContents(j), strError) Then
            wscript.echo strError & " " & strMap
        End If
    Next j
Next i

答案 1 :(得分:1)

感谢Sorceri的帮助,这是我最后的剧本。

'===================================
On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")

Set ERROR_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Error.log")
Set MAP_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Map.log")

strOutputFileName = fso.GetParentFolderName(WScript.ScriptFullName) & "\Error_Clean.log"

        Set objFile = fso.OpenTextFile(strOutputFileName, 2 , true) 'Clear old errors
        objFile.Write ""   
        objFile.Close

errorContents = Split(ERROR_logfilename.ReadAll, vbCrLf) 'get the contents and split on the line feed
mapContents = Split(MAP_logfilename.ReadAll, vbCrLf)

ERROR_logfilename.Close 'close the file
MAP_logfilename.Close

For i = 0 To UBound(errorContents) - 1 'loop through error log contents
    arrStr = Split(errorContents(i), ",")
    strError = arrStr(0)

    For j = 0 To UBound(mapContents) - 1 'loop through map log contents
        If InStr(mapContents(j), strError) and InStr(mapContents(j), "PM,Processed record: ") Then

    arrStr1 = Split(mapContents(j),"CmplId: ")
    strMapContents = arrStr1(1)

        'wscript.echo strError & vbCrlF & "Cmpl_Fac_ID: " & strMapContents
    CleanErrors = strError & vbCrlF & "Cmpl_Fac_ID: " & strMapContents & vbCrlF

        Set objFile = fso.OpenTextFile(strOutputFileName, 8 , true) 'Write new errors
        objFile.WriteLine CleanErrors   
        objFile.Close

        End If

    Next 
Next 

'==================================