LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 11-06-2006, 08:01 PM   #1
FirmbIT
LQ Newbie
 
Registered: Oct 2005
Posts: 27

Rep: Reputation: 15
Alternative to perl search and replace


I'm writing a script and am trying to do a Perl search and replace in a file by replacing something in the file with a variable but it is turning the variable blank and replacing it with nothing. Here is the code:

perl -pi -e 's/hostname/$hostname/g' /etc/nagios/configs/$hostname/host.cfg

Is there another way to do this? Maybe sed?

Thank you for the help in advance.
 
Old 11-06-2006, 08:50 PM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 60
I believe all you need to do is quote your variable such as:

Code:
perl -pi -e 's/hostname/'$hostname'/g' /etc/nagios/configs/$hostname/host.cfg
or using sed:

Code:
sed 's/hostname/'${hostname}'/g' /etc/nagios/configs/$hostname/host.cfg

Hope this helps

Last edited by fordeck; 11-07-2006 at 05:48 PM.
 
Old 11-06-2006, 08:53 PM   #3
FirmbIT
LQ Newbie
 
Registered: Oct 2005
Posts: 27

Original Poster
Rep: Reputation: 15
Perfect! Thanks.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
global search and replace? jkcunningham Linux - General 5 03-15-2006 02:43 PM
Perl: Search and replace directories within text files Erhnam Programming 2 03-07-2006 04:07 AM
Perl Regexp search-n-replace jpbarto Programming 2 06-16-2005 12:45 PM
How to "Search & Replace" in html files using Perl? rebel Red Hat 8 04-09-2005 12:58 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM


All times are GMT -5. The time now is 01:11 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration