2.5 - 1 is correctly calculated to 1,5 (using european locale)
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:
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).
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)
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)
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)
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)
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)
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)
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)
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)
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)