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
How to sort a string with numbers ahjiefreak Shell Programming and Scripting 5 12-21-2007 10:52 AM
Extract numbers from a string and store in variables davewg Shell Programming and Scripting 6 11-14-2007 05:22 AM
How do i get numbers from a string? eliraza6 Shell Programming and Scripting 13 07-18-2007 07:04 AM
perl: string comparison as numbers bishweshwar UNIX for Advanced & Expert Users 7 05-27-2007 12:54 AM
output only numbers from mixed string nortypig Shell Programming and Scripting 10 08-29-2006 10:30 PM

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 Rate Thread Display Modes
  #1 (permalink)  
Old 05-16-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
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!
  #2 (permalink)  
Old 05-16-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed "s/[^0-9]//g;s/^$/-1/;" filename
  #3 (permalink)  
Old 05-16-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
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]
  #4 (permalink)  
Old 05-16-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
Thanks all!
It works.
But how can we separate each numbers?
Anyway it's fine to me!
  #5 (permalink)  
Old 05-16-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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
  #6 (permalink)  
Old 05-16-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Code:
awk '{ gsub(/[[:alpha:]]|[[:punct:]]/," ")}1' file
  #7 (permalink)  
Old 11-22-2007
fayya fayya is offline
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!!!
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:46 PM.


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