Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vi command for adding character

11 views
Skip to first unread message

geoffhazel

unread,
Mar 19, 2012, 12:49:45 PM3/19/12
to
I have a file like this
02/1/2012
02/2/2012
02/3/2012
03/1/2012
03/2/2012
03/3/2012

I want it to be 02/01/2012 and so forth, MM/DD/YYYY format with extra
0's as placeholders.

I can change all the /1/ to /01/ with a command :%s?/1?/01? but what
I want is to be able to do is to insert a 0 in front of all the cases
where /./ happens. I can find the patterns with /./ but how do I
preserve the characters that are there? I seem to recall there's a way
to specify those with some regex but can't recall the details.

Ivan Shmakov

unread,
Mar 19, 2012, 3:04:17 PM3/19/12
to
>>>>> geoffhazel <geoff...@gmail.com> writes:

[Cross-posting to news:comp.editors and setting Followup-To:
there, for the question is hardly Unix-specific.]

> I have a file like this

> 02/1/2012
> 02/2/2012
> 02/3/2012
> 03/1/2012
> 03/2/2012
> 03/3/2012

> I want it to be 02/01/2012 and so forth, MM/DD/YYYY format with extra
> 0's as placeholders.

[...]

> I can find the patterns with /./ but how do I preserve the characters
> that are there? I seem to recall there's a way to specify those with
> some regex but can't recall the details.

One can reference a \(\)-group from within the s-command's
replacement text with \1 ... \9, like:

s;/\([0-9]/\);/0\1;

--
FSF associate member #7257

Harry

unread,
May 7, 2012, 10:57:34 PM5/7/12
to
geoffhazel wrote...
>
>I have a file like this
>02/1/2012
>02/2/2012
>02/3/2012
>03/1/2012
>03/2/2012
>03/3/2012
>
>I want it to be 02/01/2012 and so forth, MM/DD/YYYY format with extra
>0's as placeholders.

:1,$s#/#/0#


Rui Maciel

unread,
May 8, 2012, 5:07:34 AM5/8/12
to
If there is a line in the file with a date from october, november or
december, that search pattern will cause problems such as:

03/011/2012


The following search pattern won't:

:1,$s#/\([1-9]\/\)#/0\1#


Rui Maciel
0 new messages