awk string to number conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk string to number conversion
# 1  
Old 07-14-2011
awk string to number conversion

Can someone explain whats happening here:

Code:
$ awk 'BEGIN {print (2.5 - 1)}'
1,5

2.5 - 1 is correctly calculated to 1,5 (using european locale)

Code:
$ echo "2.5" | awk '{temp = $1 - 1; print temp}'
1

If i now pipe the string 2.5 through awk it seems at it truncates 2.5 to 2?

What's the difference in the two cases and how do I get it right when piping the value through awk?
# 2  
Old 07-14-2011
Works fine for me. Try nawk or gawk. Try printf("%f\n", temp);
# 3  
Old 07-14-2011
Wow, that was fast... Smilie

However doesn't work on my Mac with its awk

Code:
$ echo "2.5" | awk '{temp = $1 - 1; printf("%f\n", temp)}'
1,000000

$ awk --version
awk version 20070501

any other ideas?

---------- Post updated at 04:42 PM ---------- Previous update was at 04:38 PM ----------

It seem as it has something to do with the locale:

Code:
$ echo "2,5" | awk '{temp = $1 - 1; printf("%f\n", temp)}'
1,500000

Works when I'm piping "2,5" instead of "2.5"
# 4  
Old 07-14-2011
Quote:
Originally Posted by beow
Can someone explain whats happening here:

Code:
$ awk 'BEGIN {print (2.5 - 1)}'
1,5

2.5 - 1 is correctly calculated to 1,5 (using european locale)

Code:
$ echo "2.5" | awk '{temp = $1 - 1; print temp}'
1

If i now pipe the string 2.5 through awk it seems at it truncates 2.5 to 2?

What's the difference in the two cases and how do I get it right when piping the value through awk?
The difference is that in the first case, the 2.5 is a numeric literal within the AWK programming language. The representation of literals in the code are not subject to locale. In the second case, awk is converting external data using a locale aware process (probably atof() or strtod() or something similar).

You need to set the locale according to the type of data you're going to process. In this case, at the very least set LC_NUMERIC to the "POSIX" ("C") locale.

---------- Post updated at 12:14 PM ---------- Previous update was at 12:08 PM ----------

By the way, AWK is allowed but not required to use locale when converting strings to numbers. An excerpt from the standard:

Quote:
A string value shall be converted to a numeric value either by the equivalent of the following calls to functions defined by the ISO C standard:

setlocale(LC_NUMERIC, "");
numeric_value = atof(string_value);

or by converting the initial portion of the string to type double representation as follows:

The input string is decomposed into two parts: an initial, possibly empty, sequence of white-space characters (as specified by isspace()) and a subject sequence interpreted as a floating-point constant.

The expected form of the subject sequence is an optional '+' or '-' sign, then a non-empty sequence of digits optionally containing a <period>, then an optional exponent part. An exponent part consists of 'e' or 'E' , followed by an optional sign, followed by one or more decimal digits.

The sequence starting with the first digit or the <period> (whichever occurs first) is interpreted as a floating constant of the C language, and if neither an exponent part nor a <period> appears, a <period> is assumed to follow the last digit in the string. If the subject sequence begins with a minus-sign, the value resulting from the conversion is negated.
The section which states that regarless of locale, AWK literals always use the dot as a radix character:

Quote:
LC_NUMERIC
Determine the radix character used when interpreting numeric
input, performing conversions between numeric and string values,
and formatting numeric output. Regardless of locale, the period
character (the decimal-point character of the POSIX locale) is
the decimal-point character recognized in processing awk pro-
grams (including assignments in command line arguments).
That and more @ http://pubs.opengroup.org/onlinepubs...ities/awk.html

---------- Post updated at 12:21 PM ---------- Previous update was at 12:14 PM ----------

Example to illustrate that you should use a locale that's appropriate to the data you're working with:
Code:
$ echo 2.5 | LC_NUMERIC=POSIX awk '{print $0-1}'
1.5
$ echo 2.5 | LC_NUMERIC=fr_FR awk '{print $0-1}'
1

Regards,
Alister
# 5  
Old 07-14-2011
OK, thanks, it explains the problem. Solved it by just substituting with sed:

Code:
$ echo "2.5" | sed 's/\./,/' | awk '{temp = $1 - 1; printf("%f\n", temp)}'
1,500000

That's enough for my needs in this case and I can then continue to work with the "right" locale.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

awk - find number in string

I am trying to go through a file that has a few million lines. I want to only pull lines that contain a number anywhere in the ninth field, but it has to be after a "/" character. Here is my awk: awk -F\| '$9 ~ /\/*{1,}*/ {print $0}' file1 > file2 However, it is just printing out every... (3 Replies)
Discussion started by: dagamier
3 Replies

3. Shell Programming and Scripting

Getting awk Output as number instead of String

Hi All, I have a file a.txt, content as mentioned below: 22454750 This data in this control file and I have a variable called vCount which contains a number. I need to extract the 22454750 from the above file and compare with the variable vCount. If match fine or else exit. ... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

4. Programming

get ID number from one string name by using awk

Hi guys, I am new to unix shell scripts. I have a file-A.txt which contained several names in "ABCo12345678.gz_to_ABCn12345678.gz" format. I want to extract the numbers in a "for" loop that means I can not use cut -c6-13 A.txt.Dose anyone know how to do it by using awk? Thank you so much. ... (4 Replies)
Discussion started by: shrimpj
4 Replies

5. Shell Programming and Scripting

[awk]compare a number in a string with a list

Hi, I have a program written in awk and I want to extend it to do another task. My program is a list of CVS log reports of a repository. For each file, I have some fields. One of the fields is the comment field. I want to know how I can check if a comment (which is a free text field)... (8 Replies)
Discussion started by: sandeepk1611
8 Replies

6. UNIX for Dummies Questions & Answers

AWK - number of specified characters in a string

Hello, I'm new to using AWK and would be grateful for some basic advice to get me started. I have a file consisting of 10 fields. Initially I wish to calculate the number of . , ~ and ^ characters in the 9th field ($9) of each line. This particular string also contains alphabetical... (6 Replies)
Discussion started by: Olly
6 Replies

7. UNIX for Advanced & Expert Users

Date Conversion on output string from awk

Hi, I would like to convert the output from awk function to date and print on the screen. Example : echo "Start Date: May 24 2010" | gawk -F": " '{print $2}' Output : May 04 2010 I want this to be converted to 2010/05/24 Can i use date function here and how? Thanks, Deepika (2 Replies)
Discussion started by: deepikad
2 Replies

8. Shell Programming and Scripting

change awk string result to number

How could I change the lines with grep and awk to output a number instead of a string? case '2': @ L0 = 1 @ LN = `grep RS_D resample.in | awk '{print $3}'` @ P0 = 1 @ PN = `grep RS_D resample.in | awk '{print $5}'` # ||| fall through ||| Cheers (1 Reply)
Discussion started by: larne
1 Replies

9. Shell Programming and Scripting

awk/sed - getting string instead of number

Hi! I am writing a script handling downloading list of files and I have to check whether file is present locally and if not finished than continue downloading. To do so I have to compare sizes of remote file and local file. To check remote file size I have to parse something like this: ... (2 Replies)
Discussion started by: hrwath
2 Replies

10. Shell Programming and Scripting

String Conversion in awk

I am porting a awk script from Windows to unix I_SALE_MEDIA=$67 if ((I_VOID_FLAG == "Y") && (I_SALE_MEDIA == 0)) NOW consider the case where I_SALE_MEDIA i.e $67 is "000" The above comparison works fine in Windows , but to make it work in Unix , I had to change the above as follows : ... (3 Replies)
Discussion started by: rohanrege
3 Replies
Login or Register to Ask a Question