ex. oz to lb using old-school units(1) command:
$ units -1 oz lb
* 0.0625
# this works:
$ echo "16 $(units -1 oz lb)" |awk '{print $1 " oz = " $1 * $3 " lb"}'
16 oz = 1 lb
# this does NOT work:
$ echo "16 $(units -1 oz lb)" |awk '{print $1 " oz = " $1 $2 $3 " lb"}'
16 oz = 16*0.0625 lb
Is there a way to tell (g)awk to interpret strings like "*", "/", "+",
"-", etc. as actual math operators
or do I need to create a series of
tests of $2 in the above example?
On 2/16/23 8:36 AM, Kenny McCormack wrote:
The short answer is "No". The slightly longer answer is "No, awk does not >> have eval".
Ah okay thanks; guess I wasn't missing something in the docs after all.
I don't really have practical reason for needing an eval() function, was
just something I came across while trying other things.
The short answer is "No". The slightly longer answer is "No, awk does not have eval".
I didn't see anything in the gawk(1) manpage for doing this but it seems
like it should be possible.
On 2/16/2023 9:02 AM, someone wrote:
I didn't see anything in the gawk(1) manpage for doing this but it
seems like it should be possible.
It's not possible entirely within 1 call to awk but see https://stackoverflow.com/a/54161251/1745001 for a (slow) workaround.
Ed.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 08:27:24 |
Calls: | 6,706 |
Files: | 12,236 |
Messages: | 5,350,706 |