Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Converting String To Integer/Float (weird case)

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
sekfarok sekfarok is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 10 January 2017, 11:32 PM EST
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
Converting String To Integer/Float (weird case)

Hi guys,

I'm new here. I have a problem at work. One of our scripts was eventually having a bug and only detected recently. Here's the issue and background:

Bash Script which calls AWK script

Awk script returns a string as per below (example):
Code:
var1='00000-123'

So, when we convert it, the value becomes 0 and it messes up with our grand total of that var1.

Any idea how can I convert above string into -123?

Last edited by rbatte1; 1 Week Ago at 11:41 AM.. Reason: Added CODE tags
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 21 January 2017, 6:48 AM EST
Location: Foxborough, MA
Posts: 8,013
Thanks: 200
Thanked 715 Times in 669 Posts
you probably need to fix your awk, but YMMV:

Code:
echo '00000-123'| bc


Last edited by vgersh99; 1 Week Ago at 11:14 AM..
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 21 January 2017, 8:14 AM EST
Location: Chennai
Posts: 2,356
Thanks: 517
Thanked 1,096 Times in 993 Posts
Hello sekfarok,

Could you please try following.

Code:
echo $var1 | awk '{sub(/^0+/,"");printf("%d\n",$0)}'
OR
echo $var1 |  bc

Thanks,
R. Singh
    #4  
Old Unix and Linux 1 Week Ago
sekfarok sekfarok is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 10 January 2017, 11:32 PM EST
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by vgersh99 View Post
YMMV:

Code:
echo '00000-123'| bc

Wonder where shall i put it in my code. basically my awk script looks like below:


Code:
BEGIN {
some codes here
}
#so it search for some Trailer with T as indicator
/^T/{
       var1=substr($0,100,9);  <-- this part grep the '00000-123'
       print var1;
}
END

Linux
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 21 January 2017, 8:14 AM EST
Location: Chennai
Posts: 2,356
Thanks: 517
Thanked 1,096 Times in 993 Posts
Hello sekfarok,

Could you please try changing var1=substr($0,100,9); to var1=substr($0,106,4);, off course not tested it as lack of complete details and sample Input_file.

Thanks,
R. Singh
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 21 January 2017, 6:48 AM EST
Location: Foxborough, MA
Posts: 8,013
Thanks: 200
Thanked 715 Times in 669 Posts
Quote:
Originally Posted by sekfarok View Post
Wonder where shall i put it in my code. basically my awk script looks like below:


Code:
BEGIN {
some codes here
}
#so it search for some Trailer with T as indicator
/^T/{
       var1=substr($0,100,9);  <-- this part grep the '00000-123'
       print var1;
}
END

Linux
can you show a sample file/output that this awk is trying to parse?
Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago
sekfarok sekfarok is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 10 January 2017, 11:32 PM EST
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
ahh sorry.

Currently (ignore the position). Let say i have a few records with 15 character long (fixed length).

Input

Code:
T00001000000234
T0000200000-123     <-- this should become -123
T00003000000234
T00004000000234
T00005000-11234     <-- this should become -11234
T00006000000234
T00007000000234


the var1 should be something like: var1=substr($0,7,9)

and record 2 and 5 are having '-' sign but in different position. Hope this is enough sample.

Last edited by vgersh99; 1 Week Ago at 11:34 AM.. Reason: code tags, please!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Converting string to integer MIA651 Shell Programming and Scripting 10 04-15-2016 02:47 PM
Converting integer to String ROOZ Shell Programming and Scripting 1 06-05-2008 02:38 PM
Converting a String variable into Integer neeto UNIX for Dummies Questions & Answers 2 05-22-2008 04:38 PM
Converting the case of the string? skyineyes Shell Programming and Scripting 5 07-09-2007 06:27 AM
command for converting string to integer esham Shell Programming and Scripting 1 08-04-2005 08:20 AM



All times are GMT -4. The time now is 09:23 AM.