Numeral conversion in awk


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Numeral conversion in awk
# 1  
Numeral conversion in awk

I am running into conversion of string to numbers in awk that I can't understand and don't know how to deal with properly

My script checks for numeric only field, I use this approach to do that:

Code:
$1 + 0 == $1

It works most of the time, but in some cases it does not behave how I expect it to behave, for example for value 673E5 script wrongly assumes it is numeric.

Please see below a series of values and how it is converted, hope it makes my question clearer:

Code:
  
 echo "67305"|awk '{s=$1+0==$1?"Eq":"No"; print $1 ";" $1+0 ";" s}'
 67305;67305;Eq
  
 echo "673N5"|awk '{s=$1+0==$1?"Eq":"No"; print $1 ";" $1+0 ";" s}'
 673N5;673;No
  
 echo "673E5"|awk '{s=$1+0==$1?"Eq":"No"; print $1 ";" $1+0 ";" s}'
 673E5;67300000;Eq

I used both awk and gawk, same reaction.

See how last example is badly converted and compare comes back with positive somehow.
What do I do wrong?
# 2  
If it's not numeric, don't add zero to it.

In this case it's treating the E as an exponent, 673 * 10^5. That's actually a pretty standard notation for it.
# 3  
Thanks for explaining the E-based notation, I forgot about it.

As I indicated, I use +0 approach to determine if the field is numeric, can't use your suggestion.
# 4  
You only want to test if a given value is numeric? That'll be easier...

Code:
  echo "673E5"|awk '/^[0-9]+$/{print $0" is numeric"}'

# 5  
Thank you.

As I am adapting your code to my script (I check 1st field to be numeric) I am not getting right answer, can you help me with my syntax here, please:

Code:
 $ echo "67E15"|awk '{if($1 ~ /[0-9]+$/){print $1" is numeric"}}'
67E15 is numeric

+++
I see - I missed the "^" (beginning of string) - Thanks it works now

Last edited by migurus; 06-21-2016 at 07:37 PM.. Reason: spotted my error
# 6  
You can shorten that to the code given in the post above yours.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #792
Difficulty: Easy
In mathematics and digital electronics, a binary number is a number expressed in the base-2 numeral system or binary numeral system.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Base64 conversion in awk overlaps

hi, problem: output is not consistent as expected using external command in AWK description: I'm trying to convert $2 into a base64 string for later decoding, and for this when I use awk , I'm getting overlapped results , or say it results are not 100% correct. my code is: gawk... (9 Replies)
Discussion started by: busyboy
9 Replies

2. Shell Programming and Scripting

Conversion of line via awk or etc

Hello friends, could you help me about problem with my data lines. I suppose a simple awk code may help me. I have following data lines: (first line including 3 numbers and then a matrices of 4x10) 500 40 9 1 A B 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22... (6 Replies)
Discussion started by: rpf
6 Replies

3. Shell Programming and Scripting

awk string to number conversion

Can someone explain whats happening here: $ awk 'BEGIN {print (2.5 - 1)}' 1,5 2.5 - 1 is correctly calculated to 1,5 (using european locale) $ 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... (4 Replies)
Discussion started by: beow
4 Replies

4. Shell Programming and Scripting

Wiki conversion with Awk or Sed

I have the words in twiki format that I want to convert to mediawiki format like below : %BLUE%some words1%ENDCOLOR% bla bla blab labdad sdadasd adsasdads oerdkfj kdfjs %PINK%some wordks2 123.4.5.6/26%ENDCOLOR%, ksdjak dkasjd kjfrjkfgjdkfgjdfkgjdgdfgdgf %PURPLE%1.2.3.4/28%ENDCOLOR%, dskd... (3 Replies)
Discussion started by: rk4k
3 Replies

5. Shell Programming and Scripting

HPUX to Linux awk conversion

I have the following script to edit ^'s and newlines out of files to be entered into a database. This script has been around since the dawn of time (way before me). #!/bin/bash # Remove all ^ and \n from the source file, except do not remove ^^^\n cat myfile.hold | awk ' BEGIN {FS="|";... (1 Reply)
Discussion started by: insania
1 Replies

6. Shell Programming and Scripting

File conversion and awk

Hi Everyone, I am confused with the output of the input file and I am using below command in script to get the expected output. Also I want to add another condition using logical AND (&&) in place of $2=="L"{$4=0-$4} as $2=="L" && $3=="L" {$4=0-$4} but I am getting some awk error. Can someone... (6 Replies)
Discussion started by: gehlnar
6 Replies

7. Shell Programming and Scripting

AWK Currency Conversion

How can I use awk command to convert values to currency. For example I have a database like follows John:200 smith:300 kim:405 and want it to out put like this John $200.00 (3 Replies)
Discussion started by: 3junior
3 Replies

8. Shell Programming and Scripting

awk script for date conversion

hi awk script for dd/mm/yyyy to yyyymmdd awk script for dd-mon-yyyy to yyyymmdd awk script for dd-mm-yyyy to yyyymmdd formate ..............urgent............. Thanks in advanced (2 Replies)
Discussion started by: charandevu
2 Replies

9. 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

Featured Tech Videos