String Conversion in awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

String Conversion in awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-14-2005
rohanrege rohanrege is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 14 February 2005, 4:24 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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 :

I_SALE_MEDIA=$67
if ((I_VOID_FLAG == "Y") && (I_SALE_MEDIA == "000" ))

Such change had to be made thruout the script for similar situation.

From what I read awk should convert the I_SALE_MEDIA to numeric 0
and then compare successfully with the zero constant numeric literal .

Any suggestions why this is happening.

Regards
Rohan
Sponsored Links
    #2  
Old 02-14-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 15 September 2014, 6:32 PM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts
if ((I_VOID_FLAG == "Y") && (int(I_SALE_MEDIA) == 0))
Sponsored Links
    #3  
Old 02-14-2005
rohanrege rohanrege is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 14 February 2005, 4:24 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply, but

Is there any other setting i.e Built in variables. or any other setting that affects how the string "000" is converted to 0 automatically in windows and not in Unix.
    #4  
Old 02-14-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 15 September 2014, 6:32 PM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts
Quote:
Originally Posted by rohanrege
Thanks for the reply, but

Is there any other setting i.e Built in variables. or any other setting that affects how the string "000" is converted to 0 automatically in windows and not in Unix.
Cannot recall, but ....... CONVFMT is somewhat related, but I could not get it working as expected.

Here's another scenario:

Code:
BEGIN {
   VOID="Y"
   A="000"

   if ( (VOID == "Y") && (A == 0) )
      print "YES"
   else
      print "NO"
}

The above outputs "NO" for awk, nawk, /usr/xpg4/bin/awk and gawk [on Solaris].


Code:
BEGIN {
   VOID="Y"
   A=000

   if ( (VOID == "Y") && (A == 0) )
      print "YES"
   else
      print "NO"
}

The above outputs "YES" for all the awk's mentioned above.

This is somewhat strange, but doing 'man nawk' on Solaris:
Quote:
expr == expr Equal to
............. other expressions......


Each expression has either a string value, a numeric value
or both. Except as stated for specific contexts, the value
of an expression is implicitly converted to the type needed
for the context in which it is used
. A string value is con-
verted to a numeric value by the equivalent of the following
calls:

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

A numeric value that is exactly equal to the value of an
integer is converted to a string by the equivalent of a call
to the sprintf function with the string %d as the fmt argu-
ment and the numeric value being converted as the first and
only expr argument. Any other numeric value is converted to
a string by the equivalent of a call to the sprintf function
with the value of the variable CONVFMT as the fmt argument
and the numeric value being converted as the first and only
expr argument.
Based on my examples, it seems like under Solaris the "context" of yje comparision is defined by the type of the LEFT expression.

Actually I just tried the same test under MKS's awk on Windows and does work the SAME as it does on Solaris. And also it behaves the same under Cygwin's awk and gawk under Windows as well - well, at least for the test scenarios I've outlined above.

Last edited by vgersh99; 02-14-2005 at 04:26 PM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[python]string to list conversion animesharma Shell Programming and Scripting 1 11-10-2011 06:42 AM
awk string to number conversion beow Shell Programming and Scripting 4 07-14-2011 12:25 PM
Hex string conversion? escudo Programming 12 10-17-2010 05:50 PM
Date Conversion on output string from awk deepikad UNIX for Advanced & Expert Users 2 08-18-2010 10:03 PM
C String manipulation/conversion help Yifan_Guo Programming 3 03-14-2005 12:47 PM



All times are GMT -4. The time now is 12:55 AM.