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?
The short answer is "No". The slightly longer answer is "No, awk does not have eval".
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.
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: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 793 |
Nodes: | 10 (1 / 9) |
Uptime: | 39:43:37 |
Calls: | 11,106 |
Calls today: | 3 |
Files: | 186,086 |
Messages: | 1,751,471 |