From owner-freebsd-questions@freebsd.org Mon Oct 5 21:58:17 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 3988B9B66C7 for ; Mon, 5 Oct 2015 21:58:17 +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 C9852AEC for ; Mon, 5 Oct 2015 21:58:16 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de (port-92-195-41-64.dynamic.qsc.de [92.195.41.64]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx02.qsc.de (Postfix) with ESMTPS id 81C282782C; Mon, 5 Oct 2015 23:58:12 +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 t95LwCuM010721; Mon, 5 Oct 2015 23:58:12 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Mon, 5 Oct 2015 23:58:12 +0200 From: Polytropon To: Quartz Cc: freebsd-questions@freebsd.org Subject: Re: awk question Message-Id: <20151005235812.eee38247.freebsd@edvax.de> In-Reply-To: <5612EF57.10207@sneakertech.com> References: <5611C922.4050007@hiwaay.net> <20151005042129.1f153ec6.freebsd@edvax.de> <5611F776.9090701@hiwaay.net> <56124479.9020505@sneakertech.com> <20151005165902.ad01c288.freebsd@edvax.de> <5612EF57.10207@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 21:58:17 -0000 On Mon, 05 Oct 2015 17:44:55 -0400, Quartz wrote: > > > The form "input | step1 | step2 | step3 | step4> result" usually > > is more readable > > That's what I meant my being easier to understand conceptually. I agree > about being more readable- even though this format sometimes needs the > 'useless cat' it's often my preferred coding style, especially in > scripts where the input might change around. And the "useless cat" method also makes it easy to test the script with varying input (for example, pre-generated test input) before it "goes live". It also makes it easier to "extend" the pre- or post-processing commands with new ones. > > 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. :-) > > The problem with awk is the whole BEGIN/END/braces thing and how commas > interact with the operands. It's not that hard: BEGIN { ... } will be executed _before_ any input is processed, END { ... } will be executed _after_ all input has been processed. /pattern/ { ... } will be executed for each matching input line, (condition) { ... } will be executed when the condition is true, and { ... } will be executed for _every_ input line. Regarding commas: You can use the "print a b c" form as well as the more sophisticated C-like printf("format string", a, b, c) form. For all other functions, commas are argument separators just like in many other programming languages. % echo "a b c" | awk '{ print $3 $1 $2 }' cab % echo "a b c" | awk '{ print $3, $1, $2 }' c a b % echo "a b c" | awk '{ printf("%s-%s-%s\n", $3, $1, $2); }' c-a-b Those are the three "main methods" of printing: concatenated, separated by a space, and custom formatted string. And the semicolon is optional, it's just my C-contamination. :-) > It's not very much like sh or C syntax (or > any other syntax) and new users tend to get really confused. Hmmm... I don't know, could you provide an example where you would say, like, "this is not intuitive" or even "this does something totally strange"? > Also, different versions of awk handle math (esp floating point) with > different rounding/precision/overflow, making calculations vary between > installations, only further adding to the confusion. Yes, this is true, but keep in mind what awk is: a "pattern-directed scanning and processing language". If you want higher precision math, use system(" | dc") and incorporate the result; awk isn't really for math, but integer math is usually fine. :-) -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...