From owner-freebsd-questions@freebsd.org Mon Oct 5 05:14:58 2015 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 15583A10036 for ; Mon, 5 Oct 2015 05:14:58 +0000 (UTC) (envelope-from hedwards816@gmail.com) Received: from mail-pa0-x236.google.com (mail-pa0-x236.google.com [IPv6:2607:f8b0:400e:c03::236]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id D97D6103F for ; Mon, 5 Oct 2015 05:14:57 +0000 (UTC) (envelope-from hedwards816@gmail.com) Received: by pablk4 with SMTP id lk4so164280637pab.3 for ; Sun, 04 Oct 2015 22:14:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=EPxQxiyqB/dQaCuyAXbCHBLGJZUUp7y+qi3IKCbQVxs=; b=hsBunXbvd483v2sPlIIt1CBY79n1WeuVT8m38KLa9kLV2xv8R+o4u1XWHehAflFbdT Zv6MvrGkqHWyDq3oOGvRfV8i9qXPnBd3bQwTy9+ohW/rxGG33P7YbowlWNYhuOPPswCr jkGPQRQRLgRCeMhzJkITkS3xzQ36IpaaAoFB0lqGnztvyJVmQCeXkbB890gR6Juab93i zmA5xwwFYJMsRDJURasR4MvjljTQTECnRsTw5wqxqVsC7Xm+m5rIN/2IwsCzVruM3KuZ ccgeRJZX14LZ4ZTy9s9pv3m+xyCjRiDeJy2BapT4BPU7+TYtQfdW1caAopvdkBFIBsxf sejg== X-Received: by 10.66.253.199 with SMTP id ac7mr36999028pad.56.1444022061982; Sun, 04 Oct 2015 22:14:21 -0700 (PDT) Received: from [192.168.0.8] (174-24-130-162.tukw.qwest.net. [174.24.130.162]) by smtp.googlemail.com with ESMTPSA id sl7sm24882318pbc.54.2015.10.04.22.14.20 for (version=TLSv1/SSLv3 cipher=OTHER); Sun, 04 Oct 2015 22:14:21 -0700 (PDT) Subject: Re: awk question To: freebsd-questions@freebsd.org References: <5611C922.4050007@hiwaay.net> <5611EEE2.9030100@sneakertech.com> <20151005060351.3646d1b7.freebsd@edvax.de> From: HM Edwards Message-ID: <5612071A.4090001@gmail.com> Date: Sun, 4 Oct 2015 22:14:02 -0700 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <20151005060351.3646d1b7.freebsd@edvax.de> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 05 Oct 2015 05:14:58 -0000 On 10/04/15 21:03, Polytropon wrote: > On Sun, 04 Oct 2015 23:30:42 -0400, Quartz wrote: >> Considering you're referring to 'pulling out lines', maybe you want to >> trap the output of smartctl into a variable, then just echo that to grep >> a bunch of times, before awk? >> >> ie; >> >> x=$(smartctl) >> >> echo "$x" | grep 'foo' | awk '{print $1}' >> echo "$x" | grep 'bar' | awk '{print $2}' >> echo "$x" | grep 'baz' | awk '{print $3}' >> >> ...etc? > Note that awk has "builtin grep", so your example could be > combined to one smartctl call: > > smartctl | awk ' > /foo/ { print $1; } > /bar/ { print $2; } > /baz/ { print $3; } > ' > > Of course storing the smartctl output to a variable is very > useful when processing it _multiple_ times. But as you said, > awk is quite versatile. :-) > > > Hello, If it were me, I'd probably just grep for the bits of output I'm looking for, pipe it to tr to remove the new lines, then have awk parse out the single line into the output I'm looking for. As in something like smartctl -l scttemp /dev/ada0 | grep '(foo|bar|foo2|bar2)' | tr -d "\n" | awk '{print "label 1 " $1 "label 2 " $2}' It's another possibility there, although, it does make somewhat less use of awk. You could probably also remove the grep completely and just use awk to spit out the lines you're interested in. I'm just a habitual grep abuser.