[Solved] How to extract integer out of a string?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] How to extract integer out of a string?
# 1  
Old 06-18-2013
[Solved] How to extract integer out of a string?

Input:

XXX:1,XXX:3,XXX:3

Output:

A=1
B=3
C=3

Below code is what i do currently. However it doesn't work out for all cases. As some times the last digit extracted is giving me error. It cant be used for arithmetic computation.

Any better methods out there? If possible, can you please explain to me why the last digit extracted out cant be used for arithmetic computation? Thanks ! Smilie

Code:
SUBSTRING1=`echo $INPUT| cut -d',' -f 1` 	
SUBSTRING2=`echo $INPUT| cut -d',' -f 2`
SUBSTRING3=`echo $INPUT| cut -d',' -f 3`

SUBSTRING1=${SUBSTRING1##Base:}	   	
SUBSTRING2=${SUBSTRING2##Prev:}
SUBSTRING3=${SUBSTRING3##Post:}

# 2  
Old 06-18-2013
First off: you shouldn't use backticks any more, they are deprecated in every modern shell. But that just as an aside, it shouldn't cause your problem.

The last "number" might be no number at all, as there might be trailing blanks or something such in it. To the shell "everything is a string" and even numbers are strings containing only digits. Your variable expansion statements manipulate strings (by cutting off certain parts), it is just your implicit knowledge of the values involved to justify using these as numbers at all.

I suggest to make sure the variables are indeed what you expect them to be (numbers) by adding some debugging code to your script and output them quoted strictly and surrounded by delimiters to make leading/trailing characters show:

Code:
echo "var_in_question is \"${var_in_question}\"" > some_log

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 06-18-2013
Thanks bakunin!! Smilie
# 4  
Old 06-18-2013
Try
Code:
echo $INPUT | { IFS=":," read ST1 A ST2 B ST3 C; echo $A $B $C $((A+B+C)); }
1 3 3 7

This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-18-2013
Since ST1 ST2 and ST3 are unused you just can scan one variable instead of 3
Code:
... | read x A x B x C

instead of
Code:
... | read x A y B z C

One more way
Code:
$ echo $INPUT
XXX:1,XXX:3,XXX:3
$ set -- $(echo $INPUT | sed 's/\([,]*\)[^,:]*:/\1/g;s/,/ /g')
$ echo $1
1
$ echo $2
3
$ echo $3
3
$

This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 06-18-2013
May i check what if the INPUT is from a text file with many similar occurrence. How do i keep each occurrence as a individual variable?
# 7  
Old 06-18-2013
Does you input comes from a file that is dynamically generated (so that its content changes) ?
or is your input taken from a configuration file whose content does not change ? (configured just once)

Maybe you'd better to just setup an environment file in which you set the wanted variables so that you could load them directly by executing it.
This User Gave Thanks to ctsgnb 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

String to integer

I am on HP-UX using ksh in the script. MaxSal=`sqlplus -silent /nolog <<EOF connect / as sysdba whenever sqlerror exit sql.sqlcode set pagesize 0 feedback off verify off heading off echo off select max(sal) from emp1; select max(sal) from emp2; select max(sal) from emp3; exit; EOF`... (3 Replies)
Discussion started by: bang_dba
3 Replies

3. Shell Programming and Scripting

[Solved] String integer comparison

I am trying to execute something like this file=/tmp/test.txt firstline=$(head -n 1 $file) value=`echo $firstline | cut -d'=' -f2` if then echo true fi i read the first line of a file, cut to the numeric value in the first line and check if it greater than 2 but for some... (11 Replies)
Discussion started by: madhan_dc
11 Replies

4. Shell Programming and Scripting

[Solved] need to convert decimal to integer

Using below command awk 'NR==FNR{A=$1;next} {sum+=($2*A)}END{OFMT="%20f";print int(sum)}' Market.txt Product.txt answer:351770174.00000 how to convert this to 351770174. when i try with below command i am getting different result. awk 'NR==FNR{A=$1;next}... (3 Replies)
Discussion started by: katakamvivek
3 Replies

5. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

6. Programming

[Solved] how to send an integer via message queue?

how can i send an integer via message queue? i try this, but it doesn't work, child process receive 13345943 instead of 5 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/msg.h> #include <sys/ipc.h> #include <sys/sem.h> #include <errno.h> #include <unistd.h>... (2 Replies)
Discussion started by: tafazzi87
2 Replies

7. UNIX for Dummies Questions & Answers

integer to string

Hi all, is there an easy way to convert integer to string in bash? I have numbers like 1, 2, ..., 112, ... and I would like to get 001 002 003 004 ... Thank you, Sarah (4 Replies)
Discussion started by: f_o_555
4 Replies

8. Shell Programming and Scripting

Comparing string and integer in IF

hi, I need to create an IF condition. I read a line from a file and get the 5 word using space as a delimited. This word can have only two values either '*' or '1-5' I need to write an IF condition for two cases. I can either compare it to * or 1-5(or even 1 by cutting and getting only the... (3 Replies)
Discussion started by: kaushys
3 Replies

9. UNIX for Dummies Questions & Answers

conersting string to integer

Hi i am writing a shell script; I need to convert a string to integer so that i can use it in the 'awk' command to choose a field. can you please help me. thank you. (2 Replies)
Discussion started by: abb058
2 Replies

10. Programming

Integer to String

Which function should I use to convert an Integer to a String or Char format ? Thanx (2 Replies)
Discussion started by: psilva
2 Replies
Login or Register to Ask a Question