From owner-freebsd-stable@FreeBSD.ORG Thu Oct 30 21:40:43 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 3CB021065673 for ; Thu, 30 Oct 2008 21:40:43 +0000 (UTC) (envelope-from dudu.meyer@gmail.com) Received: from yx-out-2324.google.com (yx-out-2324.google.com [74.125.44.29]) by mx1.freebsd.org (Postfix) with ESMTP id E4D4C8FC0C for ; Thu, 30 Oct 2008 21:40:42 +0000 (UTC) (envelope-from dudu.meyer@gmail.com) Received: by yx-out-2324.google.com with SMTP id 8so358418yxb.13 for ; Thu, 30 Oct 2008 14:40:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=aujhKY4CnbBSM7aeWGHLp2G2gzOGwVu82uHdkf+Xq/g=; b=VnZP1AGeIOZQZtRUGG7KacoYAotJ0S1F0zbRjZDPOdNT96nuimqqwniYIV3MeTwLhL +cyRvTXxgotJEL+/tfcQ/fNbzNPeYGCFFjXRsVycti+XWTssll34RCHoaWpRXqFzY0ua mu8xd9GJ4NlZJpV7wCd95gvzFFKMbYgNjd+aA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=Hwmhj6RQacyzLEIe0N21Fye4zDUiHFRzoH77qM8JY+BwjONzD2uREbYuEX6O7NozPu 34sv1mf/yTtumrG6yUkOZ7f+3xK+2v+4jSWfktvaIk6ZA69twB3D0/b8ggAEMsYym24a QOEokiHuKvuDcN70rqbo67cgsiWRZeJJTduyQ= Received: by 10.65.215.14 with SMTP id s14mr3326245qbq.47.1225402841066; Thu, 30 Oct 2008 14:40:41 -0700 (PDT) Received: by 10.64.201.9 with HTTP; Thu, 30 Oct 2008 14:40:40 -0700 (PDT) Message-ID: Date: Thu, 30 Oct 2008 19:40:40 -0200 From: "Eduardo Meyer" To: freebsd-stable@freebsd.org, dudu.meyer@gmail.com In-Reply-To: <200810302114.m9ULE8jV002689@lurza.secnetix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <200810302114.m9ULE8jV002689@lurza.secnetix.de> Cc: Subject: Re: Script-friendly (parseble) ps(1) output? X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list 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 21:40:43 -0000 On Thu, Oct 30, 2008 at 7:14 PM, Oliver Fromme 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 "" > for (i = 1; i <= numfields; i++) { > content = substr($0, fstart[i], fwidth[i]) > print " " content "" > } > print "" > } > }' > > Of course that's just an example. You still have to > produce "" 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