From owner-freebsd-questions@FreeBSD.ORG Mon Dec 17 16:57:22 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 592333EB; Mon, 17 Dec 2012 16:57:22 +0000 (UTC) (envelope-from Devin.Teske@fisglobal.com) Received: from mx1.fisglobal.com (mx1.fisglobal.com [199.200.24.190]) by mx1.freebsd.org (Postfix) with ESMTP id 154238FC12; Mon, 17 Dec 2012 16:57:21 +0000 (UTC) Received: from smtp.fisglobal.com ([10.132.206.31]) by ltcfislmsgpa06.fnfis.com (8.14.5/8.14.5) with ESMTP id qBHGvIDX002186 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Mon, 17 Dec 2012 10:57:18 -0600 Received: from [10.0.0.102] (10.14.152.61) by smtp.fisglobal.com (10.132.206.31) with Microsoft SMTP Server (TLS) id 14.2.309.2; Mon, 17 Dec 2012 10:57:17 -0600 Subject: Re: using AWK MIME-Version: 1.0 (Apple Message framework v1283) From: Devin Teske In-Reply-To: <20121217172316.79d8e198.freebsd@edvax.de> Date: Mon, 17 Dec 2012 08:57:16 -0800 Message-ID: <0EFF6378-A6EE-4222-8F3A-5F437E69D3BB@fisglobal.com> References: <1355744359.61103.YahooMailNeo@web160104.mail.bf1.yahoo.com> <18B0C79B-AF04-48D1-AF26-1B8A8B3641C1@fisglobal.com> <20121217172316.79d8e198.freebsd@edvax.de> To: Polytropon X-Mailer: Apple Mail (2.1283) X-Originating-IP: [10.14.152.61] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.9.8327, 1.0.431, 0.0.0000 definitions=2012-12-17_03:2012-12-17,2012-12-17,1970-01-01 signatures=0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.14 Cc: Devin Teske , FreeBSD Questions , Jack Mc Lauren X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Devin Teske List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Dec 2012 16:57:22 -0000 On Dec 17, 2012, at 8:23 AM, Polytropon wrote: > On Mon, 17 Dec 2012 08:16:26 -0800, Devin Teske wrote: >>=20 >> On Dec 17, 2012, at 3:39 AM, Jack Mc Lauren wrote: >>=20 >>> Hi guys >>>=20 >>> How can I read a file which contains a number and assign that number to= a variable via awk programming? By the way, I want to use this awk program= in a shell script. >>>=20 >>> Thanks in advance >>=20 >> Try this: >>=20 >> awk -v file=3D/etc/ttys 'BEGIN { getline line =20 > Or more verbose: >=20 >=20 >=20 >=20 > #!/bin/sh >=20 > filename=3D$1 >=20 > echo "file is ${filename} with content:" > cat ${filename} >=20 > echo "calling awk..." > awk -v filename=3D$filename "BEGIN {=20 > getline no < filename > close filename > print no > print no * 2 > }" >=20 >=20 >=20 >=20 > # EXAMPLE: > # -------- > # > # % ./awkvar.sh /tmp/no.txt > # file is /tmp/no.txt with content: > # 12345 > # calling awk... > # 12345 > # 24690 >=20 >=20 >=20 > The example shows how to use the variable inside awk. You > could get rid of the getline function in case the file > contains only the number you're interested in. If you need > further processing of the file, you can do that inside > awk (e. g. omitting comment lines, obtain data from a given > line number of specific pattern). >=20 >=20 >=20 >=20 > --=20 > Polytropon > Magdeburg, Germany > Happy FreeBSD user since 4.0 > Andra moi ennepe, Mousa, ... _____________ The information contained in this message is proprietary and/or confidentia= l. If you are not the intended recipient, please: (i) delete the message an= d all copies; (ii) do not disclose, distribute or use the message in any ma= nner; and (iii) notify the sender immediately. In addition, please be aware= that any message addressed to our domain is subject to archiving and revie= w by persons other than the intended recipient. Thank you.