Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Why is float missing in awk on my RHEL 5.8? Was it replaced by some other function?

On Solaris:

echo "Foampile=123" | awk -F"=" '{ print float($2) "<->" $1 }'

returns

123<->Foampile

on RHEL 5.8

awk: (FILENAME=- FNR=1) fatal: function `float' not defined
share|improve this question

2 Answers 2

up vote 2 down vote accepted

Solaris /usr/bin/awk is a very old awk that doesn't support user functions. So in that awk, float(123) is the same as anything(123) or anything 123, that is the concatenation of the value of the float or anything variable (empty if not set) and 123. So, it's not an error, but it does nothing.

Had you written

echo "Foampile=123" | awk -F"=" '{float="x"; print float($2) "<->" $1}'

you'd have seen

x123<->Foampile

I don't think that there is any awk implementation that has a float function.

What would you expect that function to do anyway?

On the other hand, modern POSIX awks like Solaris /usr/xpg4/bin/awk or nawk or gawk do support user functions, so in those, unless you define the float or anything function, you'll see that error.

echo "Foampile=123" | awk -F"=" '{ print $2 "<->" $1 }'

would work exactly the same (and would work with modern awks).

In modern awks, to disambiguate between a function call and the concatenation of a variable and something within brace, you need to add at least one extra space:

$ echo x | awk '{print foo ($1)}'
x
$ echo x | awk 'function foo(x) {return "y"}; {print foo($1)}'
y
share|improve this answer
    
I am migrating some scripts (that I didn't write) from SunOS->RHEL. My goal is to get the script to do the same thing as in the previous OS by evaluating ins and outs but not really diving into why things like float and such were used. Hope this makes sense. –  amphibient Nov 26 '12 at 21:09

I think that's a Solaris specific function. You might need to instead use printf or sprintf and specify the float.

awk 'BEGIN {printf "%1##f\n", "<->" $1}

share|improve this answer
    
in Solaris i don't get an error. in RHEL i do. –  amphibient Nov 26 '12 at 21:11
    
@foampile You need to replace "##" with real numbers. –  jordanm Nov 26 '12 at 21:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.