Inserting a character in certain column

Michael Ulm michael.ulm at isis-papyrus.com
Wed Feb 27 09:14:47 UTC 2008


Szemerédy Gábor wrote:
> Michael Ulm wrote:
>> Alexandra Zaharia wrote:
>>   
>>> On 2/25/08, Szemerédy Gábor <gaborszem at eccf.su.ac.yu> wrote:
>>>     
>>>> Hello list!
>>>>  I need to insert a character in certain column in all rows of the file
>>>>  using vi.
>>>>  Please help
>>>>
>>>>       
>>> If I understand correctly, suppose you have the file
>>>
>>> aaaa
>>> bbbb
>>> cccc
>>> dddd
>>>
>>> and you want to turn it into
>>>
>>> aaaXYa
>>> bbbXYb
>>> cccXYc
>>> dddXYd
>>>
>>> Then here's what you have to do in vi:
>>>
>>>     
>> --snip--
>>
>> or use a regexp substitution. In command mode type
>>
>> :%s/^.\{3}/&XY/
>>
>> HTH,
>>
>> Michael
>>
>>   
> Thank you but it gives me
> "RE error : parentheses not balanced.
> Gabor
> 


Are you sure you entered the regexp correctly? I have just verified
that the expression I gave works with various versions of vi/vim.
Things to look out for are to use curly braces {} not parentheses (),
and not to forget any character in the expression.

Here a more detailed explanation of the command:

:%s is the command
:  to enter the command line mode
%  to tell vim that the next command should be applied to the whole document
s  is the substitute command. See the helptext (:h :s) for details

^.\{3} is the matching regular expression. It reads like this:
^    start from the beginning of the line
.    match any character
\{3} exactly 3 times.

&XY is the substitution.
&    put in the matching term (i.e. the 3 characters at the beginning of the
     line that matched the regular expression)
XY   then put in the literal string XY

HTH,

Michael




More information about the ubuntu-users mailing list