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 02-10-2010, 03:55 PM   #1
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
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
 
Old 02-10-2010, 03:59 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
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.
 
Old 02-18-2010, 08:34 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


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
Help with Regular Expression in VIM search and replace vijay_babu1981 Linux - Newbie 8 10-22-2009 07:49 AM
vim search and replace only one line hakmed Programming 5 11-07-2006 07:13 PM
LXer: Vim tips: The basics of search and replace LXer Syndicated Linux News 0 06-29-2006 08:54 AM
vim: search pattern / replace with file naflan Programming 5 01-07-2006 03:16 PM
vim search and replace - split line phreak7t7 Programming 2 08-16-2004 05:02 PM

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

All times are GMT -5. The time now is 08:48 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