LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 08-25-2006, 01:07 PM   #1
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
Perl: Using Vars in Substitutions


I have the following code:
Code:
#!/usr/bin/perl5

#Read search for hlds_amd
$search=`ps ux | grep hlds_amd | grep -v grep`;
$user=`echo "\$USER"`;

#Get pid for hlds_amd
if ($search=~ m/(kimball   [0-9]{1,})/)
{
$amd_pid=$1;
print "\n$user\n";
$amd_pid=~s/\$user   //ge;
print "\n$amd_pid\n\n";
#system("kill $amd_pid");
}
I want to substitue the occurence of the username followed by 3 spaces by nothing (remove it). However, it seems the subsitution is not interpolating my variable $user. I have tried adding the e flag onto the end of the expression but it doesn't help.
 
Old 08-25-2006, 01:21 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
I think that the problem is that you are escaping the $. So Perl thinks he has to find $user literally, and thats not what you want. If you just write:
Code:
  s/$user   //ge
I believe its going to work.

Cheers!
 
Old 08-25-2006, 05:30 PM   #3
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Original Poster
Rep: Reputation: 15
Thanks, the problem was the way I was assigning the variable using ``.
 
Old 08-25-2006, 07:00 PM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
Perl also extrapolates variables in ` `, so, I didnt get what was the problem if it wasnt the escaped " (the \"$USER).
 
Old 08-25-2006, 07:41 PM   #5
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Original Poster
Rep: Reputation: 15
It wasn't getting the USER variable correct using the backticks.

Code:
$user=`echo "\$USER"`;
 
Old 08-26-2006, 03:09 AM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
There's no need for an external command like echo to get the username. The entire environment is accessible via the hash %ENV, and $ENV{USER} gives the username.
 
Old 08-26-2006, 12:52 PM   #7
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Original Poster
Rep: Reputation: 15
Oh, thanks

I'll have to do a foreach for the ENV hash and see whatelse I can get.
That's a big help. Thanks.
I was reading a perl book but it doesn't seem to cover that.
 
  


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
Perl with bash environment vars rose_bud4201 Programming 6 06-21-2006 12:19 PM
summary and description substitutions when doing rpmbuild of Kernel dillybat Programming 0 01-14-2005 12:32 PM
sed? -- making substitutions scabies Linux - Software 1 12-21-2003 11:39 PM
Environment vars AMDPwred Linux - General 2 02-28-2003 07:11 AM
Case sensitivity in SED substitutions jkoch Programming 1 02-21-2003 04:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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