From owner-freebsd-questions@freebsd.org Tue Apr 18 14:23:39 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 D2C10D44353; Tue, 18 Apr 2017 14:23:39 +0000 (UTC) (envelope-from blaise@scadian.net) Received: from argent.heraldsnet.org (mail.pegasuscomputing.com [69.30.240.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "pegasuscomputing.com", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id BB243210; Tue, 18 Apr 2017 14:23:39 +0000 (UTC) (envelope-from blaise@scadian.net) Received: by argent.heraldsnet.org (Postfix, from userid 80) id A2A9561828EC; Tue, 18 Apr 2017 09:23:38 -0500 (CDT) To: Polytropon Subject: Re: awk help X-PHP-Originating-Script: 0:rcube.php MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Tue, 18 Apr 2017 10:23:38 -0400 From: Jim Trigg Cc: Ernie Luzar , freebsd-questions@freebsd.org, owner-freebsd-questions@freebsd.org In-Reply-To: <20170418021926.8410148b.freebsd@edvax.de> 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> Message-ID: <88bf3158d9f36b574df2fa15b8c0883a@scadian.net> X-Sender: blaise@scadian.net User-Agent: Roundcube Webmail/1.2.4 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:23:39 -0000 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 '{...}' 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 Jim Trigg