Manikandaraj Srinivasan
Manikandaraj Srinivasan

Reputation: 3647

VIM : How to delete certain char range in every line

I'm trying to delete certain character range in each line of a file using VIM.

Sample file,

"2021-11-29T06:35:04.300-0500",01,342864
"2021-11-29T06:35:20.252-0500",54,332323
"2021-11-29T06:35:20.000-0500",63,513133

I want the result to be like this,

"2021-11-29T06:35:04",01,342864
"2021-11-29T06:35:20",54,332323
"2021-11-29T06:35:20",63,513133

I want to delete the characters after the seconds[.300-0500, .252-0500, .000-0500] in every line, throughout the file, i.e, char range 21-30.

How do I do this ?

I understand I can delete the char range from the start of the line or in the end of the line using below,

:%s/^.\{0,5\}//
:%s/.\{1}$//

Upvotes: 1

Views: 767

Answers (2)

Marth
Marth

Reputation: 24802

You can use \%Xv to match (virtual) column X, so:

:%s/\%21v.*\%31v//

will delete characters between columns 21 and 30.

Upvotes: 4

mattb
mattb

Reputation: 3053

You could use:

:%s/\.\d\{3\}-\d\{4\}//

or

:%norm 20ld9l

That's move 20 columns to the left (from column 1, to column 21) and then delete from there until 21+9 = 30

Upvotes: 1

Related Questions