- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Insert string at specific column in VI
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 11:18 AM
01-20-2003 11:18 AM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 11:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 11:45 AM
01-20-2003 11:45 AM
Re: Insert string at specific column in VI
: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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 11:50 AM
01-20-2003 11:50 AM
Re: Insert string at specific column in VI
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 11:58 AM
01-20-2003 11:58 AM
Re: Insert string at specific column in VI
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 12:05 PM
01-20-2003 12:05 PM
Re: Insert string at specific column in VI
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 12:07 PM
01-20-2003 12:07 PM
Re: Insert string at specific column in VI
Just conjecture, Harry's obviously got the ultimate solution.
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2003 12:13 PM
01-20-2003 12:13 PM
Re: Insert string at specific column in VI
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2004 08:21 AM
08-02-2004 08:21 AM
Re: Insert string at specific column in VI
Syntax of script is as follow:
sed_insert_column.sh
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.