Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 17 Dec 2012 03:39:19 -0800 (PST)
From:      Jack Mc Lauren <jack.mclauren@yahoo.com>
To:        "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
Subject:   using AWK
Message-ID:  <1355744359.61103.YahooMailNeo@web160104.mail.bf1.yahoo.com>

next in thread | raw e-mail | index | archive | help
Hi guys

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.

Thanks in advance
From owner-freebsd-questions@FreeBSD.ORG  Mon Dec 17 12:00:25 2012
Return-Path: <owner-freebsd-questions@FreeBSD.ORG>
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 DFB5962F
 for <freebsd-questions@freebsd.org>; Mon, 17 Dec 2012 12:00:25 +0000 (UTC)
 (envelope-from tamino@wolfhut.org)
Received: from pendor.wolfhut.org (pendor.wolfhut.org [173.228.91.225])
 by mx1.freebsd.org (Postfix) with ESMTP id C185E8FC0C
 for <freebsd-questions@freebsd.org>; Mon, 17 Dec 2012 12:00:25 +0000 (UTC)
Received: from [192.168.42.100] (173-228-91-224.static.sonic.net
 [173.228.91.224]) (using TLSv1 with cipher AES128-SHA (128/128 bits))
 (No client certificate requested)
 by pendor.wolfhut.org (Postfix) with ESMTPSA id E598DDBB32;
 Mon, 17 Dec 2012 03:52:56 -0800 (PST)
Content-Type: text/plain; charset=us-ascii
Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\))
Subject: Re: using AWK
From: Ben Cottrell <tamino@wolfhut.org>
In-Reply-To: <1355744359.61103.YahooMailNeo@web160104.mail.bf1.yahoo.com>
Date: Mon, 17 Dec 2012 03:52:56 -0800
Content-Transfer-Encoding: 7bit
Message-Id: <8EA88B4E-9C21-4CF7-9AB0-87663AB876F8@wolfhut.org>
References: <1355744359.61103.YahooMailNeo@web160104.mail.bf1.yahoo.com>
To: Jack Mc Lauren <jack.mclauren@yahoo.com>
X-Mailer: Apple Mail (2.1499)
Cc: "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
X-BeenThere: freebsd-questions@freebsd.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: User questions <freebsd-questions.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/options/freebsd-questions>, 
 <mailto:freebsd-questions-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/freebsd-questions>;
List-Post: <mailto:freebsd-questions@freebsd.org>
List-Help: <mailto:freebsd-questions-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-questions>, 
 <mailto:freebsd-questions-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Mon, 17 Dec 2012 12:00:25 -0000

Hi Jack,

On Dec 17, 2012, at 03:39, Jack Mc Lauren <jack.mclauren@yahoo.com> wrote:
> 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.

I'm actually not sure what you're asking, exactly -- you want the number
to go into an awk variable? Or a shell variable?

Assuming you want it to go into an awk variable, I would try something
like this:

	getline my_number < filename;
	close filename;

That assumes the filename is stored in the variable named "filename".
It puts the number in the awk variable named "my_number".

To put that in context, let's say you're getting the filename from $0,
and you want to multiply the number by 2 and print it. You might do:

	filename = $0;
	getline my_number < filename;
	close filename;
	print my_number * 2;

Or if I completely misinterpreted your question, let me know :-)

	~Ben



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1355744359.61103.YahooMailNeo>