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
grep numbers mcgrailm UNIX for Dummies Questions & Answers 5 07-25-2008 01:07 AM
How to sort a string with numbers ahjiefreak Shell Programming and Scripting 5 12-21-2007 10:52 AM
How do i get numbers from a string? eliraza6 Shell Programming and Scripting 13 07-18-2007 07:04 AM
grep for non numbers shihabvk Shell Programming and Scripting 6 09-20-2005 09:06 PM
grep numbers range azmathshaikh Shell Programming and Scripting 1 08-08-2005 07:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-07-2008
ajilesh ajilesh is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 16
Sed to grep only numbers in string

Hi,

I would like to get only number in the following strings.

var1="Type20"
var2="type 3"
var3="value 2"
var4="Type 1 Datacenter Hall 2"

I would like to extract output as 20 from var1 and 3 from var2,2 from var3 and 1 from var4.

Appreciate any one help asap..

Regards,
Aji
  #2 (permalink)  
Old 11-07-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink What about this solution?

Code:
> echo "Type 20" | tr -d " " | tr -s "[:alpha:]" "~" | cut -d "~" -f2
20

> echo "Type 1 Datecenter Hall 2" | tr -d " " | tr -s "[:alpha:]" "~" | cut -d "~" -f2
1
  #3 (permalink)  
Old 11-07-2008
chatwizrd chatwizrd is offline
Banned
  
 

Join Date: Nov 2008
Posts: 67
Nice use of the worm character :P
  #4 (permalink)  
Old 11-07-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
$ cat file
var1="Type20"
var2="type 3"
var3="value 2"
var4="Type 1 Datacenter Hall 2"
$ sed 's [^"]*[^0-9]*\([0-9]*\).* \1 ' file
20
3
2
1
  #5 (permalink)  
Old 11-07-2008
ajilesh ajilesh is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 16
Better than everyone..!!

I have found out the solution my self and works fine..thanks for my self ..:P

nimbld1:root:/home/users/thayata/cvs/test # var="Type 3"
nimbld1:root:/home/users/thayata/cvs/test # print $var |tr -d '[A-Za-z]' |sed 's/ //g'
3
nimbld1:root:/home/users/thayata/cvs/test #
  #6 (permalink)  
Old 11-07-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,421
Quote:
Originally Posted by ajilesh View Post
I have found out the solution my self and works fine..thanks for my self ..:P
What about var4 ? Your solution will return 12
Take radoulov solution.

Last edited by danmero; 11-07-2008 at 01:48 PM..
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 05:30 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