Operating System - HP-UX
1828217 Members
2246 Online
109975 Solutions
New Discussion

Insert string at specific column in VI

 
SOLVED
Go to solution
Tom Weber_4
Advisor

Insert string at specific column in VI

This is probably easy for you VI experts out there. Say I have a file as such:

111111
222222
333333
444444

I want to insert "xx" at column 4 in each line to look like this:

111xx111
222xx222
333xx333
444xx444

I know doing a :1,$s/^/xx/ will add the "xx" to the beginning of each line but I want to insert the string at a specific column.

I know there are other methods of doing this using other utilities, but I'm interested in just VI for this example.

Thanks in advance!
8 REPLIES 8
harry d brown jr
Honored Contributor
Solution

Re: Insert string at specific column in VI

s/\(...\)\(.*\)/\1xx\2/99999


live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: Insert string at specific column in VI

or to nail any column without counting your periods:

:s/\(.\{3\}\)\(.*\)/\1xx\2/9999

replacing the "3" with whatever column you want.

You can EASILY do this with sed also:

sed "s/\(.\{3\}\)\(.*\)/\1xx\2/" < filename > newfilename

live free or die
harry
Live Free or Die
Tom Weber_4
Advisor

Re: Insert string at specific column in VI

Perfect! Thanks for your help Mr. Brown!
John Poff
Honored Contributor

Re: Insert string at specific column in VI

Hi,

You can do a '4|' using the vertical bar to take you to column four, but I don't know of any way to get that into an insert command, so you'd have to goto the column and then do the insert, and then move to the next line manually, move to the fourth column, and do a repeat on the insert. My fingers already hurt just thinking about that method.

Now, I know you want to do it with just vi commands, but here is a combination hack that uses a vi command to send the lines to awk to do it. It works on lines 1 through 4 as an example:

:1,4!(awk '{print substr($0,1,3) "xx" substr($0,3)}')


It's ugly, but it's one way to do it.

JP
Tom Weber_4
Advisor

Re: Insert string at specific column in VI

John, I didn't note this would be for a very large file not a 4 liner like my example. I think the manual method would be palatable for a file that small but I agree it wouldn't be pleasant with say 10,000 lines : )
Pete Randall
Outstanding Contributor

Re: Insert string at specific column in VI

Here's a thought for you, John. You can use the :map command to define a macro: "map key cmd_sep". So, if you define a macro that does your "4|" , followed by "i text" to insert the desired test, followed by a "+" for beginning of next line, all you'd have to do would be to hit you map key (over and over and over).

Just conjecture, Harry's obviously got the ultimate solution.

Pete

Pete
John Poff
Honored Contributor

Re: Insert string at specific column in VI

Tom,

Yeah, it would probably be a dog for a large file. Harry's solution with the regular expression is much more elegant.

Pete,

I looked up the macros. I haven't done one before but it sure would be handy.


Someday I'm gonna die and I'll be standing in front of the pearly gates. Before they let me in, they ask me one question, "Can you write a regular expression to insert some text at a certain column?"

Darn. :)

JP
Jack C. Mahaffey
Super Advisor

Re: Insert string at specific column in VI

I know this has been answered but wanted to share a script that I created to simplify this task a little bit. It's not very pretty but it's easy to use.

Syntax of script is as follow:

sed_insert_column.sh "text"

example: sed_insert_column.sh 4 /tmp/file1 "xx"

Script does not save the file, redirection will be needed.

Here's the script. It also includes two other scripts in the file for verifying the first argument is a number.