LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-18-2010, 02:01 PM   #1
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Rep: Reputation: 15
Repacing a string at a specific location with another in Vi (gVim)


If i have a comma separated string like this one

Code:
aa,bb,cc,dd,ee
11,22,dd,3f,22
How can i replace the value on the 4th column with a specific value. For example if i want all values in the 4th column to be '55' the output would be

Code:
aa,bb,cc,55,ee
11,22,dd,55,22
Is this possible in VI or more specifically gVim on windows?

Thnks
 
Old 03-18-2010, 02:18 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
vim
Code:
%s/\([^,][^,]*,[^,][^,]*,[^,][^,]*,\)[^,][^,]*\(,[^,][^,]*\)/\155\2/
 
Old 03-18-2010, 02:54 PM   #3
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
In general, what you need are regular expressions. You would benefit from finding a good tutorial and learning them, and then reading about how Vim uses them: :help usr_27.txt and :help regular-expression.
 
Old 03-18-2010, 04:20 PM   #4
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
vim
Code:
%s/\([^,][^,]*,[^,][^,]*,[^,][^,]*,\)[^,][^,]*\(,[^,][^,]*\)/\155\2/
Hi Tinkster,

Could you explain what exactly is happening here.. Wouldnt this work only if the content is fixed length?

Thanks
 
Old 03-18-2010, 04:22 PM   #5
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Original Poster
Rep: Reputation: 15
Talking

Quote:
Originally Posted by penguiniator View Post
In general, what you need are regular expressions. You would benefit from finding a good tutorial and learning them, and then reading about how Vim uses them: :help usr_27.txt and :help regular-expression.
Thanks. I do try to learn them but the problem is i rarely use them so i just forget about them every time i learn them and have to refresh my memory. I do know the basics but not something as tough as this though
 
Old 03-18-2010, 04:37 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by ziggy25 View Post
Hi Tinkster,

Could you explain what exactly is happening here.. Wouldnt this work only if the content is fixed length?

Thanks

What do you mean by "fixed length"? Only if there's 5 columns? Yes.
But that's easily fixed.

Atm we're looking for (and remembering):
Any number of anything not a comma followed by a comma (3x)(remember)
followed by anything not a comma
followed by a command and anything not a comma (remember)
and replace with
remembered1 55 remembered2


If the number of columns after the fourth were vaiable:


Code:
%s/\([^,][^,]*,[^,][^,]*,[^,][^,]*,\)[^,][^,]*\(,.*\)/\155\2/



Cheers,
Tink
 
Old 03-18-2010, 06:44 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
One of the tricks with regexen is, when you've got one that works, save the instructions/example in a file for future ref. After a while you build up a collection which you can often amend to use on your next problem.
Once you got a bunch in one file, with an example usage of each, you'll start to see how they work.
 
  


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
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
changing to a specific folder, one deeper than specified location PiNPOiNT Programming 4 04-28-2009 04:29 PM
write text to specific location in file mcbenus Linux - Desktop 3 02-28-2008 06:40 AM
Untar File to a specific location ! Libu Linux - General 3 12-19-2005 07:14 PM
Mount hardware to specific location with specified device blixel Linux - Hardware 5 10-21-2003 04:21 PM

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

All times are GMT -5. The time now is 02:51 AM.

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