CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
replacement problem - please advise

 



t_shtilman
New User

Mar 30, 2009, 6:32 AM

Post #1 of 2 (1880 views)
replacement problem - please advise Can't Post

Hi Guys
I am new to perl , trying to fix a script line

perl -pi -e "s/^(MachineName=).*/\$1`hostname`/;" /env_file

this should replace the MachineName= with MachineName=<the real host name> in the env_file

if hostname start with numbers , like '97979-myhost.com' the number and MachineName is being removed also from file

when hostname return something which has no numeric chars like 'myhost1.com' replacement work smoothly

Any help will be much appreciated
Thanks
TS


1arryb
User

Mar 30, 2009, 5:31 PM

Post #2 of 2 (1869 views)
Re: [t_shtilman] replacement problem - please advise [In reply to] Can't Post

Hi t,

I had to laugh when I saw your code. You're a bash programmer, right? So was I when I first started and this is a classic bashy mistake: Unlike bash, perl won't do command interpolation 'in-line'. You'd need two commands, like:

Code
... 
my $host = `hostname`;
chomp $host;
s/^(MachineName=)/$1$host/;
...

which would be tough to do with perl -pi -e. Easier is to define $HOST in the shell environment (it's usually already defined):

Code
perl -pi -e "s/^(MachineName=)/$1$ENV{HOST}/;"

Also, you should avoid using regex memory if you don't need it; it's slow:

Code
perl -pi -e "s/^MachineName=/MachineName=$ENV{HOST}/;"


Cheers,

Larry


(This post was edited by 1arryb on Mar 30, 2009, 5:32 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives