Converting String To Integer/Float (weird case)

Hi guys,

I'm new here. I have a problem at work. One of our scripts was eventually having a bug and only detected recently. Here's the issue and background:

Bash Script which calls AWK script

Awk script returns a string as per below (example):

var1='00000-123'

So, when we convert it, the value becomes 0 and it messes up with our grand total of that var1.

Any idea how can I convert above string into -123?

you probably need to fix your awk, but YMMV:

echo '00000-123'| bc

Hello sekfarok,

Could you please try following.

echo $var1 | awk '{sub(/^0+/,"");printf("%d\n",$0)}'
OR
echo $var1 |  bc

Thanks,
R. Singh

Wonder where shall i put it in my code. basically my awk script looks like below:

BEGIN {
some codes here
}
#so it search for some Trailer with T as indicator
/^T/{
       var1=substr($0,100,9);  <-- this part grep the '00000-123'
       print var1;
}
END

:confused:

Hello sekfarok,

Could you please try changing var1=substr($0,100,9); to var1=substr($0,106,4); , off course not tested it as lack of complete details and sample Input_file.

Thanks,
R. Singh

can you show a sample file/output that this awk is trying to parse?

ahh sorry.

Currently (ignore the position). Let say i have a few records with 15 character long (fixed length).

Input

T00001000000234
T0000200000-123     <-- this should become -123
T00003000000234
T00004000000234
T00005000-11234     <-- this should become -11234
T00006000000234
T00007000000234

the var1 should be something like: var1=substr($0,7,9)

and record 2 and 5 are having '-' sign but in different position. Hope this is enough sample.

Hello sekfarok,

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

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

Thanks,
R. Singh

1 Like

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

Hello sekfarok,

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

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

Thanks,
R. Singh

a bit convoluted, but doesn't rely on the fixed-width and/or hardwired column width values, but:

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.

1 Like

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.:b:

Try

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

OSX 10.12.2, default bash terminal followed by dash, longhand.

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~> _
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.

1 Like

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:

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

Thank you so much. tested your solutions and it works. all my calculations are correct too now :):b:

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

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

AMIGA:amiga~> _

Try echo ${x#${x%%[1-9-]*}} , then.

3 Likes

Unfortunately, the code I suggested in post #15 only works if all zeros in the string are leading zeros. :o Fortunately, RudiC posted the fix that does what I was trying to do in post #18. :cool:

Thanks RudiC! :b: