What is the most efficient way to remove leading text multiple times with Vim?

What is the most efficient way to delete text 2010-04-07 14: 25: 50,773 DEBUG Is this a debug log expression - from a log file such as extracting 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

Here is the result:

  9.8
 1,11
 5.2
 8.4

Please note that in this case I am using gVim for Windows, so please do not offer any UNIX programs that may be better suited to the task. I need to do this using Vim.

+6
vim
source share
6 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. 
+13
source share

You can also use the 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 
+6
source share

My decision:

 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] 

Then just hold <at> until you finish and enable automatic key repetition.

Note:
Despite the fact that recorded macros are not always the fastest and most perfect tool for work, it is often easier to record and execute a macro than to look for something better.

+2
source share

The easiest way is likely to use a macro. Press qa to start writing to register a . Then delete the characters the way you are used to in Vim. Press q to stop recording. Then take the number of lines and add @a , for example, for 4, press 4@a . This will repeat the macro 4 times.

+1
source share

You can do:

 1,$s/.*- // 
  • 1 : line 1
  • $ : last line
  • s : wildcard
  • .* : anything

Thus, it replaces on every line everything followed by a hyphen and a space, nothing.

0
source share

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

This command works by binding to numbers separated by commas at the end of the line, so it works even if everything else except these numbers in the line changes .

The visual block is probably the easiest solution and one that I would use. But this will not work if the lines were to be staggered, as in:

  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

The demimeter may also change to another character, so the line will look like this:

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

Then use :%s/.* - // will not work.

Explanation for regex:

.\{-} matches anyone except translation lines, as little as possible

\ze stops matching, so replacing does not affect the following characters

\d\+,\d\+$ digits, at least one followed by a comma, followed by at least one digits, and the end of the line

Naturally, this does not work if the format of the desired values ​​at the end of the line is unstable, in which case other solutions work if the lines before the values ​​have the same length or the delimiters are the same.

0
source share

All Articles