我想從日誌文件中提取一段信息。我正在使用的模式是節點名稱和命令的提示。我想提取命令輸出的信息並比較它們。考慮下面的示例輸出使用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 它是一個日誌文件,我在上面的代碼中讀取爲一個字符串。
你有整個日誌作爲一個字符串? –
我讀取文件作爲上述代碼的字符串。 –
這樣一個字符串有多大?你有沒有測量需要時間?將日誌讀入一個字符串?尋找開始/停止或分裂?很難給出具體的解析優化,她的輸入與代碼不匹配。 –