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


Closed    
 
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: 18 August 2017, 10:25 AM EDT
Location: Chennai
Posts: 2,567
Thanks: 567
Thanked 1,211 Times in 1,090 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: 18 August 2017, 10:25 AM EDT
Location: Chennai
Posts: 2,567
Thanks: 567
Thanked 1,211 Times in 1,090 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: 17 August 2017, 5:36 PM EDT
Location: Foxborough, MA
Posts: 8,180
Thanks: 222
Thanked 780 Times in 728 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 11:04 AM..
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 online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 August 2017, 10:29 AM EDT
Location: Aachen, Germany
Posts: 11,171
Thanks: 287
Thanked 3,442 Times in 3,171 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: 17 August 2017, 5:52 AM EDT
Location: Loughborough
Posts: 1,216
Thanks: 365
Thanked 323 Times in 253 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 04:28 PM.. Reason: See above.
The Following User Says Thank You to wisecracker For This Useful Post:
sekfarok (01-10-2017)
Sponsored Links
Closed

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 10:32 AM.