Converting String To Integer/Float (weird case)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting String To Integer/Float (weird case)
# 8  
Old 01-10-2017
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 01-10-2017
Quote:
Originally Posted by RavinderSingh13
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
# 10  
Old 01-10-2017
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 01-10-2017
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..
This User Gave Thanks to vgersh99 For This Post:
# 12  
Old 01-10-2017
Quote:
Originally Posted by RavinderSingh13
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.Smilie
# 13  
Old 01-10-2017
Try
Code:
awk '{X = substr ($1,7,9); sub (/^0+/, "", X); print X}' file
234
-123
234
234
-11234
234
234

This User Gave Thanks to RudiC For This Post:
# 14  
Old 01-10-2017
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.
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting string to integer

I have a function that is supposed to check for user processes and wait for 0 count before exiting the function. I am sure I have more than one issue in my code, but the stumbling block right now is that I am trying to convert the value of my variable from a string to integer. process_count... (10 Replies)
Discussion started by: MIA651
10 Replies

2. Shell Programming and Scripting

Converting date +%j into integer

Dear community, i got a problem to get "date +%j" as the right value. Today is the 10th day of the year. #./script.sh 2 #!/bin/bash/ Var1=$(date +%j) Var2=$1 let result=$Var1+$Var2 echo $Var1 plus $Var2 equals $result The output of the script is: 010 plus 2 equals 10... (9 Replies)
Discussion started by: OskarHF
9 Replies

3. Shell Programming and Scripting

Converting Month into Integer

okay so i run an openssl command to get the date of an expired script. Doing so gives me this: enddate=Jun 26 23:59:59 2012 GMT Then i cut everything out and just leave the month which is "Jun" Now the next part of my script is to tell the user if the the certificate is expired or not... (6 Replies)
Discussion started by: shade917
6 Replies

4. Shell Programming and Scripting

converting to lower case or upper case

here is a code column_name="vivek" column_name2="ViVeK" column_name=$(echo $column_name | awk '{print tolower($0)}') column_name2=$(echo $column_name2 | awk '{print tolower($0)}') echo "column name 1 lower: $column_name" echo "column name... (6 Replies)
Discussion started by: vivek d r
6 Replies

5. UNIX for Advanced & Expert Users

byte swapping 32-bit float and weird od results

I'm attempting to read a file that is composed of complex 32-bit floating point values on Solaris 10 that came from a 64-bit Red Hat computer. When I first tried reading the file, it looked like there was a byte-swapping problem and after running the od command on the file Solaris and Red Hat... (2 Replies)
Discussion started by: GoDonkeys
2 Replies

6. Shell Programming and Scripting

Converting integer to String

Hi everyone, I would like to know how to convert an integer to a string. for instance if i=1 i would like to creat a variable called constant1. i want to do this in a for loop so for each value of i, i create a new variable such as constant2, constant3,... and so on. for i in 1 2 3 do ... (1 Reply)
Discussion started by: ROOZ
1 Replies

7. UNIX for Dummies Questions & Answers

Converting a String variable into Integer

Hi, I am passing a variable to a unix function. However when I try to assign the value to another variable like typeset -i I_CACHE_VAL=$2 Is this because of String to Integer conversion? I get an error. Please help me with thsi. Thanks (2 Replies)
Discussion started by: neeto
2 Replies

8. Shell Programming and Scripting

Converting the case of the string?

Hi i have a string value in a variable STR eg. STR="ABSDSCSFS" How can be convert it to LOWERCASE in the script? Thanks (5 Replies)
Discussion started by: skyineyes
5 Replies

9. Shell Programming and Scripting

command for converting string to integer

Hi ... I am trying to calculate the time needed for a command to execute.. but the resulting value is getting as string.. so i am not able to use "expr " command.. please help me to convert the value to integer so that i can proceed with my script.. Regards esham (1 Reply)
Discussion started by: esham
1 Replies

10. UNIX for Dummies Questions & Answers

Integer/Float Script Problem

Hi, I have a script which takes a value from a file and performs calculations on it. Trouble is that this value is a float not an integer and it errors at the decimal point! eg. 94.62 I would like to be able to detect the length of the float (in this above case, 5 characters), and simply do a... (2 Replies)
Discussion started by: danhodges99
2 Replies
Login or Register to Ask a Question