從日誌檔案中提取特定行

從日誌檔案中提取特定行

我有一個如下所示的日誌文件,

 - UTT (1): test_1978_recreatie_1656.wav
lattice-to-ctm-conf ark:- output/spk001_test_1978_recreatie_1656_1bestsym.ctm 
online2-wav-nnet3-latgen-faster --online=false --do-endpointing=false --frame-subsampling-factor=3 --config=exp/tdnn1a_sp_bi_online/conf/online.conf --max-active=7000 --beam=15.0 --lattice-beam=6.0 --acoustic-scale=1.0 --word-symbol-table=exp/tdnn1a_sp_bi_online/graph_s/words.txt exp/tdnn1a_sp_bi_online/final.mdl exp/tdnn1a_sp_bi_online/graph_s/HCLG.fst 'ark:echo spk001 test_1978_recreatie_1656|' 'scp:echo test_1978_recreatie_1656 raw_data/spk001/test_1978_recreatie_1656.wav|' ark:- 
test_1978_recreatie_1656 wij zullen <unk> 
LOG (online2-wav-nnet3-latgen-faster[5.5.929~1-9bca2]:main():online2-wav-nnet3-latgen-faster.cc:296) Decoded utterance test_1978_recreatie_1656
LOG (lattice-to-ctm-conf[5.5.929~1-9bca2]:main():lattice-to-ctm-conf.cc:175) For utterance test_1978_recreatie_1656, Bayes Risk 4.06451, avg. confidence per-word 0.636305
 - UTT (1): test_1978_recreatie_1656.wav, time: 1 seconds
 - UTT (2): test_1978_recreatie_1657.wav
lattice-to-ctm-conf ark:- output/spk001_test_1978_recreatie_1657_1bestsym.ctm 
online2-wav-nnet3-latgen-faster --online=false --do-endpointing=false --frame-subsampling-factor=3 --config=exp/tdnn1a_sp_bi_online/conf/online.conf --max-active=7000 --beam=15.0 --lattice-beam=6.0 --acoustic-scale=1.0 --word-symbol-table=exp/tdnn1a_sp_bi_online/graph_s/words.txt exp/tdnn1a_sp_bi_online/final.mdl exp/tdnn1a_sp_bi_online/graph_s/HCLG.fst 'ark:echo spk001 test_1978_recreatie_1657|' 'scp:echo test_1978_recreatie_1657 raw_data/spk001/test_1978_recreatie_1657.wav|' ark:- 
test_1978_recreatie_1657 we kunnen dat wel zeggen 
LOG (online2-wav-nnet3-latgen-faster[5.5.929~1-9bca2]:main():online2-wav-nnet3-latgen-faster.cc:296) Decoded utterance test_1978_recreatie_1657
LOG (lattice-to-ctm-conf[5.5.929~1-9bca2]:main():lattice-to-ctm-conf.cc:175) For utterance test_1978_recreatie_1657, Bayes Risk 0.654865, avg. confidence per-word 0.922916
 - UTT (2): test_1978_recreatie_1657.wav, time: 0 seconds

在日誌檔案中,UTT(n) 的每第 4 行都有一個轉錄,我想使用 Linux 指令來提取該轉錄。例如test_1978_recreatie_1657 we kunnen dat wel zeggentest_1978_recreatie_1656 wij zullen <unk> 早些時候我一直在尋找用於特定模式提取的 grep 命令,但它沒有成功。請建議我該怎麼做。

答案1

我認為你要求的是在出現任何 後打印第四行UTT (n),每次遇到 時將計數器重置為 1 UTT (n)

awk '/UTT \([0-9]+\)/{line=0} {line++} line==4' file

輸出

test_1978_recreatie_1656 wij zullen <unk>
test_1978_recreatie_1657 we kunnen dat wel zeggen

一些解釋。awk具有 形式的行pattern {action},其中pattern或是action可選的(但不能同時是兩者)。每行輸入都依序套用於所有模式/動作指令。缺少模式意味著將為每個輸入行執行該操作。缺失action意味著將列印輸入行。

/UTT \([0-9]+\)/ {line=0}    # Match the pattern to set line=0
{line++}                     # Each line of input increments line
line==4                      # When line==4, implicitly print the line

答案2

對於這樣的事情,awk 可能比 grep 更簡單。

awk 'c && !--c; $2 == "UTT" {c=3}' file

每當“UTT”行匹配時,就會設定一個計數器。如果設定了計數器,則遞減變數並在計數器返回零時列印該行。您可以透過多種方式來搭配「UTT」行,例如 regex/^ - UTT/ {c=3}或 string match index($0, " - UTT") == 1 {c=3}$2 == "UTT"當第二個字段是字串“UTT”時,條件匹配。

答案3

使用awk

awk '/UTT/{a=NR+3} NR==a' input

在此命令中,如果UTT找到則a設定為NR+3NR==a列印所需的輸出。

相關內容