From owner-freebsd-stable@FreeBSD.ORG Thu Oct 30 14:19:00 2008 Return-Path: Delivered-To: freebsd-stable@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 901081065670 for ; Thu, 30 Oct 2008 14:19:00 +0000 (UTC) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (unknown [IPv6:2a01:170:102f::2]) by mx1.freebsd.org (Postfix) with ESMTP id 07F648FC1F for ; Thu, 30 Oct 2008 14:18:59 +0000 (UTC) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (localhost [127.0.0.1]) by lurza.secnetix.de (8.14.3/8.14.3) with ESMTP id m9UEIwl2086322; Thu, 30 Oct 2008 15:18:58 +0100 (CET) (envelope-from oliver.fromme@secnetix.de) Received: (from olli@localhost) by lurza.secnetix.de (8.14.3/8.14.3/Submit) id m9UEIvSj086321; Thu, 30 Oct 2008 15:18:57 +0100 (CET) (envelope-from olli) Date: Thu, 30 Oct 2008 15:18:57 +0100 (CET) Message-Id: <200810301418.m9UEIvSj086321@lurza.secnetix.de> From: Oliver Fromme To: freebsd-stable@FreeBSD.ORG, dudu.meyer@gmail.com In-Reply-To: X-Newsgroups: list.freebsd-stable User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (FreeBSD/6.4-PRERELEASE-20080904 (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.1.2 (lurza.secnetix.de [127.0.0.1]); Thu, 30 Oct 2008 15:18:58 +0100 (CET) Cc: Subject: Re: Script-friendly (parseble) ps(1) output? X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: freebsd-stable@FreeBSD.ORG, dudu.meyer@gmail.com List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Oct 2008 14:19:00 -0000 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