谢谢您的帮助。
我有一个很大的日志文件,需要为每个块(记录)提取特定信息。
在日志文件中,三个百分号(%%%)描绘了每个记录块。 每个记录块都有几行数据。
我需要查询的某些信息在日志文件的一行(字符串)中,而其他数据则嵌入在一行的字符串中。我将包括一个示例日志文件,这样也许会更清楚。
我有几个条件需要搜索每个记录块。例如:
If Cond_Id: = X-MODE1-999999I
And
If Mode_Name: = Mode1_xx_ALA
Then
From the “Info:” string within that record block
I need to Return
the “Sub_Task” and “Com_Task” values for “Com10”
以及以下内容:
Cond_Id
Mode_Name
Exec_time(xxx)
Com10- Sub_Task
Com10- Com_Task
Some reference to a line number in the record block for "Cond_Id:" would be helpful
我需要将这些数据写入具有上面六(6)项标题的Excel工作表中。每次搜索都会产生许多实例,我需要将所有实例都写在excel工作表中。
由于我有限的编程知识,所以我不确定将日志文件读入Excel还是将其加载到数组中并执行查询是否是最好的方法。这些将是相当大的日志文件,因此我不确定开销以及哪种方法最好。
我真的需要在excel vba中完成此任务的逻辑方面的帮助。
因此,我可以将日志文件读取到数组中并将其写入Excel工作表。请看下面:
Sub getTextFile()
Dim strPathFile As String
Dim strScriptPath As String
'Dim textFileName As String
'GET FILE & path of Text File
strPathFile = strOpenTextFilePath
strScriptPath = Left(strPathFile, InStrRev(strPathFile, "\"))
frmLogFileAnalysis.lblLogFileSRR = strPathFile
Dim txt As String, Arr, d, r, c, rv(), u, j
'read in the entire file
With CreateObject("scripting.filesystemobject").opentextfile(strPathFile)
txt = .readall()
.Close
End With
Arr = Split(txt, vbCrLf) 'split lines to an array
For j = 1 To UBound(Arr)
Worksheets("LogFile").Cells(j, 1).Value = Arr(j - 1)
Next
End Sub
日志文件示例:
%%%
xxx_xxx.xxx_xxx.Send.xxx_xxx.xxx_xxx
xxx_xxx.xxx_xxx.Send.xxx_xxx.xxx_xxx
2019 01 01 01 01 01.12345 xxx
2019 01 01 01 01 01.12345 xxx
2019 01 01 01 01 01.12345
xxx_xxx_xxx
1234
Mode1_xx_xxx
xxx 1
id: 0; Exec_time(xxx): { 2019 01 01 01 01 01.12345 };
Category: XXX_XXX.XXX_MSG.ERROR;
xxx_xxxxxx_State: TEST;
Mode_Name: Mode1_xx_Txx;
Source: XXX_XXX.MODEM.XXXX.XXXX;
Condition_Id: X-MODE1-999999I;
Link_Id: 9( 9);
Operation: ;
Msg_Name: Stat_Table D_Name: Status_1 From_P: W To_Prev: I Value => 111111111 Limits => Value => 111111111
Imp cond: Info Id: 999; Execution_Time(xxx): 2019 01 01 01 01 01.12345; L_Id: 9( 9); Command_String: 00 00 00 00 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 ; ;
%%%
xxx_xxx.xxx_xxx.Send.xxx_xxx.xxx_xxx
2019 01 01 01 01 01.12347 xxx
2019 01 01 01 01 01.12347 xxx
2019 01 01 01 01 01.12347
xxx_xxx_xxx
1234
Mode1_xx_xxx
xxx 1
id: 0; Exec_time(xxx): { 2019 01 01 01 01 01.12347 };
Category: XXX_XXX.XXX_MSG.ERROR;
xxx_xxxxxx_State: TEST;
Mode_Name: Mode1_xx_Txx;
Source: XXX_XXX.MODEM.XXXX.XXXX;
Condition_Id: X-MODE1-999999P;
Link_Id: 9( 9);
Operation: ;
Msg_Name: Stat_Table D_Name: Status_1 From_P: I To_Prev: P Value => 111111111 Limits => Value => 111111111
Imp cond: Info Id: 999; Execution_Time(xxx): 2019 01 01 01 01 01.12347; L_Id: 9( 9); Command_String: 00 00 00 00 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 ; ;
%%%
xxx_xxx.xxx_xxx.Send.xxx_xxx.xxx_xxx
2019 01 01 01 01 01.12349 xxx
2019 01 01 01 01 01.12349 xxx
2019 01 01 01 01 01.12349
xxx_xxx_xxx
1234
Mode1_xx_xxx
xxx 1
id: 0; Exec_time(xxx): { 2019 01 01 01 01 01.12349 };
Category: XXX_XXX.XXX_MSG.ERROR;
xxx_xxxxxx_State: TEST;
Mode_Name: Mode1_xx_Txx;
Source: XXX_XXX.MODEM.XXXX.XXXX;
Condition_Id: X-MODE1-999999W;
Link_Id: 9( 9);
Operation: ;
Msg_Name: Stat_Table D_Name: Status_1 From_P: P To_Prev: W Value => 111111111 Limits => Value => 111111111
Imp cond: Info Id: 999; Execution_Time(xxx): 2019 01 01 01 01 01.12349; L_Id: 9( 9); Command_String: 00 00 00 00 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 ; ;
%%%
xxx_xxx.xxx_xxx.Send.xxx_xxx.xxx_xxx
2019 01 01 01 01 01.12346 xxx
2019 01 01 01 01 01.12346 xxx
2019 01 01 01 01 01.12346
xxx_xxx_xxx
1234
Mode1_xx_xxx
xxx 1
id: 0; Exec_time(xxx): { 2019 01 01 01 01 01.12346 };
Category: XXX_XXX.XXX_MSG.ERROR;
xxx_xxxxxx_State: TEST;
Mode_Name: Mode1_xx_ALA;
Source: XXX_XXX.MODEM.XXXX.XXXX;
Condition_Id: X-MODE1-999999I;
Link_Id: 9( 9);
Operation: ;
Info: Sub_Task_table: { Com1: Sub_Task: TTTT; Com_Task: TTTT; ; Com2: Sub_Task: TTTT; Com_Task: TTTT; ;Com3: Sub_Task: TTTT; Com_Task: TTTT; ;Com4: Sub_Task: TTTT; Com_Task: TTTT; ;Com5: Sub_Task: TTTT; Com_Task: TTTT; ;Com6: Sub_Task: TTTT; Com_Task: TTTT; ;Com7: Sub_Task: TTTT; Com_Task: TTTT; ;Com8: Sub_Task: TTTT; Com_Task: TTTT; ;Com9: Sub_Task: TTTT; Com_Task: TTTT; ;Com10: Sub_Task: TTTT; Com_Task: TTTT; ;Com11: Sub_Task: TTTT; Com_Task: TTTT; ;Com12: Sub_Task: TTTT; Com_Task: TTTT; ;Com13: Sub_Task: TTTT; Com_Task: TTTT; ;Com14: Sub_Task: TTTT; Com_Task: TTTT; ;Com15: Sub_Task: TTTT; Com_Task: TTTT; ;Com16: Sub_Task: TTTT; Com_Task: TTTT; ;Com17: Sub_Task: TTTT; Com_Task: TTTT; ;Com18: Sub_Task: TTTT; Com_Task: TTTT; ;Com19: Sub_Task: TTTT; Com_Task: TTTT; ;Com20: Sub_Task: TTTT; Com_Task: TTTT; ; }; ;
%%%
xxx_xxx.xxx_xxx.Send.xxx_xxx.xxx_xxx
2019 01 01 01 01 01.12348 xxx
2019 01 01 01 01 01.12348 xxx
2019 01 01 01 01 01.12348
xxx_xxx_xxx
1234
Mode1_xx_xxx
xxx 1
id: 0; Exec_time(xxx): { 2019 01 01 01 01 01.12348 };
Category: XXX_XXX.XXX_MSG.ERROR;
xxx_xxxxxx_State: TEST;
Mode_Name: Mode1_xx_ALA;
Source: XXX_XXX.MODEM.XXXX.XXXX;
Condition_Id: X-MODE1-999999P;
Link_Id: 9( 9);
Operation: ;
Info: Sub_Task_table: { Com1: Sub_Task: TTTT; Com_Task: TTTT; ; Com2: Sub_Task: TTTT; Com_Task: TTTT; ;Com3: Sub_Task: TTTT; Com_Task: TTTT; ;Com4: Sub_Task: TTTT; Com_Task: TTTT; ;Com5: Sub_Task: TTTT; Com_Task: TTTT; ;Com6: Sub_Task: TTTT; Com_Task: TTTT; ;Com7: Sub_Task: TTTT; Com_Task: TTTT; ;Com8: Sub_Task: TTTT; Com_Task: TTTT; ;Com9: Sub_Task: TTTT; Com_Task: TTTT; ;Com10: Sub_Task: NNNN; Com_Task: NNNN; ;Com11: Sub_Task: TTTT; Com_Task: TTTT; ;Com12: Sub_Task: TTTT; Com_Task: TTTT; ;Com13: Sub_Task: TTTT; Com_Task: TTTT; ;Com14: Sub_Task: TTTT; Com_Task: TTTT; ;Com15: Sub_Task: TTTT; Com_Task: TTTT; ;Com16: Sub_Task: TTTT; Com_Task: TTTT; ;Com17: Sub_Task: TTTT; Com_Task: TTTT; ;Com18: Sub_Task: TTTT; Com_Task: TTTT; ;Com19: Sub_Task: TTTT; Com_Task: TTTT; ;Com20: Sub_Task: TTTT; Com_Task: TTTT; ; }; ;
%%%
xxx_xxx.xxx_xxx.Send.xxx_xxx.xxx_xxx
2019 01 01 01 01 01.12350 xxx
2019 01 01 01 01 01.12350 xxx
2019 01 01 01 01 01.12350
xxx_xxx_xxx
1234
Mode1_xx_xxx
xxx 1
id: 0; Exec_time(xxx): { 2019 01 01 01 01 01.12350 };
Category: XXX_XXX.XXX_MSG.ERROR;
xxx_xxxxxx_State: TEST;
Mode_Name: Mode1_xx_ALA;
Source: XXX_XXX.MODEM.XXXX.XXXX;
Condition_Id: X-MODE1-999999W;
Link_Id: 9( 9);
Operation: ;
Info: Sub_Task_table: { Com1: Sub_Task: TTTT; Com_Task: TTTT; ; Com2: Sub_Task: TTTT; Com_Task: TTTT; ;Com3: Sub_Task: TTTT; Com_Task: TTTT; ;Com4: Sub_Task: TTTT; Com_Task: TTTT; ;Com5: Sub_Task: TTTT; Com_Task: TTTT; ;Com6: Sub_Task: TTTT; Com_Task: TTTT; ;Com7: Sub_Task: TTTT; Com_Task: TTTT; ;Com8: Sub_Task: TTTT; Com_Task: TTTT; ;Com9: Sub_Task: TTTT; Com_Task: TTTT; ;Com10: Sub_Task: WWWW; Com_Task: WWWW; ;Com11: Sub_Task: TTTT; Com_Task: TTTT; ;Com12: Sub_Task: TTTT; Com_Task: TTTT; ;Com13: Sub_Task: TTTT; Com_Task: TTTT; ;Com14: Sub_Task: TTTT; Com_Task: TTTT; ;Com15: Sub_Task: TTTT; Com_Task: TTTT; ;Com16: Sub_Task: TTTT; Com_Task: TTTT; ;Com17: Sub_Task: TTTT; Com_Task: TTTT; ;Com18: Sub_Task: TTTT; Com_Task: TTTT; ;Com19: Sub_Task: TTTT; Com_Task: TTTT; ;Com20: Sub_Task: TTTT; Com_Task: TTTT; ; }; ;
答案 0 :(得分:0)
什么大小的日志文件?排?和Kb / Mb / Gb?
上面的VBA会先执行Readall,Split,然后Loop / Cells并将其全部导入Excel
此新过程读取每一行,并搜索5个请求的字段,并将其保存到局部变量中。到达“ Info:”时,它将获取Com10数据,然后将新行写入输出。每当Mode_Name或Cond_Id不正确时,它就会绕过所有行,直到下一个“ %%%”。
vspace