Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Parsing 'ps' output

by vineet2004 (Initiate)
on Dec 28, 2006 at 11:13 UTC ( [id://592013]=perlquestion: print w/replies, xml ) Need Help??

vineet2004 has asked for the wisdom of the Perl Monks concerning the following question:

11299 pts/4 00:00:00 su 11303 pts/4 00:00:00 bash 11403 tty2 00:00:00 bsc_static 11406 tty2 00:00:00 rtpTrau_static 11408 tty2 00:11:30 hullBts_static 11418 pts/4 00:00:00 tail 12157 pts/5 00:00:00 su
the above lines are part of a file. the first 4 digits (or more) in each line make up the PID number(in this case there are 5 digits)...eg. 11403 the last column of each line has the file name. eg. su i need to find out PID number for the file name "hullBts_static"(in the above case it is 11408.......please mind that "hullBts_static" will not always be in the same line as shown... as the file entries change each time i run it).... then i need to generate a file name using the above found PID. this file name is of the form "frag_ PID.log"... in other words , the new filename will consist the matched PID. this file name will now have to be piped into a shell script file. my code looks like this....is it correct??
#!usr/bin/perl while(<>) { if(/^([\d]+)*(hullBts_static)/) { my $var1= "frag_$1.log"; } print STDOUT $var1; }

Replies are listed 'Best First'.
Re: Parsing 'ps' output
by polettix (Vicar) on Dec 28, 2006 at 11:35 UTC

    Update: luckily this whole thread was renamed to "Parsing 'ps' output" from "hull bts"

    Hi vineet2004,

    given the fast pace you're posting here, it seems that you find this site quite useful, and this is good. I hope you'll understand that much of its usefulness is not only in the general quickness in the answer one can get, or in their quality; one big aspect of this site is that all the questions, answers, thoughts, etc. are kept for future memory. In this way, it is useful today (to whoever asks a question) and it will be useful tomorrow for those who look for something similar (e.g. using Super Search).

    One of the key aspects when searching inside the node base is the fact that one can perform a query based on the title of the post. Let's say that I'm looking for something about "regexp matching" - that's exactly what I would type, as a start, inside Super Search within the title entry. Making queries by title is usually faster (titles are short) and this makes title an invaluable resource for nodes categorisation.

    Which brings us to your node. Unfortunately, whatever usefulness your post (and the answers I hope you'll receive) could have, they will be missed by those looking for it in the quick way I was talking about. While "hull bts" makes perfectly sense to you, it means probably near to nothing to most people here, which is bad for two reasons: the first is that the community will miss something and the second, more important from your point of view, is that you will get less answers (because you're not actually declaring what you're looking for).

    To cope with this, I'd suggest you to read How do I compose an effective node title? and try to apply the suggestions in this node's title and in the future nodes you'll produce for the community. By the way, you could also enjoy How do I post a question effectively?, which will surely give you hints on how to receive (more) lots of answers to your questions.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Parsing 'ps' output
by monarch (Priest) on Dec 28, 2006 at 11:26 UTC
    First, let's create a test bed so we can tell if we got it right!

    use strict; my $correctanswer = "11408"; my $regexp = qr/^([\d]+)*(hullBts_static)/; while (<DATA>) { if ( /$regexp/ ) { if ( $1 eq $correctanswer ) { print( "Congratulations, the regexp is correct!" ); exit( 1 ); } else { die( "Error, match was $1" ) } } } die( "Error, never matched" ); __DATA__ 11299 pts/4 00:00:00 su 11303 pts/4 00:00:00 bash 11403 tty2 00:00:00 bsc_static 11406 tty2 00:00:00 rtpTrau_static 11408 tty2 00:11:30 hullBts_static 11418 pts/4 00:00:00 tail 12157 pts/5 00:00:00 su

    Well if I run this test code I get:

    Error, never matched at /tmp/hullbts.pl line 17, <DATA> line 7.

    Clearly something is wrong. I suggest taking a closer look at the regexp and what you're trying to match.

    First you want the beginning of a line. Then as many digits in a row as possible. Then anything until we find "hullBts_static". With this in mind let's have another look at a regexp:

    my $regexp = qr/ ^ # beginning of line (\d+) # as many digits as we can find .* # anything at all hullBts_static # literally, "hullBts_static" /x;

    When substituting this new regexp we get the following output from the test script:

    Congratulations, the regexp is correct!

    Note that the /x at the end of the regular expression merely allows us to spread the expression over several lines. That regular expression looks, more compactly, like /^(\d+).*hullBts_static/.

Re: Parsing 'ps' output
by themage (Friar) on Dec 28, 2006 at 11:26 UTC
    Hi vineet2004,

    You would not be here asking us it it was correct, would you? I think your only mistake is in this line:
    if(/^([\d]+)*(hullBts_static)/)
    You are missing the dot (.) before the asterisk. It should be:
    if(/^([\d]+).*(hullBts_static)/)
    I don't see any other problem, and it should do what you want... I guess.

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://592013]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found