Avatar of gram77
gram77
Flag for India asked on

Error while assigning a character value to a numeric variable

I have a script that uses a numeric variable resA.

#!/bin/ksh
typeset -i resA=1,233

It has to be an integer type variable, however, it's value assigned is character.

So it gives an error:

$> ksh -x run.ksh
+ typeset -i resA=1,22
run.ksh[2]: 1,22: syntax error

How do I change 1,22 into a number 122 in korn shell?
Unix OSLinux

Avatar of undefined
Last Comment
omarfarid

8/22/2022 - Mon
Paulo Pimenta

Have you tried "1.22" instead of "1,22"? Dot instead of comma.
gram77

ASKER
I grep the value 1,22 from a log file. So it can't be changed.
gram77

ASKER
Can i use sed to remove the comma from 8,454 into 8454 and save it to a file?

This is my requirement in detail:

I have to read a log file for "lines fetched":

4/14/2008 11:15:01 AM: 0067  SQL SELECT "TEAM_CD",
4/14/2008 11:15:01 AM: 0068      "TERR_ID"
4/14/2008 11:15:01 AM: 0069  FROM "OPS$TMS"."DM_TERR"
4/14/2008 11:15:01 AM:             2 fields found: TEAM_CD, TERR_ID, 8,454 lines fetched

I pick 8,454 value, and assign it to a variable resB like this.

resB=`grep '2 fields found: TEAM_CD, TERR_ID,' BCVP_Main_Loader.qvw.log | perl -nle 'print $1 if m/(\d+\,?\d+)\ lines/;'

resB is assigned 8,454.

Then i compare resB with resA

set `sqlplus -s  $opstms_conn_string << EOF
set pages 0
WHENEVER SQLERROR CONTINUE

SELECT count(*)
FROM DM_TERR;
exit
EOF`

resA=$1

now as resA is numeric  8454 and resB is character 8,454 I get an error.

The error is because resB is defined as a numeric variable too.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
gram77

ASKER
I need to compare resA and resB,

if [ $resA -eq $resB ]; then
...

this can be done only when both resA and resB are numeric.
gram77

ASKER
echo 1,234  | sed 's/,//' gives me 1234

gram77

ASKER
This has removed the first comma in the string using sed, how do i remove the second comma.
$>  echo 1,085,712 | sed 's/,//'
1085,712

I want an sed command that would remove all commas from the string.

123,456,789,101,112,131,4
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Tintin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
omarfarid

Why don't you run it on the whole log file:

sed 'sed s/,//g' < logfile > logfile.tmp
mv logile.tmp logfile