我的具体案例是一个包含大量文本和IPv4地址的文本文档.我想删除除IP地址以外的所有内容.
我可以:vglobal用来搜索([0-9]{1,3}\.){3}[0-9]{1,3}和删除没有IP地址的所有行,但之后我只知道如何搜索整行并选择匹配的文本.有没有更简单的方法.
简而言之,我正在寻找一种方法来执行以下操作而不使用外部程序(如grep):
grep --extended-regexp --only-matching --regexp="([0-9]{1,3}\.){3}[0-9]{1,3}"
Run Code Online (Sandbox Code Playgroud)
从vim调用grep可能需要调整我的正则表达式(例如:remove\v).使用vim的增量搜索向我显示我的模式是正确的,我也不想在grep中验证我的正则表达式.
编辑:感谢彼得,这是我现在使用的功能.(C是我在函数中常常使用的寄存器.)
"" Remove all text except what matches the current search result
"" The opposite of :%s///g (which clears all instances of the current search).
function! ClearAllButMatches()
let old = @c
let @c=""
%s//\=setreg('C', submatch(0), 'l')/g
%d _
put c
0d _
let @c = old
endfunction
Run Code Online (Sandbox Code Playgroud)
Edit2:我把它作为一个接受范围的命令(但默认为整个文件).
"" Remove all text except what matches the current search result. Will put each
"" match on its …Run Code Online (Sandbox Code Playgroud) 我想用vim从文本中提取一些数据..数据属于这种类型:
72" title="(168,72)" onmouseover="posizione('(168,72)');" onmouseout="posizione('(-,-)');">>
72" title="(180,72)" onmouseover="posizione('(180,72)');" onmouseout="posizione('(-,-)');">>
72" title="(192,72)" onmouseover="posizione('(192,72)');" onmouseout="posizione('(-,-)');">>
72" title="(204,72)" onmouseover="posizione('(204,72)');" onmouseout="posizione('(-,-)');">>
Run Code Online (Sandbox Code Playgroud)
我需要提取的数据包含在:title ="(168,72)"中.
特别是我感兴趣的是只提取这些坐标.
我虽然在使用vim之前首先删除了title =" ..但我不是真正的正则表达式的大师..所以我问你:如果有人有任何提示:请告诉我:)
我有一个像这样的XML文件:
<fruit><apple>100</apple><banana>200</banana></fruit>
<fruit><apple>150</apple><banana>250</banana></fruit>
Run Code Online (Sandbox Code Playgroud)
现在我要删除文件中的所有文本,除了标签apple中的单词.也就是说,该文件应包含:
100
150
Run Code Online (Sandbox Code Playgroud)
我怎么能得到这个?
我无法为我的具体问题找到任何合理的答案;我知道如何处理 vim/sed 的替换搜索,但是我们如何处理 vim 中关于列模式搜索和替换的 csv。即我们的 csv 数据块如下:
automotive_bitcount,1,1,0,1,0,0,0,0,1,0,0,1,1,0,1,0,1,1,1,1,0,0,1,0,0
automotive_bitcount,1,0,0,1,0,1,0,1,1,1,0,0,0,0,1,1,1,0,1,0,1,0,0,1,0
automotive_bitcount,2,1,0,0,0,0,1,0,1,0,1,1,0,0,0,0,0,0,1,1,1,1,0,1,0
automotive_bitcount,2,0,0,0,1,1,0,0,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0
automotive_bitcount,2,1,0,0,0,1,1,0,1,0,1,1,0,0,1,1,1,0,1,1,1,0,1,1,1
Run Code Online (Sandbox Code Playgroud)
它代表标题:
APP_NAME, DATASET, COMPILER FLAGS#1,...,COMPILER FLAG#24
Run Code Online (Sandbox Code Playgroud)
这是搜索和替换的语句;我想用相应的“编译器标志选项”(我把它放在这里)替换列中的那些“1”,所以最后我可以有类似这样的结构,以便将它们传递给编译器:
automotive_bitcount dataset1 -fno-guess-branch-probability -fno-ivopts -fno-tree-loop-optimize -fno-inline-functions -fno-omit-frame-pointer -falign-jumps -fselective-scheduling -fno-tree-pre -fno-move-loop-invariants
Run Code Online (Sandbox Code Playgroud)
仅供记录,24 个编译器标志如下(按顺序排列):
compilerOptionList= "-funsafe-math-optimizations -fno-guess-branch-probability -fno-ivopts -fno-tree-loop-optimize -fno-inline-functions -funroll-all-loops -fno-omit-frame-pointer -falign-jumps -fselective-scheduling -fno-inline-small-functions -fno-tree-pre ftracer -fno-move-loop-invariants -O2 -fno-tree-ter -fprefethch-loop-arrays -max-unrolled-insns -fno-inline-functions-called-once -fno-cprop-registers -finline-functions -fno-schedule -fno-align-functions -fno-tree-dce -fno-merge-constants"
Run Code Online (Sandbox Code Playgroud)