greping a value from a file

Discussion in 'Perl Misc' started by paul_0403@yahoo.com, Oct 22, 2008.

  1. Guest

    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
     
    , Oct 22, 2008
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Oct 22, 2008
    #2
    1. Advertising

  3. Tim Greer Guest

    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!
     
    Tim Greer, Oct 22, 2008
    #3
  4. Owen Guest

    On Oct 23, 12:44 am, 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

    ========================================================
     
    Owen, Oct 22, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. San Diego Guy
    Replies:
    0
    Views:
    553
    San Diego Guy
    Aug 7, 2003
  2. =?Utf-8?B?QW5hY2hvc3RpYw==?=

    RE: Textbox postback putting value,value

    =?Utf-8?B?QW5hY2hvc3RpYw==?=, Jun 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    636
    =?Utf-8?B?S2VpdGggRG9yYW4=?=
    Jun 21, 2004
  3. Replies:
    3
    Views:
    96
    David Squire
    Aug 5, 2006
  4. Kimi
    Replies:
    4
    Views:
    81
    Jürgen Exner
    Jan 8, 2007
  5. VUNETdotUS
    Replies:
    25
    Views:
    461
    Thomas 'PointedEars' Lahn
    Nov 10, 2007
Loading...

Share This Page