From owner-freebsd-questions@freebsd.org Tue Apr 18 14:49:41 2017 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 713E9D44EA8 for ; Tue, 18 Apr 2017 14:49:41 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mailrelay10.qsc.de (mailrelay10.qsc.de [212.99.163.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.antispameurope.com", Issuer "TeleSec ServerPass DE-2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id D74DB14BB for ; Tue, 18 Apr 2017 14:49:40 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de ([213.148.129.14]) by mailrelay10.qsc.de; Tue, 18 Apr 2017 16:49:37 +0200 Received: from r56.edvax.de (port-92-195-114-44.dynamic.qsc.de [92.195.114.44]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx01.qsc.de (Postfix) with ESMTPS id 74EEA3CBF9; Tue, 18 Apr 2017 16:49:36 +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 v3IEna6B007092; Tue, 18 Apr 2017 16:49:36 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Tue, 18 Apr 2017 16:49:36 +0200 From: Polytropon To: Jim Trigg Cc: Ernie Luzar , freebsd-questions@freebsd.org Subject: Re: awk help Message-Id: <20170418164936.98e78648.freebsd@edvax.de> In-Reply-To: <88bf3158d9f36b574df2fa15b8c0883a@scadian.net> References: <58F25A01.1060208@gmail.com> <7951DF71-5CD3-4B53-9CB4-13CAA8945983@huiekin.org> <58F4CD14.7090008@gmail.com> <58F53EEA.2030206@gmail.com> <20170418021926.8410148b.freebsd@edvax.de> <88bf3158d9f36b574df2fa15b8c0883a@scadian.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-cloud-security-sender: freebsd@edvax.de X-cloud-security-recipient: freebsd-questions@freebsd.org X-cloud-security-Virusscan: CLEAN X-cloud-security-disclaimer: This E-Mail was scanned by E-Mailservice on mailrelay10.qsc.de with DD8AB683475 X-cloud-security-connect: mx01.qsc.de[213.148.129.14], TLS=1, IP=213.148.129.14 X-cloud-security: scantime:.1176 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Apr 2017 14:49:41 -0000 On Tue, 18 Apr 2017 10:23:38 -0400, Jim Trigg wrote: > On 2017-04-17 20:19, Polytropon wrote: > > On Mon, 17 Apr 2017 18:17:14 -0400, Ernie Luzar wrote: > >> Also can a csh $variable be used inside of an awk program? > > > > No directly. A sh (not csh!) variable is prefixed by $, but the > > awk program is typically enclosed in single quotes which prohibit > > the normal function of $FOO or ${FOO}; awk uses $ itself, for > > example as field identifiers like $0, $1, $2 and so on. > > > > If you'd have _no_ $ in your awk code, you could probably do > > something like this: > > > > #!/bin/sh > > FOO=100 > > awk "BEGIN { print $FOO }" > > > > But of course, now you'll get problems using double quotes in awk. > > > > However, there is (at least) a way to deal with this problem: Prefix > > the data you're going to process with "special lines", let's say > > they start with #, a name (the "variable name", a =, and the "value". > > You can easily generate this as a temporary file from your "glue" > > script. > [snip] > > > > I'm sure there are several other ways of doing this, but maybe those > > two examples can help or at least inspire you. :-) > > The trivial way is: > awk -v FOO=$FOO '{...}' Thanks for the reminder - it's really a very comfortable way! I should be re-readinging the manpage from time to time to remind me of this nice feature. > Note also that instead of embedding the awk script in the shell script > you can make it a separate file and call it with > awk -v FOO=$FOO -e script.awk Quick addition: The form is awk -v FOO=$FOO -f script.awk It's -f, not -e. :-) -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...