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
    #15  
Old Unix and Linux 01-10-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 27 June 2017, 9:15 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
It isn't really clear to me what you're trying to do in cases where there is no <hyphen> character in your output from awk. Is a return value like 000000234 to remain unchanged or do you want to remove leading zeros?

If you always want to remove leading zeros (and never have a string that is all zeros and don't want to change your awk script, you don't need to use bc to perform your arithmetic; it can all be done with variable expansions:

Code:
$ x=000000234
$ echo ${x##*[^1-9-]}
234
$ x=0-123456
$ echo ${x##*[^1-9-]}
-123456
$ 

The Following 2 Users Say Thank You to Don Cragun For This Useful Post:
sekfarok (01-10-2017), vgersh99 (01-11-2017)
Sponsored Links
    #16  
Old Unix and Linux 01-10-2017
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 RudiC View Post
Try

Code:
awk '{X = substr ($1,7,9); sub (/^0+/, "", X); print X}' file
234
-123
234
234
-11234
234
234

Thank you so much. tested your solutions and it works. all my calculations are correct too now LinuxLinux
Sponsored Links
    #17  
Old Unix and Linux 01-11-2017
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 22 June 2017, 3:16 PM EDT
Location: Loughborough
Posts: 1,210
Thanks: 364
Thanked 322 Times in 252 Posts
Quote:
Originally Posted by Don Cragun View Post
It isn't really clear to me what you're trying to do in cases where there is no <hyphen> character in your output from awk. Is a return value like 000000234 to remain unchanged or do you want to remove leading zeros?

If you always want to remove leading zeros (and never have a string that is all zeros and don't want to change your awk script, you don't need to use bc to perform your arithmetic; it can all be done with variable expansions:

Code:
$ x=000000234
$ echo ${x##*[^1-9-]}
234
$ x=0-123456
$ echo ${x##*[^1-9-]}
-123456
$ 

Sadly it is not entirely foolproof:-
OSX 10.12.2, default bash terminal.
Here 230 is a valid number.

Code:
Last login: Wed Jan 11 18:39:25 on ttys000
AMIGA:amiga~> x=000000230
AMIGA:amiga~> echo ${x##*[^1-9-]}

AMIGA:amiga~> _

    #18  
Old Unix and Linux 01-11-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 June 2017, 6:06 AM EDT
Location: Aachen, Germany
Posts: 10,972
Thanks: 280
Thanked 3,373 Times in 3,106 Posts
Try echo ${x#${x%%[1-9-]*}}, then.
The Following 3 Users Say Thank You to RudiC For This Useful Post:
Don Cragun (01-12-2017), RavinderSingh13 (01-12-2017), wisecracker (01-11-2017)
Sponsored Links
    #19  
Old Unix and Linux 01-12-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 27 June 2017, 9:15 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
Unfortunately, the code I suggested in post #15 only works if all zeros in the string are leading zeros. Linux Fortunately, RudiC posted the fix that does what I was trying to do in post #18. Linux

Thanks RudiC! Linux
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 01:47 PM
Converting integer to String ROOZ Shell Programming and Scripting 1 06-05-2008 01:38 PM
Converting a String variable into Integer neeto UNIX for Dummies Questions & Answers 2 05-22-2008 03:38 PM
Converting the case of the string? skyineyes Shell Programming and Scripting 5 07-09-2007 05:27 AM
command for converting string to integer esham Shell Programming and Scripting 1 08-04-2005 07:20 AM



All times are GMT -4. The time now is 06:33 AM.