using AWK

Ben Cottrell tamino at wolfhut.org
Mon Dec 17 12:00:25 UTC 2012


Hi Jack,

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


More information about the freebsd-questions mailing list