我想从日志文件中提取一段信息。我正在使用的模式是节点名称和命令的提示。我想提取命令输出的信息并比较它们。考虑下面的示例输出使用Java从日志中提取某些模式
NodeName > command1
this is the sample output
NodeName > command2
this is the sample output
我试过下面的代码。
public static void searchcommand(String strLineString)
{
String searchFor = "Nodename> command1";
String endStr = "Nodename";
String op="";
int end=0;
int len = searchFor.length();
int result = 0;
if (len > 0) {
int start = strLineString.indexOf(searchFor);
while(start!=-1){
end = strLineString.indexOf(endStr,start+len);
if(end!=-1){
op=strLineString.substring(start, end);
}else{
op=strLineString.substring(start, strLineString.length());
}
String[] arr = op.split("%%%%%%%");
for (String z : arr) {
System.out.println(z);
}
start = strLineString.indexOf(searchFor,start+len);
}
}
}
问题是代码太慢而无法提取数据。有没有其他方法可以这样做?
编辑1 它是一个日志文件,我在上面的代码中读取为一个字符串。
你有整个日志作为一个字符串? –
我读取文件作为上述代码的字符串。 –
这样一个字符串有多大?你有没有测量需要时间?将日志读入一个字符串?寻找开始/停止或分裂?很难给出具体的解析优化,她的输入与代码不匹配。 –