Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > greping a value from a file

Reply
Thread Tools

greping a value from a file

 
 
paul_0403@yahoo.com
Guest
Posts: n/a
 
      10-22-2008
I have a file with the following contents(see below) and I want to get
the value associated with PROCESS_PID using grep or what ever is the
most effient way. Once I get that value (23491) into a variable
I am going to use it to send a kill command to a process.

Of course I need to test if the grep was successful or not since all
my files may not contain that name value pair.

cat file
PROCESS_START_DATE='20081021'
PROCESS_PID='23491'

Thanks
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-22-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>I have a file with the following contents(see below) and I want to get
>the value associated with PROCESS_PID using grep or what ever is the
>most effient way. Once I get that value (23491) into a variable
>I am going to use it to send a kill command to a process.
>
>Of course I need to test if the grep was successful or not since all
>my files may not contain that name value pair.
>
>cat file
>PROCESS_START_DATE='20081021'
>PROCESS_PID='23491'


No need for grep(). Just loop through the file in a standard
while(<FILE>) loop and try to m//atch the line, grouping the value in $1
in the process.

jue
 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      10-22-2008
(E-Mail Removed) wrote:

> I have a file with the following contents(see below) and I want to get
> the value associated with PROCESS_PID using grep or what ever is the
> most effient way. Once I get that value (23491) into a variable
> I am going to use it to send a kill command to a process.


Open the file normally and just step through it, per line, with a while
loop, and then grab it with my $pid = $1 if
(m/^PROCESS_PID='(\d+)'$/); Or do the look and check, and if positive,
use last to break out of the loop and do the appropriate processing.

> Of course I need to test if the grep was successful or not since all
> my files may not contain that name value pair.


I'm confused by the above. Do you not know what files might contain
PROCESS_PID='number'? Is that why you want to run a grep (perhaps
first) to see? Something like grep -l ^PROCESS_PID /path/to/files/* to
get the file(s) that have it, and then use Perl to open and grab the
actual value (if not grep itself) from those files? I'm not sure if
you're asking how to do this in Perl (instead of grep), how to use grep
inside a Perl script, or if you want to use Perl's built in grep?

--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
Owen
Guest
Posts: n/a
 
      10-22-2008
On Oct 23, 12:44 am, (E-Mail Removed) wrote:
> I have a file with the following contents(see below) and I want to get
> the value associated with PROCESS_PID using grep or what ever is the
> most effient way. Once I get that value (23491) into a variable
> I am going to use it to send a kill command to a process.
>
> Of course I need to test if the grep was successful or not since all
> my files may not contain that name value pair.
>
> cat file
> PROCESS_START_DATE='20081021'
> PROCESS_PID='23491'
>
> Thanks



Hi,

I tried use the program below to start or stop ktorrent, it may be
relevant to your problem. (it was a failure for me because ktorrent
has a gui, but works ok where non gui programs are invoked)




owen

===============================================

#!/usr/bin/perl -w

use strict;

my $program = "ktorrent";
my $status = `/bin/ps cat | /bin/grep $program`;

if ( length($status) > 0 ) {

$status =~ /(^\d+)/;
print "$1\n"; #extract pid from here
exec "kill -9 $1"
}
else { exec "/usr/bin/ktorrent" } # start program

================================================== ======
 
Reply With Quote
 
 
 
Reply

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Do I Locate a value in a text file and evaluate it and then writeout that line based on the value? kwalike57 Perl Misc 7 01-03-2008 06:20 PM
Copy File Field Value to Dynamic File Field Value VUNETdotUS Javascript 25 11-10-2007 10:36 AM
using 'Not' while greping using regex Kimi Perl Misc 4 01-08-2007 01:09 PM
Reading value from File and using in another file vedpsingh@gmail.com Perl Misc 3 08-05-2006 06:08 PM
The value of MCSD: added value to a degree? uv2003 MCSD 4 06-23-2005 05:41 AM



Advertisments