The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to sort a string with numbers ahjiefreak Shell Programming and Scripting 5 12-21-2007 06:52 AM
Extract numbers from a string and store in variables davewg Shell Programming and Scripting 6 11-14-2007 01:22 AM
How do i get numbers from a string? eliraza6 Shell Programming and Scripting 13 07-18-2007 04:04 AM
perl: string comparison as numbers bishweshwar UNIX for Advanced & Expert Users 7 05-26-2007 09:54 PM
output only numbers from mixed string nortypig Shell Programming and Scripting 10 08-29-2006 07:30 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-15-2007
Registered User
 

Join Date: Nov 2006
Posts: 51
to find numbers in a string

I writing my script and got stuck in this function. Can someone help me?
I need to extract out the numbers inside a string.
Ex:
INPUT -> OUTPUT
abcdef123 -> 123
abc123def -> 123
123abcdef -> 123
a123bc45d -> 123 45
abcdefghi -> -1

Thank you!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-15-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
sed "s/[^0-9]//g;s/^$/-1/;" filename
Reply With Quote
  #3 (permalink)  
Old 05-15-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,449
one way
Code:
echo "salkf4243lsfk" | tr -d '[[:alpha:]]' #or [a-z]
or in bash
Code:
#s="slfk32432sdf"
#echo ${s//[a-zA-Z]/} # or [!0-9]
Reply With Quote
  #4 (permalink)  
Old 05-15-2007
Registered User
 

Join Date: Nov 2006
Posts: 51
Thanks all!
It works.
But how can we separate each numbers?
Anyway it's fine to me!
Reply With Quote
  #5 (permalink)  
Old 05-15-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by fongthai
Thanks all!
It works.
But how can we separate each numbers?
Anyway it's fine to me!
Code:
sed "s/[^0-9]*\([0-9][0-9]*\)[^0-9]*/ \1 /g;s/^[^0-9][^0-9]*$/-1/" filename
Reply With Quote
  #6 (permalink)  
Old 05-16-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,449
Code:
awk '{ gsub(/[[:alpha:]]|[[:punct:]]/," ")}1' file
Reply With Quote
  #7 (permalink)  
Old 11-22-2007
Registered User
 

Join Date: Nov 2007
Posts: 4
Hi I have a similar problem, I would like to get a number that falls within a range from a string.

ex. input: "abcd 9034 efg 1234 hi"
output: 9034

I would only want to get the number range from 9000-9999

I've tried to use sed/grep to get what I want for hours but not able to do it, can anyone advice on this? Thanks a lot!!!
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0