LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-21-2009, 04:04 PM   #1
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Rep: Reputation: 16
Vi Search and Replace


Hi,
Can't seem to find any docs that do a search and replace for a string within a string, so here's the post =) I'm trying to replace the word "DB9IR2" in this file with DBTEST, but I always get pattern not found, I think its because it is considering it as a whole string per line? Here's some snippets of the file:

Code:
background_dump_dest=/home/oracle/admin/DB9IR2/bdump
 13 
 14 user_dump_dest=/home/oracle/admin/DB9IR2/udump
 15 
 16 # Control File Configuration
 17 control_files=("/home/oracle/oradata/DB9IR2/control01.ctl",
 18                "/home/oracle/oradata/DB9IR2/control02.ctl",
 19                "/home/oracle/oradata/DB9IR2/control03.ctl")


I do:

:s/DB9IR2/DBTEST/g
Is there a nice docu on this? Thanks!
 
Old 10-21-2009, 04:27 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hi mortonmorton,

You'll have to tell vim to replace the pattern in the whole file with a "%":
Code:
:%s/DB9IR2/DBTEST/g
your pattern matches only the current line.

Markus
 
Old 10-21-2009, 05:03 PM   #3
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
Thank you! So that's what hapenning!
 
Old 10-21-2009, 08:52 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Just FYI, you can also give line ranges:

:5,16s/A/B/

A => B for first occurrence on each line only, lines 5-16 inclusive.

:5,16s/A/B/g

as above, but all (g = global; actually on specified lines, not global across all lines) occurrences

:.,16s/A/B/

as example1, but '.' means start from current line (ie one cursor was on)

:.,$s/A/B/

as above, but $ means to end-of-file.

So, an equiv for % is 1,$ eg

:1,$s/A/B/g

A => B for all occurrences on all lines ie whole file.
 
Old 10-22-2009, 08:55 AM   #5
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
Thanks! Found also this great book!

http://hell.org.ua/Docs/oreilly/unix2/vi/index.htm
 
Old 10-22-2009, 09:54 AM   #6
thegeek
Member
 
Registered: Oct 2009
Location: Amsterdam
Distribution: CentOS,Fedora,Puppy
Posts: 62

Rep: Reputation: 20
This is a good page that shows handy solutions for VI:
http://winunixmac.com/unix-how-tos/4...nd-cheat-sheet
 
Old 10-22-2009, 10:09 AM   #7
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 63
vim documentation is good.
 
  


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
search and replace with value of variable say_hi_ravi Programming 10 08-28-2009 03:39 PM
Search and replace TL_CLD Slackware 4 12-25-2007 02:45 PM
search & replace raj_sony2001 Linux - General 4 10-05-2006 03:05 PM
Batch search and replace devilkin Linux - Newbie 2 02-14-2005 03:39 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


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