awk and floating point arithmetic

  • Follow


Using Solaris 10, I find the following ksh function hangs for /usr/bin/awk 
(the default awk in your path) but works okay for /usr/xpg4/bin/awk;

calc() { awk "BEGIN {print $*}"; }

Why is this? Works fine with HPUX and Red Hat Linux "default" awks. 


0
Reply oparr 6/4/2005 4:20:33 PM

oparr wrote:
> Using Solaris 10, I find the following ksh function hangs for /usr/bin/awk 
> (the default awk in your path) but works okay for /usr/xpg4/bin/awk;
> 
> calc() { awk "BEGIN {print $*}"; }
> 
> Why is this? Works fine with HPUX and Red Hat Linux "default" awks. 

Because /usr/bin/awk is the ancient version of awk, and 
/usr/xpg4/bin/awk is the POSIX version. You should use the POSIX version 
whenever possible.

For example, I hit a bug in both `nawk` and `awk` that made awk hurl 
when I threw a combined match pattern at it, that had more than 800 
patterns in it:

/pattern1|pattern2|...|pattern800+/ { ... }

but /usr/xpg4/bin/awk worked fine.

0
Reply UNIX 6/4/2005 7:49:06 PM


>Because /usr/bin/awk is the ancient version of awk

This therefore begs the question "Why isn't the POSIX version in /usr/bin by 
default instead of the ancient version?". If you write a ksh script for 
various platforms then you have to replace awk with $AWK (for example) and 
do something like this at the beginning;

[[ -f /usr/xpg4/bin/awk ]] && AWK=/usr/xpg4/bin/awk  || AWK=/usr/bin/awk

This really sucks Sun.

"UNIX admin" <tripivceta@hotmail.com> wrote in message 
news:42a205af$0$1163$5402220f@news.sunrise.ch...
> oparr wrote:
>> Using Solaris 10, I find the following ksh function hangs for 
>> /usr/bin/awk (the default awk in your path) but works okay for 
>> /usr/xpg4/bin/awk;
>>
>> calc() { awk "BEGIN {print $*}"; }
>>
>> Why is this? Works fine with HPUX and Red Hat Linux "default" awks.
>
> Because /usr/bin/awk is the ancient version of awk, and /usr/xpg4/bin/awk 
> is the POSIX version. You should use the POSIX version whenever possible.
>
> For example, I hit a bug in both `nawk` and `awk` that made awk hurl when 
> I threw a combined match pattern at it, that had more than 800 patterns in 
> it:
>
> /pattern1|pattern2|...|pattern800+/ { ... }
>
> but /usr/xpg4/bin/awk worked fine.
> 


0
Reply oparr 6/4/2005 9:35:44 PM

oparr wrote:
> This therefore begs the question "Why isn't the POSIX version in /usr/bin by 
> default instead of the ancient version?". If you write a ksh script for 
> various platforms then you have to replace awk with $AWK (for example) and 
> do something like this at the beginning;

No, it is laid out correctly. Remember that on Solaris, compatibility 
comes first.

And if you are a pro, you will use $AWK anyways. That's how I solved the 
problem: instead of changing tons and tons of code, I changed one single 
line, and the whole behemoth magically worked. Scalable, clean code.

As for day to day use, contrary to Eric Boutilier, /usr/xpg4/bin should 
come first in the PATH.

> [[ -f /usr/xpg4/bin/awk ]] && AWK=/usr/xpg4/bin/awk  || AWK=/usr/bin/awk
> 
> This really sucks Sun.

Nooo, it does not suck. They've done everything correctly. You need to 
better understand how Solaris works. SVR4 binaries go in /usr/bin/ while 
POSIX binaries go in /usr/xpg4/bin. And that's correct.

0
Reply UNIX 6/5/2005 8:04:46 AM

3 Replies
444 Views

(page loaded in 0.065 seconds)

Similiar Articles:













7/21/2012 11:10:16 PM


Reply: