Colon Removal Using VI

I am trying to find and replace in a VI to remove the timestamp. I usually do this in VI using the S command, but how can I tell I need to remove the colons when the very part of the structure of the VI command itself

EX: "xxxxx xxxxx 24:00:00 CDT"

tried to

s:24:00:00 CDT::g

s:"24:00:00 CDT"::g

s:/:::g

Any help is appreciated.

+2


source to share


5 answers


Typically vi uses the character that follows the command letter as a separator.

Try the following:



s!24:00:00 CDT!!g

      

+3


source


The problem here is not colon matching, but you learned that vim MUST use a colon to separate its regex. This is not true.

awk / vi / perl / ruby ​​(and many others) allow you to specify the wahtever delimiter you want. This character corresponds to the next command character (in our case S), for example:

s/hello/there/
s:hello:there:
s@hello@there@

      

- it's still a regular expression, only with different delimiters. This flexibility means that if you use / a lot, but then you need to match / in the regex, then you can simply switch to some other delimiter, like:

sMhel/loMthereM

      

While "M" may not be the best choice when the regex contains text, it depends on your style and what you actually match.



You can even use parentheses. For a single regex, this is:

s[hello]

      

or

s(hello)

      

I think for a find and replace style you can use s[hello][there]

or maybe even s[hello](there)

. But the last sentence about parentheses is half a catchy guess when I was using perl.

+6


source


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

:s/://g

      

+2


source


s/\d\+:\d\+:\d\+ CDT//g

works for me:

initial content:

xxxxx xxxxx 24:00:00 CDT

      

after the command:

xxxxx xxxxx

      

if you want to be sure that only timestamps will be affected (since this regex will change any number of digits p> 1) use

s/\d\d:\d\d:\d\d CDT//g

      

where final g modifies all occurrences of the pattern, not just the first one.

If there are multiple time zones in the list, group them:

:s/\d\+:\d\+:\d\+^Y \(CDT\|UDT\)//g

      

+2


source


It is not part of the command as it should allow you to use whatever delimiter you like.

I would probably try: -

%s/\d\{2}:\d{2}:\d{2}//g

      

+1


source







All Articles