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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-10-2010, 03:55 PM
|
#1
|
Senior Member
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098
Rep: 
|
Search and replace problem with VIM
I am trying to search a series of text files where each line is supposed to contain the following
record, lastname, firstname, cd
however some records have
record, lastname, firstname, extra character, cd
I am trying to use vim to remove the extra comma. Does anyone know what I am doing wrong Here is what I am typing in vim
:%s/\(\(\w,\)\(\w,\)\(\w,\)\(\w,\)/ <here is where I am lost>
Also I am not very good with regular expression.
Found out what I did wrong.
%s/\(\w\+\),\(\w\+\),\(\w\+\),\(\w\+\),\(\w\+\)/\1, \2,\3 \4,\5/g
this is working.
Last edited by jstephens84; 02-10-2010 at 04:11 PM.
Reason: Solved
|
|
|
02-10-2010, 03:59 PM
|
#2
|
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833
Rep: 
|
Try keeping it simple:
:%s/, extra character, /, /g
(I realize you didn't mean the words 'extra character' but without seeing an example of your data it's hard to be more specific than you were, you could use a wild card character though too.)
Last edited by rweaver; 02-10-2010 at 04:12 PM.
|
|
|
02-18-2010, 08:34 PM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
I know this has already been solved but M2C would be to use awk:
awk -F, '{ OFS = ","; if (NF > 4) print $1, $2, $3, $NF; else print $0 }' file > new_file
|
|
|
All times are GMT -5. The time now is 01:38 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|