LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-11-2008, 12:32 PM   #1
ufmale
Member
 
Registered: Feb 2007
Posts: 385

Rep: Reputation: 30
tap character in vi


How do I replace the first space of each line with tap in vi?
For example, i have

Name phone zip
John 243-3244 44234
Tim 523-2342 55242

I want the space between Name and phone number to be replace by tap.
is there an easy way in vi?
 
Old 08-11-2008, 12:39 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,977
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Assuming that it's ALWAYS going to be "Name phone" then I will try...

Code:
%s/Name phone/Name tap phone/g
 
Old 08-11-2008, 12:43 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
I think the OP means TAB (not tap) character.

Use \t (backslash t) as the tab character.

:%s/Name phone/Name\tphone/g
 
Old 08-11-2008, 12:44 PM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Do you mean a tab character in the first space on each line? If so, start the editor then
Code:
:g/ /s//<tab>/
where <tab> is the tab key on your keyboard.

If you wanted to replace all (not just the first)
Code:
:g/ /s//<tab>/g
 
Old 08-11-2008, 01:33 PM   #5
ufmale
Member
 
Registered: Feb 2007
Posts: 385

Original Poster
Rep: Reputation: 30
Thank you for the quick response.. I meant <tab>

The method from tronayne seems to work, but I want to replace only the first space of each line. How do I do that? do I have to write a script to do it?

if I also want to have a index line number as well, is that a way to do it?

Here is an example of output i really need. Can anyone help?

1 <tab> John <tab> 243-3244 44234
2.<tab> xxx <tab> xxx xxx
 
Old 08-11-2008, 01:52 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Remove the g flag from the end of the substitution.

Here's one way to number a range of lines:

Go to the first line that you want numbered. Type ma to place a Mark labeded a.
Go to the last line you wanted numbered. Type !'acat -n and hit Enter. This will send all the lines between the first and current line to cat -n, replacing your output.
 
Old 08-11-2008, 02:19 PM   #7
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Quote:
The method from tronayne seems to work, but I want to replace only the first space of each line. How do I do that? do I have to write a script to do it?
Code:
:g/ /s//<tab/
replaces (or substitutes, which is what the s in the expression stands for) only the first space (as indicated); no, you do not have to write a script or anything else to do that (the additional g at the end of the line in the second example given would replace all spaces in every line).

If you want to insert a <tab> at the beginning of a line
Code:
:g/^/s//<tab>/
will do that.

If you wanted to insert a <tab> at the end of a line,
Code:
:g/$/s//<tab>/
would do that.

The caret (^) stands for beginning of line, the dollar sign ($) stands for end of line.

The vi editor uses regular expressions for editing; take a look at http://www.regular-expressions.info/reference.html for examples.
 
Old 08-11-2008, 07:07 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Replace first space on each line:

:%s/ /<tab>/

where <tab> means hit tab key once
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about wide character and multiple byte character George2 Programming 5 05-23-2006 01:03 AM
Using Tap Interface Kenji Miyamoto Linux - Networking 1 02-05-2006 04:12 PM
help using tap device sunnos9 Linux - Networking 0 05-01-2005 12:18 AM
Tap Drive imsajjadali Linux - General 9 02-07-2004 06:12 AM
Tap Drive imsajjadali Linux - Distributions 2 01-26-2004 06:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration