in reply to Retrieve "ps -ef" strings using regex

I would change things such that you stuff the ps output into an array, rather than a single string:
@outlines = `ps -ef | grep $logname`;

Then, I would capture what you want with the regex, not the pre-match. Note that you can avoid excessive escaping in the regex by using alternate delimiters, m{}:

use strict; use warnings; my $logname = 'mylogname'; while (<DATA>) { if (m{(.*/export/home/$logname)/.*/bin/.*}) { print $1, "\n" } } __DATA__ root 28834 28833 0 Aug25 ? 00:00:00 login -- mylogname2 root 29853 29852 0 Aug25 ? 00:00:00 login -- mylogname2 root 5379 5378 0 Aug26 ? 00:00:00 login -- mylogname2 root 5454 5453 0 Aug26 ? 00:00:00 login -- mylogname2 509 6710 1 0 00:20 ? 00:00:28 /export/home/mylogname/folder1/bin/binar +y 509 6729 1 0 00:35 ? 00:00:11 /export/home/mylogname/folder2/bin/binar +y 522 7996 1 0 07:00 ? 00:00:07 /export/home/mylogname2/folder1/bin/bina +ry 509 7997 1 0 07:00 ? 00:00:01 /export/home/mylogname/folder3/bin/binar +y 522 8045 1 0 07:02 ? 00:00:02 /export/home/mylogname2/folder4/bin/bina +ry

prints:

509 6710 1 0 00:20 ? 00:00:28 /export/home/mylogname 509 6729 1 0 00:35 ? 00:00:11 /export/home/mylogname 509 7997 1 0 07:00 ? 00:00:01 /export/home/mylogname

Is this the output you are looking for?

Update: Please re-read How do I compose an effective node title?.

Replies are listed 'Best First'.
Re^2: Retrieve "ps -ef" strings using regex
by Raoul (Initiate) on Aug 27, 2008 at 20:00 UTC
    Thank a lot wise Monks.
    I finally figured it out before seeing your replies:

    my $logname = `logname`; chomp($logname); my @output = `ps -ef | grep $logname`; foreach(@output) { chomp; next if /\~$/ || !$_; my $file = $_; if ($file =~ /^.*\/export\/home\/$logname\/(.*)\/bin\/.*?/) { print "$1\n"; } }


    Displays what I want:
    folder1
    folder2
    folder3

    The soluce was indeed in the array instead of the string.