LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   awk/nawk returning decimal values? (https://www.linuxquestions.org/questions/linux-general-1/awk-nawk-returning-decimal-values-630560/)

moutaye 03-25-2008 01:51 PM

awk/nawk returning decimal values?
 
Hi

Running a specific nawk statement over a 17m lines files returns the following:

/bin/nawk: not enough args in .....
input record number 1,25955e+06, file test.1
source line number 1

I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out.

I tried to pass the OTMF variable in the nawk statement to "%.8d" but nothing helped.

Any awk/nawk expert woudl be able to figure this out?

Thanks for your help guys

colucix 03-25-2008 02:22 PM

Maybe it is a typo, but the variable is OFMT. According to the AWK manual:
Quote:

According to the POSIX standard, awk’s behavior is undefined if OFMT contains anything but a floating-point conversion specification.
It is a good practice to define OFMT as "%.0f" to print numbers as integers. If the number has some decimal digits, it results in a rounding effect. I hope this will help.

moutaye 03-25-2008 02:31 PM

thanks for the reply colucix

i actually meant OFMT

i tried your suggestion as follows:

/bin/nawk -F'|' '{OFMT="%.0f"; printf $1;printf "|"; printf $1;printf "|"; printf $2;printf "|"; printf $3;printf "|"; printf $3;printf "|"; printf $4;printf..........

I still get the float type value returned not the integer...

Any other idea?

Thanks for this

Tinkster 03-25-2008 03:30 PM

Tried gawk?
Code:

echo "" |awk '{printf"%d\n", 12595594224142}'
12595594224142

And what's with all the printf-statements?

Code:

awk -F'|' '{printf "%d | %s | %s | %s | %s | %s |\n", NR, $1, $2, $3, $4, $5}' file


Cheers,
Tink

colucix 03-25-2008 03:57 PM

Two notes: 1. the variable OFMT controls the output of the print statement, which internally uses the C function sprintf to convert numbers to strings. If you use the printf statement, you can specify the format as in
Code:

printf "%.0f",$1  # or simply
printf "%d",$1

2. The format specified by the OFMT variable affects only the printing of numbers. But if you try to print an input field as in
Code:

print $1
the output is not affected by OFMT even if $1 represents a numeric value, since variables and fields are considered strings in awk, unless the context forces them to be treated as numbers. A workaround to force awk to consider variables and fields as numbers is to add 0, for example the statement
Code:

print $1+0
force awk to consider $1 as a number, due to the presence of an arithmetic operator and in this case it is affected by the format specified by OFMT.

moutaye 03-25-2008 04:42 PM

thanks for the help on this guys

i might not have been clear but i'm not trying to convert float values or fields within the file to decimal. i'm trying to convert the actual line number output of the incriminated line reported by default by awk as a float if it exceeds 6 digits.

hence i'm actually not sure if OMFT can actually help as coculix outlined...

any other ideas?

Tinkster 03-25-2008 05:16 PM

Did you see my gawk example above? That's like outputting a line-number.
Maybe you've found an nawk limitation?

Actually I just checked: nawk will print those
long integers just fine using printf "%d"


Cheers,
Tink


All times are GMT -5. The time now is 09:48 AM.