2009-10-30 31 views
0

因此,我作爲程序員所做的一項常見任務是調試一個實時系統。我調試實時系統的方法之一是從控制檯捕獲詳細的日誌。你會如何將它變成一個VIM宏?

通常,日誌文件對於我感興趣的每一行都有大約20條額外的行。

爲了最大限度地減少我的宏腳本,我開始創建一個宏,它將抓取我感興趣的20行中的一行! (而不是對所有我不想要的行進行20次替換......這會使宏的長度比所需要的長20倍。)此宏代碼的其餘部分將把這一行變成* .csv文件,所以我可以在Matlab或Excel中用我認爲合適的數字來玩。

下面是宏的代碼(這些命令是超編輯特定的命令):

Clipboard 1 
ClearClipboard 
Loop 
Find RegExp "{*}DBGLINE: STR1 (*) STR2 (*)^p" 
IfFound 
CopyAppend 
Else 
ExitLoop 
EndIf 
EndLoop 
SelectAll 
Delete 
Paste 
Find RegExp "{*}DBGLINE: STR1(" 
Replace All "" 
Find RegExp ") STR2 (" 
Replace All " , " 
Find RegExp ")*^p" 
Replace All "^p" 
ClearClipboard 

*僅供參考,I have posted the API/description of what each command does online.

讓我打破在一個更可讀的僞代碼是什麼這個宏做:

buffer = ""; 
// Keep finding $REGEX until EOF 
while(1) { 
    if(Find $REGEX) { 
     Select the text; 
    } else { 
     break; 
    } 
    buffer += selected piece of text; 
} 

// Now we can focus only on the selected lines from the log file 
Select the entire text in the file; 
Delete all selected text; 
Paste the buffer into the text file; 

// Convert the human readable text into a *.csv file 
Parse out all the non-numerical content; 
Replace with " , " commas; 

我知道如何通過添加地圖到我的.vimrc文件創建在VIM簡單的宏:

map $KEYBOARD :%s/$STR_A/$STR_B/gc<CR> 

但我想知道是否有某種方式來做宏(1)和CopyAppend。像某種的.vimrc定義的函數:

function! CustomScript1() 
... 
" TODO: vim commands here 
... 
endfunction 

map $KEYBOARD :call CustomScrip1()<CR> 

那麼你會如何改變上述UltraEdit的宏成VIM功能?

我真的需要重新創建這種在VIM腳本的能力,因爲我目前堅持做20替代(有時更多),並取代......這是推動我堅果浪費我的時間做這樣的不雅解析日誌文件的解決方案!

回答

4

如果你有興趣在所有的模式的出現,嘗試使用:v刪除所有其他行:

:v/pattern/d_ 

如果你只在第一個興趣,嘗試要麼/search()。在一個功能中,它看起來像這樣:

function! CustomScript1() 
    " search where your pattern is 
    let l = search(pattern) " move to the line found -- i.e., no 'n' flag 
    " get rid of everything else 
    if l > 0 
     if l>1 
      :0,-d_ 
     endif 
     if l != line('$') 
      :+,$d_ 
     endif 
    endif 
    " format as CSV 
    :s/\D\+/ , /g 
endfunction