Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 17 Dec 2012 08:57:16 -0800
From:      Devin Teske <devin.teske@fisglobal.com>
To:        Polytropon <freebsd@edvax.de>
Cc:        Devin Teske <dteske@freebsd.org>, FreeBSD Questions <freebsd-questions@freebsd.org>, Jack Mc Lauren <jack.mclauren@yahoo.com>
Subject:   Re: using AWK
Message-ID:  <0EFF6378-A6EE-4222-8F3A-5F437E69D3BB@fisglobal.com>
In-Reply-To: <20121217172316.79d8e198.freebsd@edvax.de>
References:  <1355744359.61103.YahooMailNeo@web160104.mail.bf1.yahoo.com> <18B0C79B-AF04-48D1-AF26-1B8A8B3641C1@fisglobal.com> <20121217172316.79d8e198.freebsd@edvax.de>

next in thread | previous in thread | raw e-mail | index | archive | help


On Dec 17, 2012, at 8:23 AM, Polytropon wrote:

> On Mon, 17 Dec 2012 08:16:26 -0800, Devin Teske wrote:
>> 
>> On Dec 17, 2012, at 3:39 AM, Jack Mc Lauren wrote:
>> 
>>> 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
>> 
>> Try this:
>> 
>> awk -v file=/etc/ttys 'BEGIN { getline line <file; printf "First line from %s: %s\n", file, line }'
> 
> Or more verbose:
> 
> 
> 
> 
> #!/bin/sh
> 
> filename=$1
> 
> echo "file is ${filename} with content:"
> cat ${filename}
> 
> echo "calling awk..."
> awk -v filename=$filename "BEGIN { 
>        getline no < filename
>        close filename
>        print no
>        print no * 2
> }"
> 
> 
> 
> 
> # EXAMPLE:
> # --------
> #
> # % ./awkvar.sh /tmp/no.txt
> # file is /tmp/no.txt with content:
> # 12345
> # calling awk...
> # 12345
> # 24690
> 
> 
> 
> 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).
> 
> 
> 
> 
> -- 
> Polytropon
> Magdeburg, Germany
> Happy FreeBSD user since 4.0
> Andra moi ennepe, Mousa, ...

_____________
The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?0EFF6378-A6EE-4222-8F3A-5F437E69D3BB>