2010-04-07 35 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的格式不起作用最後一行的結果是不穩定的,在這種情況下,如果直到值的行長度相同或者分隔符相同,則其他解決方案可以工作。

相關問題