Ok, this is a bit of an odd one but it's been bugging me for days. I'm using perl as part of a filter for listing all running applications. The point is to be able to use the list returned as a completion for a command in tcsh... Ok. So this is really fairly obscure.

I'm running OSX, so some explanation of how applications show up in ps listings may be required. Using ps -axwwo command= I'll get a list of processes including those without controlling terminals, and doesn't width-limit the result and only prints the command path + arguments.

As well as the applications which are running, there's also going to be a fair few other processes. I don't want those in the list.

The rules for how an application looks in the ps listing:

1. All applications have XXX.app/Contents/ in their path, the actual executable being at something like somepath/XXX.app/Contents/MacOS/XXX Or perhaps somepath/XXX.app/Contents/MacOSClassic/XXX. Anyhow, all applications have XXX.app/Contents/ in their path.

2. Applications may infact be nested (helper apps etc). So may appear as somepath/XXX.app/Contents/Resources/Helper.app/Contents/MacOS/Helper which kinda confuses things somewhat, as the running app is actually Helper, not XXX.

3. In the ps listing you have often a postfixed "-psn=101010" indicating the psn of the application.

4. Incidentally application names may contain spaces ;(

I want a listing consisting of the running applications, only. In the form

A.app B.app C.app

etc. even when C.app may be inside D.app's bundle folder.

Yeah, this is very esoteric.

So currently, I have the following line which I execute in tcsh.

/bin/ps -axwwo command= | grep '\.ap\{2\}/Contents' | perl -p -e 's|^/(([^/]+)+/)+(.+\.app)/.*$|$3|'

Can anyone do better? I'm looking for a one-liner. I'd love to fold the grep into the perl, but it complicates processes finding themselves, and I couldn't see how to do it as a perl filter...

Note that the use of the grep and it's bizarre construction means it doesn't match itself or the perl filter.


In reply to The simplest (perl) filter for the job by geohar

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.