extract numbers from a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract numbers from a word
# 1  
Old 03-20-2006
extract numbers from a word

Hi ppl,

I am a bit lost on this...can some one assist. I know this can be down with awk or sed, but i cant get the exact syntax right.

I need to only extract the numbers from a signle word ( eg abcd.123.xyz )

How can i extract 123 only ?

Thanks
# 2  
Old 03-20-2006
Hi,
i am assuming u have a file containing the strings and each string is uniform as below:

ccc.nnn.ccc #where c is alphabet, n is number

Code:
nawk -F. '{print $2}' filename
or
$number = `echo line | nawk -F. '{print $2}' ` #not sure if this will work
echo $number

# 3  
Old 03-20-2006
Try...
Code:
echo "abcd.123.xyz" | tr -dc '[0-9]'

# 4  
Old 03-20-2006
Hi Yogr,

Thank you so much, That worked Smilie Thank u so much.
# 5  
Old 03-20-2006
But there is a problem Smilie


What if the file name is "ab42.1.2.3.tar.gz"

I only need to extract "1.2.3", but as of now it is reading "42.123" any help on that?

Thanks
# 6  
Old 03-20-2006
try this,

Code:
echo ab42.1.2.3.tar.gz | sed -e 's/^[a-z]*[0-9]*.//;s/.tar.gz//'

# 7  
Old 03-20-2006
that worked like a gem. Thank u so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Substr with % - extract numbers only

# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.5 (Maipo) I have this script that will monitor filesystems and send me e-amil alerts. #! /bin/ksh DIST_LIST=monitor@...com WORKDIR=/home/monitor WARNLEVEL=90 MAIL_SUBJ="filesystems monitor on "$(hostname) ... (3 Replies)
Discussion started by: danielshell
3 Replies

2. Shell Programming and Scripting

Extract numbers from file name-how to ?

Hello friends,I am new to Unix programming. how do I achieve the following in Unix shell script (I am running ksh on AIX) extract the number from name of file? My file format is like "LongFileName-1234.020614-221030.txt" now I want to extract value which is between (-) hyphen and (.) dot... (4 Replies)
Discussion started by: f150
4 Replies

3. Shell Programming and Scripting

Put numbers after word

Hello I have an file with this content -------------------------------------------- timer one timer two timer three timer four timer five timer six timer seven ------------------------------------------- And I want the following output.... (4 Replies)
Discussion started by: thailand
4 Replies

4. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

5. Shell Programming and Scripting

Summing numbers after specific word

Hi all, Looking for suggestions on a better way to sum numbers in a key value pair formated file. What I have works but seems really clunky to me. Any suggestions would be greatly appreciated. cat test.txt | perl -ne 'm/(M=)(\d+\.?\d?\d?)/ && print "$2\n"' | awk '{ sum+=$1} END {printf... (7 Replies)
Discussion started by: cgol
7 Replies

6. UNIX for Dummies Questions & Answers

Extract numbers from .txt file

I need to extract all the p-value numbers and the rho numbers from a .txt file and write them as coma separated values in a new file. Ideally I would get two files in the end, one for p- values and one for rho. Any suggestions? I appreciate your help!!! The .txt file looks essentially like this... (5 Replies)
Discussion started by: eggali
5 Replies

7. Shell Programming and Scripting

find the last word with all numbers?

Hi, I was trying to extract the last word with all numbers using awk. like the below example. I am looking for a pattern using awk. desired result: (13 Replies)
Discussion started by: hitmansilentass
13 Replies

8. Shell Programming and Scripting

awk fetch numbers after the word

Hi, I would want to fetch all the numbers after a word the number of characters could very. how can I do that? below is the example of the data and the expected output sample data 03 xxxx occurs 1090 times. 04 aslkja occurs 10 times. I would want to fetch 10 & 1090 separately. (13 Replies)
Discussion started by: ahmedwaseem2000
13 Replies

9. Shell Programming and Scripting

BASH. Need to extract some numbers and take the average

Hey all, I ran some simulations, of which the output is 100s of files. I've used grep to extract the vital information needed from the files. This has made my task somewhat easier. But I still need to perform some mathematical calculations (average and geometrical average) on the results of the... (9 Replies)
Discussion started by: slackjack
9 Replies

10. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies
Login or Register to Ask a Question