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


In reply to Re: Retrieve "ps -ef" strings using regex by toolic
in thread Retrieve "ps -ef" strings using regex by Raoul

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.