[英]extracting specific words (not keywords) from a log file
我試圖從以下sample.txt中提取幾個單詞(如預期輸出中所示)並將它們放在一個列表中。 我在提取正確的字段時遇到困難。 我嘗試過我的方法,但它對大多數情況都不起作用。 我更喜歡使用python,但對其他語言開放。 任何指向其他方法的指針都非常感謝。
sample.log
//*********************************************************************************
// update section
//*********************************************************************************
for (i=0; i< models; i = i+1) begin:modelgen
model_ip model_inst
(
.model_powerdown(model_powerdown),
.mcg(model_powerdown),
.lambda(_lambda[i])
);
assign fnl_verifier_lock = (tx_ready & rx_ready) ? &verifier_lock :1'b0;
native_my_ip native_my_inst
(
.tx_analogreset(tx_analogreset),
//.unused_tx_parallel_data({1536{1'b0}})
);
// END Section I :
//*********************************************************************************
resync
#(
.INIT_VALUE (1)
) inst_reset_sync
(
.clk (tx_coreclkin),
.reset (!tx_ready), // tx_digitalreset from reset
.d (1'b0),
.q (srst_tx_common )
);
預期產出
model_ip
native_my_ip
resync
我的嘗試
import re
input_file = open("sample.log", "r")
result = []
for line in input_file:
# need a more generic match condition to extract expected results
match_instantiation = re.match(r'\s(.*) ([a-zA-Z_0-9]+) ([a-zA-Z_0-9]+)_inst (.*)', line)
if match_instantiation:
print match_instantiation.group(1)
result.append(match_instantiation.group(1))
else:
continue
您可能需要一次讀取多行以確定該字符串是否為模塊名稱。
請嘗試以下方法:
import re
input_file = open("sample.log", "r")
lines = input_file.read() # reads all lines and store into a variable
input_file.close()
for m in re.finditer(r'^\s*([a-zA-Z_0-9]+)\s+([a-zA-Z_0-9]+\s+\(|#\()', lines, re.MULTILINE):
print m.group(1)
產量:
model_ip
native_my_ip
resync
上面的正則表達式可以查看可能的實例名稱或#(
。
希望這可以幫助。
使用Perl
$ perl -0777 -ne ' while ( /^\s+((\w+)\s+(\S+)\s+\(\s+\.)|^\s+(\S+)\s+\#\(\s+/gmsx ) { print "$2$4\n" } ' sample.log
model_ip
native_my_ip
resync
$
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.