greping a value from a file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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


Re: greping a value from a file wrote:
Quoted text here. Click to load it

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.


Re: greping a value from a file wrote:

Quoted text here. Click to load it

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.
Quoted text here. Click to load it

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,, 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!

Re: greping a value from a file

On Oct 23, 12:44 am, wrote:
Quoted text here. Click to load it


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)



 #!/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


Site Timeline