0

Below is my code:

#!/usr/bin/ksh
awk 'BEGIN {FS="|"; flag=0;}
{
x=$2
rem=int(x)%62
quo=int(x/62)
flag=0
while(quo>62)
{
sub_rem=int(quo)%62
quo=int(quo/62)
if(flag==0)
{
grp_rem=sub_rem
}
else
{
grp_rem=sub_rem","grp_rem
}
flag++
}
printf "%d|%d|%s,%s,%s\n",$1,$2,quo,grp_rem,rem >> "text.out"
}
end{}
' test.txt

Issue with the above code is:

1) when trying to pass input with more than 18 or more taking a default value

Sample data: 
1|123456789123456789123456789
2|24536789215457896314563

2) After doing base62 operation for certain rows, output is different for others.

sample data: 
row1:  1|276
row2:  1|276
row3:  1|277
row4:  1|278
.
.
.
.
row 300: 300|276


samplet out: 
row1:  1|276|4,0,28
row1:  1|276|4,0,28
row1:  1|277|4,0,27
row1:  1|278|4,0,28
...
row 300: 300|276|4,1,28

Is there anyway to resolve the above issues?

  • I'm afraid it's not obvious what you want to do, what output you expect and what you get instead. If you want people to help make a small test case that clearly shows the problem. – MLu Aug 14 '18 at 5:59
1

You're hitting the awk integer precision.

Given your input file:

$ cat file
1|123456789123456789123456789
2|24536789215457896314563

A simple print is not showing the right result:

$ awk -F\| '{print $2+0}' file
123456789123456791337762816
24536789215457895776256

A way forward is to use option --bignum or -M:

$ awk --bignum -F\| '{print $2+0}' file
123456789123456789123456789
24536789215457896314563

Note that the option --bignum is available from GNU awk version 4.1.0.

  • Thanks @oliv but how to use --bignum in awk can you help me with this – siva10442 Aug 14 '18 at 8:24
  • @siva10442 Just add the option --bignum to your awk command line and run your program again. If you have another problem, please be more specific. – oliv Aug 14 '18 at 8:43
  • I see the awk version 3.1.7 only, is there any other way I can use this program with current version (3.1.7) – siva10442 Aug 14 '18 at 9:40
  • @siva10442 I don't see any easy way. If you can't update the awk to a recent version, then you'd better go for another programming language (perl, python, etc...) – oliv Aug 14 '18 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.