Thursday 11 March 2010

Using awk as a floor function

I was searching the internet trying to find out if awk had a floor function when I realised awk IS a floor function.



graham@graham-laptop:~$ x=3.5
graham@graham-laptop:~$ echo $x | awk -F. '{print $1}'
3
graham@graham-laptop:~$

You could always change $1 for $2 and it becomes a mod function.

2 comments:

Satchmo said...

Excellent Tip. I for one will implement this finding immediately as I have also been researching the Internet for awk related trivia.
Thanks Graham!

declinator said...

Actually, it's not a floor function, as
echo -3.5 | awk -F. '{print $1}'
prints -3, not -4, which would be floor(-3.5).