Question
I am trying do a find and replace in VI to remove a timestamp. I usually do this in VI using the S command but how do I tell VI I need to remove colons when its part of the structure of the VI command itself
EX: " xxxxx xxxxx 24:00:00 CDT"
tried
s:24:00:00 CDT::g
s:"24:00:00 CDT"::g
s:/:::g
Any assistance is appreciated.
Solution
Normally, vi uses the character that follows the command letter as seperator.
Try this:
s!24:00:00 CDT!!g
OTHER TIPS
The problem here is not matching a colon, but that you've been taught that vim MUST use a colon to seperate it's regex. This is incorrect.
awk/vi/perl/ruby (and many more) let you specify wahtever delimiter that you want. This character is the one following the command character (in our case an S), eg:
s/hello/there/
s:hello:there:
s@hello@there@
are all the same regex, just with different delimiters. This flexability means that if you often use /, but you then need to match a / in the regex, then you can just switch to some other delimiter, eg:
sMhel/loMthereM
Though "M" might not be the best choice when the regex contains text -- it depends on your style and what you're matching really.
You can even use brackets. For a single regex it is:
s[hello]
or
s(hello)
I think for the search and replace style you can use s[hello][there]
or possibly even s[hello](there)
. But this last sentence about the brackets is a half remembered guess from when I alst used perl.
In my version of vi, I can do the following to remove colons from a line. YMMV.
:s/://g
s/\d\+:\d\+:\d\+ CDT//g
works for me:
initial content:
xxxxx xxxxx 24:00:00 CDT
after command:
xxxxx xxxxx
if you want to be sure it will only affect timestamps (as is, that regex above changes any number of digits > 1), use
s/\d\d:\d\d:\d\d CDT//g
where the final g changes all occurrences of the pattern, not just the first one.
If you have more than one timezone in the list, group them:
:s/\d\+:\d\+:\d\+^Y \(CDT\|UDT\)//g
It's not really part of the command, since it should let you use any delimiter you like.
I'd probably try:-
%s/\d\{2}:\d{2}:\d{2}//g