Extracting numbers from a String


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extracting numbers from a String
# 1  
Old 05-13-2010
Extracting numbers from a String

Hi all,

I'm a new programmer to shell script... and I have no idea how to use substring.

I want to extract the numbers from the following string and place it into a variable:
"170 unique conformations found"

The numbers can be more than three digits depending on the case. I just want to ensure that I can isolate ALL the numbers in this string for every case (no matter how many digits there are). In other words, the programming should not depend on the number of digits of the number.

Thanks! Much appreciated by me and our laboratory at University of Richmond!!
# 2  
Old 05-13-2010
assuming no other types of charachters then in your example

Code:
tr -d [:alpha:] < filename

# 3  
Old 05-13-2010
Hi I'm new to unix scripting myself. I learned how to do this the other day, here goes:

Assuming you have assigned the string to a variable:

Code:
x="170 unique conformations found"

You can get the numerical part and assign it to the variable y with:

Code:
y=${x%%unique conformations found}

Test:

Code:
echo $y

I hope this helps
# 4  
Old 05-13-2010
This is somewhat like a Perl equivalent to denn's approach:
Code:
echo "170 unique conformations found" | perl -ane 'foreach my $rec (@F) {print "$rec\n" if "$rec" !~ m/[A-Za-z]/;}'

To put the output in a variable:
Code:
var=$(echo "170 unique conformations found" | perl -ane 'foreach my $rec (@F) {print "$rec\n" if "$rec" !~ m/[A-Za-z]/;}')

# 5  
Old 05-13-2010
Code:
var=$( echo "170 unique conformations found" sed "s/[^0-9]//g")

# 6  
Old 05-13-2010
Quote:
Originally Posted by anbu23
Code:
var=$( echo "170 unique conformations found" sed "s/[^0-9]//g")

I think you're missing a '|'.
# 7  
Old 05-13-2010
Thanks everyone! I ended up using the perl solution!

However I have one more question! Sorry...

I have one string that has two numbers i want to isolate SEPARATELY:

"Global minimum E = 391.86 found 10 times."

I want to isolate the "391.86" number preceding the "found" word, and store that into a variable.
I also want to isolate the "10" number preceding the "times" word, and store that into a separate variable.

If you guys could help me out again, it would help a great deal!!!

Thank so much!

-Alex
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed extracting numbers

I have number 192.168.21.8. I want to extract from this number with sed 21 and 8 to variables a and b. Any Ideas? I did like 's/\(192.168.\)/ /' but its wrong :( (6 Replies)
Discussion started by: Natalie
6 Replies

2. Shell Programming and Scripting

Extracting numbers

Hi I am part of a academic organization and I want to send a fax to the students however there must be a quicker way to get the fax numbers extracted from the online forms they sent me. The file looks like this (numbers are fake in order to protect identity): Biochemistry Major Michael... (3 Replies)
Discussion started by: phil_heath
3 Replies

3. Shell Programming and Scripting

Help extracting single instance of numbers which repeat

Hi, the title isn't very descriptive but it'll be easier to explain what I need if I write out the coordinates from which I need to extract certain information: ATOM 2521 C MAM X 61 44.622 49.357 12.584 1.00 0.00 C ATOM 2522 H MAM X 61 43.644 49.102 12.205 ... (10 Replies)
Discussion started by: crunchgargoyle
10 Replies

4. Shell Programming and Scripting

extracting non-zero pairs of numbers from each row

Hi all, I do have a tab delimited file a1 a2 b1 b2 c1 c2 d1 d2 e1 e2 f1 f2 0 0 123 546 0 0 0 0 0 0 0 0 0 0 345 456 765 890 902 1003 0 0 0 0 534 768 0 0 0 0 0 0 0 0 0 0 0 0 0 0 456 765 0 0 0 0 0 0 0 0 0 0 0 0 12 102 0 0 0 0 456 578 789 1003 678 765 345 400 801 1003 134 765... (5 Replies)
Discussion started by: Lucky Ali
5 Replies

5. Shell Programming and Scripting

[Solved] Help on extracting the numbers out of top command

Hi guys, Any easy way to generate a CSV file that contains only the numbers out of the following lines? load averages: 15.09, 12.89, 11.76 03:39:22 999 processes: 854 sleeping, 2 running, 122 zombie, 5 stopped, 16 on cpu Memory: 32G real, 17G free, 18G swap in use, 15G swap free ... (6 Replies)
Discussion started by: ejianu
6 Replies

6. Shell Programming and Scripting

Extracting formatted text and numbers

Hello, I have a file of text and numbers from which I want to extract certain fields and write it to a new file. I would use awk but unfortunately the input data isn't always formatted into the correct columns. I am using tcsh. For example, given the following data I want to extract: and... (3 Replies)
Discussion started by: DFr0st
3 Replies

7. Shell Programming and Scripting

Extracting numbers from a string

Hello Everyone, i have quick question. I have file names like: bin_map300.asc and I would like to extract grid300. My approach so far: name=bin_map300.asc echo ${name%%.*} echo ${name##*_} I am stuck combining the two. Any help would be appreciated. (3 Replies)
Discussion started by: creamcheese
3 Replies

8. UNIX for Dummies Questions & Answers

Extracting numbers and multipling

Hi All, I have searched the forum but couldn't find exactly what I need. Hopefully someone may be able to help. I'm trying to put a script together that will extract numbers from a text file and multiply them by, for example 1.5 or 1.2 Sample file looks like this...... (1 Reply)
Discussion started by: speedfreak
1 Replies

9. Shell Programming and Scripting

Error extracting 1 or more numbers with expr command

Need help with the following, I want to extract the digits from the following file pattern using the expr command. digits are in the range 1-99 Tried two different methods, not sure what I am doing wrong. file1=file1.dbf file10=file10.dbf Works for expr "$file10" : '.*\(\)' 10 ... (2 Replies)
Discussion started by: fire!
2 Replies

10. Shell Programming and Scripting

extracting numbers from strings

Hello all, I am being dumb with this and I know there is a simple solution. I have a file with the follwing lines bc stuff (more)...............123 bc stuffagain (moretoo)............0 bc stuffyetagain (morehere)......34 failed L3 thing..............1 failed this... (2 Replies)
Discussion started by: gobi
2 Replies
Login or Register to Ask a Question