From owner-freebsd-questions@freebsd.org Mon Oct 5 14:59:05 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 2CADC9B135A for ; Mon, 5 Oct 2015 14:59:05 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx02.qsc.de (mx02.qsc.de [213.148.130.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 E678F876 for ; Mon, 5 Oct 2015 14:59:04 +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 mx02.qsc.de (Postfix) with ESMTPS id 80CD327827; Mon, 5 Oct 2015 16:59:02 +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 t95Ex2ve008671; Mon, 5 Oct 2015 16:59:02 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Mon, 5 Oct 2015 16:59:02 +0200 From: Polytropon To: Quartz Cc: freebsd-questions@freebsd.org Subject: Re: awk question Message-Id: <20151005165902.ad01c288.freebsd@edvax.de> In-Reply-To: <56124479.9020505@sneakertech.com> References: <5611C922.4050007@hiwaay.net> <20151005042129.1f153ec6.freebsd@edvax.de> <5611F776.9090701@hiwaay.net> <56124479.9020505@sneakertech.com> 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 14:59:05 -0000 On Mon, 05 Oct 2015 05:35:53 -0400, Quartz wrote: > > 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. > > Awk, sed, and various other utilities all have basic regex line > matching, so if you're already calling them for some other feature then > piping the output through grep first is kinda silly and inefficient. > It's sorta like doing $(cat foo.txt | grep 'bar') instead of just $(grep > 'bar' foo.txt). I suggested it mainly because it's easy to understand > conceptually and because awk's syntax has something of a steep learning > curve. The example "cat foo.txt | grep 'bar'" is a case of "useless use of cat". However, it makes the processing order more clear. On the other hand, the first output program might be a "real program" (not just "cat" printing a file that alredy exists). The form "input | step1 | step2 | step3 | step4 > result" usually is more readable than what's possible with <, << EOF, <(), `cmd` or $(cmd). But it depends on the actual problem you want to solve which tool from the toolbox you will use. Additionally, awk isn't that hard to learn. Reading "man awk" will provide you with a good background. And if you're already a C programmer, you'll see that many things you can do in C will also work similarly in awk, which _might_ not even be a good thing. :-) Simple tasks where inefficiency doesn't matter - go with grep, tr, cut, sort, maybe sed. More complex things (like document generators from database input) - consider awk. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...