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
    #8  
Old Unix and Linux 01-10-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 22 February 2017, 5:59 AM EST
Location: Chennai
Posts: 2,375
Thanks: 523
Thanked 1,104 Times in 999 Posts
Hello sekfarok,

Still 100% not sure, could you please try following.

Code:
awk '/^T/ && /-/{split($0, A,"-");print "-"A[2]}'   Input_file
or
awk '/^T/ && /-/{sub(/.*-/,"-");print}'  Input_file

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
sekfarok (01-10-2017)
Sponsored Links
    #9  
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 RavinderSingh13 View Post
Hello sekfarok,

Still 100% not sure, could you please try following.

Code:
awk '/^T/ && /-/{split($0, A,"-");print "-"A[2]}'   Input_file
or
awk '/^T/ && /-/{sub(/.*-/,"-");print}'  Input_file

Thanks,
R. Singh
Thanks Ravinder. I've tested your solutions and it works if i use a single line awk command. But my scripts are in an awk script file. so my scripts is calling my myscript.awk file.

here is how my script runs.

inside my myksh.ksh

awk -f myawk.awk ${input_file}

cheers
Sponsored Links
    #10  
Old Unix and Linux 01-10-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 22 February 2017, 5:59 AM EST
Location: Chennai
Posts: 2,375
Thanks: 523
Thanked 1,104 Times in 999 Posts
Hello sekfarok,

Could you please try it like following, it should fly then.

Code:
cat Input_file.awk
/^T/ && /-/{sub(/.*-/,"-");print}
###Run it like following.
awk -f  Input_file.awk Input_file

Thanks,
R. Singh
    #11  
Old Unix and Linux 01-10-2017
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 21 February 2017, 9:02 PM EST
Location: Foxborough, MA
Posts: 8,046
Thanks: 205
Thanked 732 Times in 683 Posts
a bit convoluted, but doesn't rely on the fixed-width and/or hardwired column width values, but:

Code:
awk -F'T0+[1-6]+0' '/^T/{print (($2~/-/)?substr($2,index($2,"-")+1):$2+0)}' myFile

You can change your awk script accordingly - including the setting of FS in the BEGIN block.

Last edited by vgersh99; 01-10-2017 at 12:04 PM..
The Following User Says Thank You to vgersh99 For This Useful Post:
sekfarok (01-10-2017)
Sponsored Links
    #12  
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 RavinderSingh13 View Post
Hello sekfarok,

Could you please try it like following, it should fly then.

Code:
cat Input_file.awk
/^T/ && /-/{sub(/.*-/,"-");print}
###Run it like following.
awk -f  Input_file.awk Input_file

Thanks,
R. Singh
if i add that part, it gives me error: no space. my example is a snapshot of what my awk file have. and those numbers without the '-' sign needs to be converted too but that has no issue. only the negative part is.

thanks for helping tho.Linux
Sponsored Links
    #13  
Old Unix and Linux 01-10-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 February 2017, 2:28 PM EST
Location: Aachen, Germany
Posts: 10,313
Thanks: 242
Thanked 3,145 Times in 2,910 Posts
Try

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

The Following User Says Thank You to RudiC For This Useful Post:
sekfarok (01-10-2017)
Sponsored Links
    #14  
Old Unix and Linux 01-10-2017
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 21 February 2017, 9:42 AM EST
Location: Loughborough
Posts: 1,148
Thanks: 337
Thanked 299 Times in 231 Posts
OSX 10.12.2, default bash terminal followed by dash, longhand.

Code:
Last login: Tue Jan 10 20:16:48 on ttys000
AMIGA:amiga~> var='00000-123'
AMIGA:amiga~> echo $(($var))
-123
AMIGA:amiga~> dash
AMIGA:\u\w> var='00000-123'          
AMIGA:\u\w> echo $(($var))
-123
AMIGA:\u\w> exit
AMIGA:amiga~> _


Code:
Last login: Tue Jan 10 20:58:45 on ttys000
AMIGA:amiga~> var='T0000200000-123'
AMIGA:amiga~> echo "$((0-${var#*-}))"
-123
AMIGA:amiga~> -

EDIT:
Scratch this as I should have read the whole post before posting it.

Last edited by wisecracker; 01-10-2017 at 05:28 PM.. Reason: See above.
The Following User Says Thank You to wisecracker For This Useful Post:
sekfarok (01-10-2017)
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 07:00 AM.