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 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 24 January 2017, 9:52 AM EST
Location: San Jose, CA, USA
Posts: 9,883
Thanks: 477
Thanked 3,391 Times in 2,896 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 (1 Week Ago), vgersh99 (1 Week Ago)
Sponsored Links
    #16  
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 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 1 Week Ago
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 23 January 2017, 2:54 PM EST
Location: Loughborough
Posts: 1,127
Thanks: 329
Thanked 296 Times in 228 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 1 Week Ago
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 January 2017, 10:07 AM EST
Location: Aachen, Germany
Posts: 10,169
Thanks: 230
Thanked 3,076 Times in 2,852 Posts
Try echo ${x#${x%%[1-9-]*}}, then.
The Following 3 Users Say Thank You to RudiC For This Useful Post:
Don Cragun (1 Week Ago), RavinderSingh13 (1 Week Ago), wisecracker (1 Week Ago)
Sponsored Links
    #19  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 24 January 2017, 9:52 AM EST
Location: San Jose, CA, USA
Posts: 9,883
Thanks: 477
Thanked 3,391 Times in 2,896 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 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 11:12 AM.