On 01/10/2011 11:02 AM, jbw wrote:
I'm trying to quickly change the date format in a downloaded csv file.

Is there a way to change the following   "12/27/2010"  -->  "2010/12/27"
with one substitution command.

The first thing to know in this case is that the delimiter character can be a wide variety of characters, it doesn't have to be "/". Second, you want to tag the bits of interest and then reassemble them. The final transformation would look something like

 :%...@\(\d\{1,2}\)/\(\d\{1,2}\)/\(\d\{4}\)@\3/\1/\...@g

which breaks down as

 :%...@pattern@replacem...@g

where "pattern" is

 \(\d\{1,2}\)   capture 1-2 digits (the month)
 /              the literal slash delimiting the date(*)
 \(\d\{1,2}\)   capture 1-2 digits (the day)
 /              the literal slash delimiting the date(*)
 \(\d\{4}\)     capture 4 digits (the year)

and "replacement" is

  \3/\1/\2

where the "\3" is the year, the "\2" is the day and the "\1" is the month, adding the "/" delimiters between them. If you wanted to use dashes instead (more common for this YMD format), it would be

  \3-\1-\2

The search-pattern is intentionally a little lax for ease of reading. It does allow for bogus dates like "99/88/0000" but it transforms them all the same to "0000/99/88".

The second substitution question I have is how to I perform a substitution
but just add something to the pattern instead of completely replacing it.
For the same csv file i want to add a newline before the pattern and then
add some words after the pattern.  I remember seeing how to do this before
but can't find the instructions anymore

for example.

change  the following

one  $100

to this

one
   $100 two three four

Just add the newline ("\r" in the case of the replacement) followed by the text, using "&" to stand in for the matched-text. This assumes that it's the *same* added-text for every line. Thus the above would become

  :%s/\s\+\$100/\r& two three four

Hope this helps,

-tim


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to