views:

244

answers:

5

Hi,

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.

+1  A: 

In my version of vi, I can do the following to remove colons from a line. YMMV.

:s/://g
jbourque
Beat me to it.I'm using GVim and it works. Not sure about vi on *nix.
Jason Down
The question actually asks for removing the timestamp, not the single colon.
Adriano Varoli Piazza
I took from the question that the colon was the problem and that the OP knew how to handle the rest. Also the title of the question is 'remove colon using VI'. So, this is the minimalist interpretation of that. :)
jbourque
+3  A: 

Normally, vi uses the character that follows the command letter as seperator.

Try this:

s!24:00:00 CDT!!g
Juergen
unless every line has the same timestamp, that's not very useful.
Adriano Varoli Piazza
true but it was enough to solve this particular problem
homerjay
+2  A: 

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
Adriano Varoli Piazza
A: 

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
Gavin Gilmour
+4  A: 

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.

Pod