Getting number from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting number from a string
# 1  
Old 01-06-2014
Getting number from a string

I have a string c12 and want to get the number 12

In general instead of 12 I can have any number. I have to capture the number as I need to do some computations on it.

I am using a bash script
# 2  
Old 01-06-2014
Try:
Code:
number=`echo $string | tr -d 'a-zA-Z'`

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 01-06-2014
Using parameter expansion:
Code:
bash-4.2$ v=c12
bash-4.2$ echo "${v//[!0-9]}"
12

# 4  
Old 01-06-2014
Longhand using cygwin...
Code:
AMIGA:~> var="c12fg"
AMIGA:~> echo "${var:1:2}"
12
AMIGA:~> _

# 5  
Old 01-06-2014
Some more

Code:
$ echo "c12" | tr -cd '[[:digit:]]' 
12

$ echo "c12" | awk 'gsub(/[[:alpha:]]/,x)' 
12

# 6  
Old 01-06-2014
Code:
result=`echo $string | egrep -o "[0,9]{1,5}"`


Last edited by bartus11; 01-06-2014 at 10:44 AM.. Reason: Please use code tags.
# 7  
Old 01-06-2014
if you have Ruby
Code:
# number=$(echo "c12" | ruby -e "puts gets.scan(/\d+$/)")
# echo $number
12

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

3. Shell Programming and Scripting

string to number problem

Hi actually what happen i have taken a value from database table and stored in variable and that value is 20100601 000000 but this value is stored as string value in database table so after storing this value in variable a when i did operation on this a variable like a=`expr ${a} +1` i m... (1 Reply)
Discussion started by: aishsimplesweet
1 Replies

4. Shell Programming and Scripting

Python String <--> Number

My question is so simple: A = raw_input("A ") if A == '56': VAR = (A + 54)/13 else: print "other operations" if I write in input 5656565656 i want to make some arithmetic operations if the first input is 56XXX but the output is TypeError: cannot concatenate 'str' and... (2 Replies)
Discussion started by: kazikamuntu
2 Replies

5. Shell Programming and Scripting

number of characters in a string

Hi there, I have some user input in a variable called $VAR, and i need to ensure that the string is 5 or less characters .... does anybody know how i can count the characters in the variables ? any help would be great, cheers (2 Replies)
Discussion started by: rethink
2 Replies

6. UNIX for Dummies Questions & Answers

Number of lines containing a certain string

Hey I want to know if there is an option to know the number of lines containing a certain string (bit for example) in a file? Say I want to know number of lines containing only the string BIT in file xyz. I know how to get number of lines in a file by using wc -l but how do you get number of lines... (1 Reply)
Discussion started by: #moveon
1 Replies

7. Shell Programming and Scripting

extracting a number from a string

Hi everyone, I have a string as follow ts1n65ulpa4096x16m16_130a_ss1p08v125c i would like to extract 4096 and 16 from string and save it into two variable. but this string could also have the form ts1n65ulpa32x16m16_130a_ss1p08v125c therefore the number before "x" could be 2 or 3 digits. i use... (6 Replies)
Discussion started by: ROOZ
6 Replies

8. Shell Programming and Scripting

$A is a number / any other string? How to determine ?

I have a variable (say $A) and while passing it gets either a number or some other string. Now how can test (with if else) whether the variable is just a ne or something else ? Thanks a lot to all in advance C Saha (2 Replies)
Discussion started by: csaha
2 Replies

9. Shell Programming and Scripting

string to number

In the following script when I get SIZE1 and SIZE2, they are of type string. I can not do any mathematical operations on them. I wanted to find out if I can convert string in to Number. Thanks in Advance. FILE1='/export/registry/incoming/reg.dmp' FILE2='/export/registry/reg.dmp' SIZE1=`ls... (2 Replies)
Discussion started by: George_king
2 Replies

10. Shell Programming and Scripting

shell to number and to string

Hello Does the unix korn shell provide a function to convert between number and string data-types regards Hrishy (1 Reply)
Discussion started by: xiamin
1 Replies
Login or Register to Ask a Question