From owner-freebsd-questions@freebsd.org Mon Oct 5 04:07:21 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 64BC1A10002 for ; Mon, 5 Oct 2015 04:07:21 +0000 (UTC) (envelope-from wam@hiwaay.net) Received: from fly.hiwaay.net (fly.hiwaay.net [216.180.54.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3570F1DE3 for ; Mon, 5 Oct 2015 04:07:20 +0000 (UTC) (envelope-from wam@hiwaay.net) Received: from kabini1.local (dynamic-216-186-213-32.knology.net [216.186.213.32] (may be forged)) (authenticated bits=0) by fly.hiwaay.net (8.13.8/8.13.8/fly) with ESMTP id t9547IA9008126 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO) for ; Sun, 4 Oct 2015 23:07:19 -0500 Subject: Re: awk question References: <5611C922.4050007@hiwaay.net> <20151005042129.1f153ec6.freebsd@edvax.de> Cc: FreeBSD Questions !!!! From: "William A. Mahaffey III" Message-ID: <5611F776.9090701@hiwaay.net> Date: Sun, 4 Oct 2015 23:12:48 -0453.75 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <20151005042129.1f153ec6.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 04:07:21 -0000 On 10/04/15 21:27, Polytropon wrote: > On Sun, 4 Oct 2015 19:55:08 -0453.75, William A. Mahaffey III wrote: >> I am using awk & smartctl in a small shell script to print out HDD temps >> in a purty format, 1 line per drive. As it happens, the output I want is >> spread out over 4 lines of smartctl out, requiring (I *think*) 4 calls >> to smartctl each piped to its own awk invocation to pull out the line I >> want & print its info out. Is there some way to get awk to consider more >> than 1 line at a time ? In my case my 4 lines are indeed sequential, & >> it would be a bit more efficient if I could process all 4 lines once I >> found the 1st one. This is definitely *not* critical, what I have now >> works AOK, I was/am just curious if it could be optimized a bit. TIA & >> have a good one. > I'm not sure I understand your question correctly, as you're not > providing some example input data and what output you want. But > awk can process one line against multiple patterns, and if, let's > say, 4 patterns match, 4 lines will be output: > > smartctl | awk ' > /pattern1/ > /pattern2/ > /pattern3/ > /pattern4/ > ' > out.txt > > If no action is provided, the whole line will be printed; if you > just want some (maybe postprocessed) fields of a line, add > > { print > to each pattern. Another way is "counting down" the amount of > additional lines after one pattern has been found: > > smartctl | awk ' > { > if (nextlines > 0) { > print; > nextlines--; > } > } > /pattern/ { > nextlines = 4; > } > ' > out.txt > > The first block without a pattern will always be executed > (in this case, "print" is the command that will be called > for all desired lines), and the one with a pattern that > will "trigger" that first block to actually output something. > > > > By the way: If there is no processing, and you just need some > data lines as is, why not use grep? > > smartctl | grep "" -A 4 > out.txt > > See "man grep" for details on the -A option. Good point, here is the relevant part of the smartctl output: [root@kabini1, /etc, 7:10:34pm] 915 % smartctl -l scttemp /dev/ada0 smartctl 6.4 2015-06-04 r4109 [FreeBSD 9.3-RELEASE-p24 amd64] (local build) Copyright (C) 2002-15, Bruce Allen, Christian Franke, www.smartmontools.org === START OF READ SMART DATA SECTION === SCT Status Version: 3 SCT Version (vendor specific): 256 (0x0100) SCT Support Level: 1 Device State: Active (0) Current Temperature: 27 Celsius Power Cycle Min/Max Temperature: 23/31 Celsius Lifetime Min/Max Temperature: 19/33 Celsius Lifetime Average Temperature: 23 Celsius Under/Over Temperature Limit Count: 0/0 I am invoking smartctl 4 times per drive to extract & process (print in my own format) the 4 lines beginning w/ 'Current Temperature:'. I want the resulting output in 1 line per drive as such (for 4 drives): [root@kabini1, /etc, 7:32:53pm] 456 % hddtemp /dev/ada[0-3] SMART supported, SMART enabled drive /dev/ada0: HGST HTS721010A9E630, S/N: JR10006PGYH08E, Temp. 27 degC, min/max, cycle: 23/31, lifetime: 19/33, lifetime avg. 23 degC drive /dev/ada1: HGST HTS721010A9E630, S/N: JR10006PGYD6ZE, Temp. 28 degC, min/max, cycle: 24/33, lifetime: 20/33, lifetime avg. 24 degC drive /dev/ada2: HGST HTS721010A9E630, S/N: JR10006PGYTR8E, Temp. 27 degC, min/max, cycle: 24/33, lifetime: 19/33, lifetime avg. 24 degC drive /dev/ada3: HGST HTS721010A9E630, S/N: JR10006PGYK1YE, Temp. 27 degC, min/max, cycle: 23/31, lifetime: 19/33, lifetime avg. 23 degC [root@kabini1, /etc, 7:32:58pm] 457 % Funny you mention grep, I had a similar conversation on the NetBSD list last week & everyone there suggested using awk alone to 'grep' out the lines I wanted. However, over there, the temp output I wanted was all in 1 line, just not formatted as I wanted it. I think your 1st suggested method above will work AOK (if I understand it correctly, I will try it directly. Thanks :-) .... -- William A. Mahaffey III ---------------------------------------------------------------------- "The M1 Garand is without doubt the finest implement of war ever devised by man." -- Gen. George S. Patton Jr.