LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-04-2015, 10:50 AM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Rep: Reputation: 39
Replacing multiple instances of text within a file using vi editor


Hello --

I need to replace multiple instances in a give file of the following text:

Quote:
/usr/lib/nagios/plugins
with the following text:

Quote:
/usr/local/nagios/libexec
Can this be done in vi editor, and if so, what is the correct command sequence?

If vi editor is not the preferred method, what would be a better way?

Thanks.
 
Old 11-04-2015, 11:17 AM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Probably something like

Code:
:s/\/usr\/lib\/nagios\/plugins/\/usr\/local\/nagios\/libexec/g
Without escapes for /

Code:
:%s//usr/lib/nagios/plugins//usr/local/nagios/libexec/g
 
Old 11-04-2015, 11:38 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
In general, string replacement in vi is:
Code:
:%s/oldstring/newstring/g
Since your strings include forward slashes, you'll need to delimit them so they don't interfere with the string replacement syntax.

Last edited by suicidaleggroll; 11-04-2015 at 11:40 AM.
 
Old 11-04-2015, 12:01 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
My preferred method would be just to use sed, and the command is still the same. Never thought of it, but sed and vi may be common code in that respect.
 
Old 11-04-2015, 12:47 PM   #5
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hello --

I tried both expressions, vi and sed, but without success.

When I try the vi command syntax:

Quote:
:%s//usr/lib/nagios/plugins/usr/local/nagios/libexec/g
I get the error message:

Quote:
E488: Trailing characters
When I enter the follow sed command syntax:

Quote:
sed 's/usr/libexec/nagios/plugins/usr/local/nagios/libexec/' checkcommands.cfg
The error message is as follows:

Quote:
sed: -e expression #1, char 15: unknown option to `s'
Where are my syntaces mistaken?
 
Old 11-04-2015, 12:52 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You have to delimit similar to how Sefyir shows you.

You can experiment with using double quotes. I'm unsure if they work, I know in naming files and referring to them in commands like cp and mv, using double quotes around filenames which have spaces works. But also delimiting the spaces with a backslash before them works too. Similarly, everywhere you have a forward slash which is just supposed to be a forward slash you need to delimit with a backslash before it.

Any chance you can just use gedit or emacs? For me emacs, providing you have the display capabilities to support an emacs buffer window just works fine for global search and replace. You don't need regular expression formats because the editor recognizes you are within search and replace mode. I'm assuming gedit is similar to that.
 
Old 11-04-2015, 04:50 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just use an alternate delimiter (same as sed can)
Code:
:s:/usr/lib/nagios/plugins:/usr/local/nagios/libexec:g
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing text on multiple lines neon99 Linux - Newbie 5 03-07-2014 09:54 AM
Deleting all instances of a certain character from a text file SentralOrigin Linux - Software 6 02-18-2010 01:49 PM
Replacing text in multiple files ? centosfan Linux - General 10 09-20-2008 07:25 AM
Combine multiple one column text file into one text file with multiple colum khairilthegreat Linux - Newbie 7 11-23-2007 01:31 PM
Replacing All Instances of a pathname with another in Vi Editor kaplan71 Linux - General 1 06-30-2005 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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