From owner-freebsd-questions@freebsd.org Mon Oct 5 02:30:10 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 9ED28A10DE1 for ; Mon, 5 Oct 2015 02:30:10 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de (mx01.qsc.de [213.148.129.14]) (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 6471211C9 for ; Mon, 5 Oct 2015 02:30:10 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de (port-92-195-13-98.dynamic.qsc.de [92.195.13.98]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx01.qsc.de (Postfix) with ESMTPS id F2EF53CC44; Mon, 5 Oct 2015 04:21:29 +0200 (CEST) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id t952LTpH007044; Mon, 5 Oct 2015 04:21:29 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Mon, 5 Oct 2015 04:21:29 +0200 From: Polytropon To: "William A. Mahaffey III" Cc: FreeBSD Questions !!!! Subject: Re: awk question Message-Id: <20151005042129.1f153ec6.freebsd@edvax.de> In-Reply-To: <5611C922.4050007@hiwaay.net> References: <5611C922.4050007@hiwaay.net> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII 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 02:30:10 -0000 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 | 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. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...