LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2006, 10:31 AM   #1
zwylinux
LQ Newbie
 
Registered: May 2006
Posts: 27

Rep: Reputation: 15
about the vi editor


I have a file,the contents as follows:
$ cat myfile
1,Tom,20
2,Jim,18
3,Lily,19


with the vi editor ,how can i change the content as
"1","Tom",20
"2","Jim",18
"3","Lily",19

Please help me,many thanks.
 
Old 05-27-2006, 10:49 AM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
The editor vi has a little tutorial that doesn't take too long (~30 mins). To access it just type:

Code:
vimtutor
That will show you the basics of using vim.

Regards,

Alunduil
 
Old 05-27-2006, 10:51 AM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Well try this first, close the editor for a second and run this command on the file
Code:
awk -F, '{ print "\"" $1 "\",\"" $2 "\"," $3 }' filename >newfilename
 
Old 05-27-2006, 11:23 AM   #4
zwylinux
LQ Newbie
 
Registered: May 2006
Posts: 27

Original Poster
Rep: Reputation: 15
But how to carry out this opening the file with vi?
 
Old 05-27-2006, 12:34 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by zwylinux
But how to carry out this opening the file with vi?
Sigh,

1.) Open vi
Code:
$ vi
2). Open your file
Code:
:e ./myfile
3). Process all of your file through awk
Code:
:%!awk -F, '{ print "\"" $1 "\",\"" $2 "\"," $3 }'
4). Write and quit
Code:
:wq
 
Old 05-27-2006, 01:12 PM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
here's one way to do it in vi:
Code:
:%s/\([^,]*\),\([^,]*\)/"\1","\2"/
maybe someone else can do it prettier
 
Old 05-27-2006, 01:14 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Sigh,

1.) Open vi
Code:
$ vi
2). Open your file
Code:
:e ./myfile
3). Process all of your file through the substitute command
Code:
:%s/^\([0-9]*\),\([a-zA-Z]*\),\([0-9]*\)$/"\1","\2",\3
4). Write and quit
Code:
:wq
 
Old 05-27-2006, 10:36 PM   #8
zwylinux
LQ Newbie
 
Registered: May 2006
Posts: 27

Original Poster
Rep: Reputation: 15
Many thanks!
 
Old 05-28-2006, 09:15 AM   #9
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
If you use vim rather than vi you can use :x to save and exit.
 
Old 05-28-2006, 09:25 AM   #10
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
But ":wq" is classic !
 
Old 05-28-2006, 09:33 AM   #11
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Rep: Reputation: 62
Quote:
1. If you use vim rather than vi you can use :x to save and exit.
2. But ":wq" is classic !
When I first learned vi, I learned that "ZZ" from command mode does the same thing. Sorry. I just can't give it up.
 
Old 05-28-2006, 11:04 AM   #12
zwylinux
LQ Newbie
 
Registered: May 2006
Posts: 27

Original Poster
Rep: Reputation: 15
vim is really powerful,maybe we neet to talk about the vim editor another time.
 
Old 05-28-2006, 12:35 PM   #13
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by zwylinux
vim is really powerful,maybe we neet to talk about the vim editor another time.
If you use vim rather than vi, you have regexps that match digits (i.e., instead of [0-9] you have \d) and alphabetic characters (i.e., \a instead of [a-zA-Z]), and a whole lot more tools at your disposal.
 
  


Reply



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
vi editor IwantLINUX Linux - Newbie 8 06-28-2005 10:28 AM
C editor MassiveBlood Linux - Software 3 10-16-2004 08:24 AM
What editor can be used on RH 9.0 satimis Red Hat 2 06-17-2004 05:36 PM
Vi editor tmurray Linux - General 5 03-12-2004 10:33 PM
Vi Editor BittaBrotha Linux - Software 1 02-09-2003 12:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:54 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
Open Source Consulting | Domain Registration