greping a value from a file

P

paul_0403

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
 
J

Jürgen Exner

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
 
T

Tim Greer

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?
 
O

Owen

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top