Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 30 Oct 2008 19:40:40 -0200
From:      "Eduardo Meyer" <dudu.meyer@gmail.com>
To:        freebsd-stable@freebsd.org, dudu.meyer@gmail.com
Subject:   Re: Script-friendly (parseble) ps(1) output?
Message-ID:  <d3ea75b30810301440n2a124c26ye1d61997f9ef7c05@mail.gmail.com>
In-Reply-To: <200810302114.m9ULE8jV002689@lurza.secnetix.de>
References:  <d3ea75b30810301303y8657f65vd6165f0650ea2bb5@mail.gmail.com> <200810302114.m9ULE8jV002689@lurza.secnetix.de>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Oct 30, 2008 at 7:14 PM, Oliver Fromme <olli@lurza.secnetix.de> wrote:
> Eduardo Meyer wrote:
>  > Oliver Fromme wrote:
>  > > Eduardo Meyer wrote:
>  > > > I need to write a cgi script which will print the output from ps(1) in
>  > > > a table (html), so the average-operator can click on a KILL link and
>  >
>  > Thank you all. I didnt mention the language, yes, I wanted it with
>  > shell script. Sadly, no idea was completly enough, for the default ps
>  > output it simple has no pattern. No multiple-pattern would do the job
>  > safely.
>
> Did you actually read my suggestion?  I explained that you
> cannot cut by pattern, but that you need to cut on field
> widths according to the header line.

Yes, sure I did. This is why I replied agreeing with you. However, it
was not enough, I had to format the header so I was sure the pattern
would not fail.

>
> Here's a solution that implements that, using awk to do
> the parsing:
>
> #!/bin/sh -
>
> ps -axww -o pid -o user -o emul -o lstart -o lockname -o stat -o command |
> awk '{
>    if (NR == 1) {
>        #   Parse header line.
>        #   Build arrays fstart[] and fwidth[].
>        numfields = split(" " $0, field, / [^ ]/) - 1
>        fwidth[1] = length(field[1] field[2]) + 2
>        fstart[1] = 1
>        for (i = 2; i <= numfields; i++) {
>            fwidth[i] = length(field[i + 1]) + 2
>            fstart[i] = fstart[i - 1] + fwidth[i - 1]
>        }
>        fwidth[numfields] = 100
>    }
>    else {
>        #   Parse data line.
>        print "<tr>"
>        for (i = 1; i <= numfields; i++) {
>            content = substr($0, fstart[i], fwidth[i])
>            print "  <td>" content "</td>"
>        }
>        print "</tr>"
>    }
> }'
>
> Of course that's just an example.  You still have to
> produce "<table>" and other surrounding HTML, of course.
>
> Best regards
>   Oliver

Thank you :) I will use it as a template.


-- 
===========
Eduardo Meyer
pessoal: dudu.meyer@gmail.com
profissional: ddm.farmaciap@saude.gov.br



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?d3ea75b30810301440n2a124c26ye1d61997f9ef7c05>