Date: Thu, 30 Oct 2008 15:18:57 +0100 (CET) From: Oliver Fromme <olli@lurza.secnetix.de> To: freebsd-stable@FreeBSD.ORG, dudu.meyer@gmail.com Subject: Re: Script-friendly (parseble) ps(1) output? Message-ID: <200810301418.m9UEIvSj086321@lurza.secnetix.de> In-Reply-To: <d3ea75b30810291502h716ae979r9906a7a053ec5f56@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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 > the cgi will send the selected signal. > > I need to add one ps information per column in a table (html), > however, I found ps(1) output to be too hard to parse. There is no > separator. I believed \t was the separator but its not. > > The ps(1) command I need to use is: > > ps -ax -o pid -o user -o emul -o lstart -o lockname -o stat -o command You should use -axww so the command is not truncated. > Since many of those args use [:space:] in the output, I can not use > [:space:] as a separator. Yup, that's a problem. That's why a simple awk line won't do it. You should use fixed character positions instead of field separaters for splitting fields. I think you can look at the headers (first line) to determine the field widths. All fields are left-aligned except for the PID. So your script should simply look at the first line and store the starting position of every word in an array. Using those values you can split all the data lines according to the character positions. I could provide a 3-liner in Python, but I assume you're not writing that script in Python. :-) Best regards Oliver -- Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M. Handelsregister: Registergericht Muenchen, HRA 74606, Geschäftsfuehrung: secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün- chen, HRB 125758, Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart FreeBSD-Dienstleistungen, -Produkte und mehr: http://www.secnetix.de/bsd C++: "an octopus made by nailing extra legs onto a dog" -- Steve Taylor, 1998
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200810301418.m9UEIvSj086321>