Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient way to repeatedly remove leading text using Vim?

Tags:

vim

What's the most efficient way to remove the text 2010-04-07 14:25:50,773 DEBUG This is a debug log statement - from a log file like the extract below using 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

This is what the result should look like:

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

Note that on this occasion I'm using gVim on Windows, so please don't suggest any UNIX programs which may be better suited to the task—I have to do it using Vim.

like image 393
John Topley Avatar asked Apr 07 '10 13:04

John Topley


People also ask

How do I delete all lines above VI?

Delete All Lines Press the Esc key to go to normal mode. Type %d and hit Enter to delete all the lines.


2 Answers

Run the command: :%s/.* - //

Edit, explanation:

%: whole file
s: subsitute
.* - : find any text followed by a space a dash and a space.
// : replace it with the empty string.
like image 74
Stephen Avatar answered Nov 03 '22 05:11

Stephen


You could also use visual block mode to select all the characters you want to delete:

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
like image 5
sth Avatar answered Nov 03 '22 06:11

sth