2010-04-07 69 views
6

什么是删除文本的最有效的方法2010-04-07 14:25:50,773调试这是一个调试日志语句 -从类似下面使用Vim的提取日志文件?使用Vim重复删除主要文本的最有效方法是什么?

 
2010-04-07 14:25:50,772 DEBUG This is a debug log statement - 9,8 
2010-04-07 14:25:50,772 DEBUG This is a debug log statement - 1,11 
2010-04-07 14:25:50,772 DEBUG This is a debug log statement - 5,2 
2010-04-07 14:25:50,772 DEBUG This is a debug log statement - 8,4 

这是结果应该是什么样子:

 
9,8 
1,11 
5,2 
8,4 

注意,在此之际,我用gvim在Windows上,所以请不要提出任何UNIX程序可能更适合到任务—我必须使用Vim来完成。

回答

13

运行命令::%s/.* - //

编辑,解释:

%: whole file 
s: subsitute 
.* - : find any text followed by a space a dash and a space. 
// : replace it with the empty string. 
1

最简单的方法可能是使用宏。按qa开始记录a。然后按照习惯Vim的方式去除角色。按q停止录制。然后,取行数并追加@a,例如4,按[email protected]。这将重复4次宏。

2

我的解决办法是:

qa     [start recording in record buffer a] 
^ (possibly twice) [get to the beginning of the line] 
d3f-    [delete everything till - including] 
<del>    [delete the remaining space] 
<down>    [move to next line] 
q     [end recording] 
<at>a    [execute recorded command] 

然后,只需按住<at>,直到你完成,让自动按键重复做为你工作。

注意:
事件虽然录制的宏可能并不总是最快和最完美的工具,但它通常比录制和执行宏更容易,而不是查找更好的东西。

+0

它应该是'd3f-',因为所有的* last *(3rd)dash都应该被删除。无论如何,非常好的解决方案。 :-) – 2010-04-07 14:21:11

+0

谢谢,不知何故错过了日期中的破折号。后更正。 – dbemerlin 2010-04-07 20:03:17

0

你可以这样做:

1,$s/.*- // 
  • 1:1号线
  • $:最后一行
  • s:替代
  • .*:什么

因此它在每行代替任何后面连字符和空格的任何东西。

+0

因为你只希望替换每行发生一次,所以我会放弃'g'。 – 2010-04-07 14:01:58

+0

@判断:你说得对。谢谢:) – codaddict 2010-04-07 14:05:25

6

你也可以使用可视块模式,选择您要删除的字符:

gg  Go to the beginning of the file 
Ctrl-v Enter visual block mode 
G   Go to the end of the file 
f-  Go to dash 
<right> Go one more to the right 
d   Delete the selected block 
+0

不应该是Shift + V进入视觉模块模式?此外,它需要匹配第三个破折号,因为时间戳包含两个破折号。 – 2010-04-07 14:14:33

+0

Shift-v用于视觉线模式。 Ctrl-v确实是可视块模式。 – 2010-04-07 14:19:11

+0

'Shift + v'(它是'V')用于进入视线模式。 – wRAR 2010-04-07 14:20:22

0

:%s/.\{-}\ze\d\+,\d\+$//

即由固定到逗号命令工作在该行的末尾分隔的数字,因此即使除字符串中的这些数字之外的其他所有内容都更改为,它也可以工作。

视觉块的方式可能是最简单的解决方案,我会用。但是,这是行不通的,如果行要成为交错,如:

 
2010-04-07 14:25:50,772 DEBUG This is a debug log statement - 9,8 
2010-04-07 14:25:50,772 DEBUG This is another debug log statement - 9,8 

的分隔符也可以更改为不同的性格让一个行将再看看这样的:

2010-04-07 14:25:50,772 DEBUG This is a debug log statement | 9,8

然后使用:%s/.* - //不起作用。

解释正则表达式:

.\{-}比赛什么的,不包括换行,尽可能少

\ze停止匹配,所以更换不会影响下面的字符

\d\+,\d\+$数字,在至少一个,后跟一个逗号,后跟数字,至少一个,并且行末

当然,如果所需va的格式不起作用最后一行的结果是不稳定的,在这种情况下,如果直到值的行长度相同或者分隔符相同,则其他解决方案可以工作。

相关问题