The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extract same word from two files tjmannonline AIX 4 04-06-2008 12:42 PM
perl newbie: how to extract an unknown word from a string wolwy_pete Shell Programming and Scripting 3 03-23-2008 10:41 AM
Extract numbers from a string and store in variables davewg Shell Programming and Scripting 6 11-14-2007 05:22 AM
grep or awk problem, unable to extract numbers baghera Shell Programming and Scripting 7 08-31-2007 04:42 PM
extract last word on line to new file michieka UNIX for Dummies Questions & Answers 10 05-07-2002 11:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 2.00 average. Display Modes
  #1 (permalink)  
Old 03-20-2006
systemali systemali is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 47
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 (permalink)  
Old 03-20-2006
new2ss new2ss is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 133
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 (permalink)  
Old 03-20-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try...
Code:
echo "abcd.123.xyz" | tr -dc '[0-9]'
  #4 (permalink)  
Old 03-20-2006
systemali systemali is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 47
Hi Yogr,

Thank you so much, That worked Thank u so much.
  #5 (permalink)  
Old 03-20-2006
systemali systemali is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 47
But there is a problem


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 (permalink)  
Old 03-20-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
try this,

Code:
echo ab42.1.2.3.tar.gz | sed -e 's/^[a-z]*[0-9]*.//;s/.tar.gz//'
  #7 (permalink)  
Old 03-20-2006
systemali systemali is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 47
that worked like a gem. Thank u so much.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0