Regex 如何将其转换为VIM宏?
作为一名程序员,我的一项常见任务就是调试一个实时系统。调试实时系统的方法之一是从控制台捕获详细日志 通常,日志文件中我感兴趣的每一行都有大约20行额外的内容 为了最小化我的宏脚本,我开始创建一个宏,它将只获取我感兴趣的20行中的一行!(与对我不想要的所有行进行20次替换不同…这将使宏比需要的长20倍。)此宏代码的其余部分将把这一行转换为*.csv文件,以便我可以在Matlab或Excel中使用我认为合适的数字 以下是宏的代码(这些命令是超编辑特定命令): *仅供参考 让我用一个更容易理解的伪代码来解释这个宏的作用:Regex 如何将其转换为VIM宏?,regex,vim,scripting,vim-macros,Regex,Vim,Scripting,Vim Macros,作为一名程序员,我的一项常见任务就是调试一个实时系统。调试实时系统的方法之一是从控制台捕获详细日志 通常,日志文件中我感兴趣的每一行都有大约20行额外的内容 为了最小化我的宏脚本,我开始创建一个宏,它将只获取我感兴趣的20行中的一行!(与对我不想要的所有行进行20次替换不同…这将使宏比需要的长20倍。)此宏代码的其余部分将把这一行转换为*.csv文件,以便我可以在Matlab或Excel中使用我认为合适的数字 以下是宏的代码(这些命令是超编辑特定命令): *仅供参考 让我用一个更容易理解的伪代码
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>
map$键盘:%s/$STR_A/$STR_B/gc
但是我想知道是否有办法在宏中执行while(1)和CopyAppend。类似于某种.vimrc定义的函数:
function! CustomScript1()
...
" TODO: vim commands here
...
endfunction
map $KEYBOARD :call CustomScrip1()<CR>
函数!CustomScript1()
...
“TODO:这里是vim命令
...
端功能
map$键盘:调用CustomScrip1()
那么如何将上述UltraEdit宏更改为VIM函数?
我真的需要在VIM中重新创建这种脚本的能力,因为我目前一直在做20个替换(有时更多)并替换…这让我抓狂,浪费我的时间来解析日志文件!如果您对模式的所有实例感兴趣,请尝试使用
:v
删除所有其他行:
:v/pattern/d_
如果您只对第一个感兴趣,请尝试/
或搜索()
。在函数中,它将如下所示:
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
如果您对模式的所有引用感兴趣,请尝试使用
:v
删除所有其他行:
:v/pattern/d_
如果您只对第一个感兴趣,请尝试/
或搜索()
。在函数中,它将如下所示:
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