The easiest way would probably be using a macro. Press qa
to start a recording in register a
. Then remove the characters the way you're used to in Vim. Press q
to stop recording. Then, take the amount of lines and append @a
, for example for 4, press 4@a
. This will repeat the macro 4 times.
views:
197answers:
6Run 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.
My solution would be:
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 are done and let automatic key repeat do the work for you.
Note:
Eventhough recorded macros might not always be the fastest and most perfect tool for the job it's often easier to record and execute a macro than to lookup something better.
You can do:
1,$s/.*- //
1
: line 1$
: last lines
: substitution.*
: anything
So it replaces on each line anything followed by hyphen and space with nothing.
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
:%s/.\{-}\ze\d\+,\d\+$//
That command works by anchoring to the comma separated numbers at the end of the line, therefore it works even if everything else but those numbers in the string change.
The visual block way is probably the easiest solution and the one I'd use. But, it doesn't work if the lines were to become 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 delimeter might also change to a different character so a line would then look like:
2010-04-07 14:25:50,772 DEBUG This is a debug log statement | 9,8
Then using :%s/.* - //
would not work.
Explanation for the regex:
.\{-}
matches whatever, excluding newlines, as few as possible
\ze
stops matching, so the replace doesn't affect the following characters
\d\+,\d\+$
digits, at least one, followed by a comma, followed by digits, at least one, and the end of line
Naturally it doesn't work if the format of the desired values in the end of the line is erratic, in which case the other solutions work if the lines up to the values are of same length or the delimeters are the same.