Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting "awk" inside of perl, system `cmd`

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
US
hello,

does perl allow/understand when i use AWK inside of a system command? I can't understand why this isn't working:

$pid=`ps -ef | grep java | egrep -v grep | awk '{print $2}'`;

print "$pid";

when i type: ps -ef | grep java | egrep -v grep | awk '{print $2}'

at the command prompt itself - i get ONLY the process ID.

But in perl - this returns the whole shebang - like the owner, process id, parent process id, etc, etc ...

I JUST want the process id (so i can kill it, in the next command).

~p0lka
 
Didn't you post something very similar to this before? Did you not get the answer you were looking for?

If you're using perl, why bother with grep, egrep and awk? How about something like:
Code:
my @processes = `ps -ef`;
my @java = grep {$_ !~ /grep/} grep {/java/} @processes;
my @pids = map {(split)[1]} for @java;

foreach (@pids) {
    # Code to kill the process
}
Or, if you want to avoid using the temporary arrays, you could do:
Code:
my @pids = map {(split)[1]} grep {$_ !~ /grep/}
           grep {/java/} `ps -ef`;
It's been a while since I've used awk, but if I remember correctly, awk '{print $2}'` prints the second column, which in perl is array index 1. If this isn't correct, you'll need to change the field number associated with the split.
 
Thank you! Yes, I had posted something very similar & was still not able to do what I wanted, but this time I *believe* i've got it. I needed to "split", which I wasn't doing - so thank you for that hint.

system "rm seq2";
$ps=`ps -ef | grep java`;
print "$ps";
@pid=split /\s+/, "$ps";

print "@pid[1] \n";

system "kill @pid[1]";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top