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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can i add two numbers kripssmart UNIX for Dummies Questions & Answers 4 01-06-2009 06:45 PM
seperating records with numbers from a set of numbers Shiv@jad UNIX for Dummies Questions & Answers 15 11-25-2008 12:04 PM
need top 3 numbers shary Shell Programming and Scripting 4 03-25-2008 01:58 AM
ksh and hex numbers JamesByars Shell Programming and Scripting 2 01-15-2008 03:36 PM
How to add numbers? pnxi Shell Programming and Scripting 7 09-11-2003 07:25 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 03-31-2009
vijay_0209 vijay_0209 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 52
How get numbers only?

I have a file with the following contents..say

123 abc 90and /[abd] 1009

from which i only need numbers to be printed.
like 123 90 1009

using any shell command.

Thanks.
  #2 (permalink)  
Old 03-31-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,972

Code:
$ perl -ne 'print $1," " while /(\d+)/g; print "\n"'

  #3 (permalink)  
Old 03-31-2009
vijay_0209 vijay_0209 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 52
Thats nice Pludi. But I'd prefer that to be done in shell itself
not possible with shell command,awk or sed?
  #4 (permalink)  
Old 03-31-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,972
Possible: yes. Shorter? Probably not. And the Perl command integrates into a pipe or other shell sequence just as well as awk or sed.
  #5 (permalink)  
Old 03-31-2009
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 Here is another approach to the matter


Code:
> echo "123 abc 90and /[abd] 1009"                                          
123 abc 90and /[abd] 1009

> echo "123 abc 90and /[abd] 1009" | tr -d "[:alpha:]|[:punct:]"
123  90  1009

> echo "123 abc 90and /[abd] 1009" | tr -d "[:alpha:]|[:punct:]" | tr -s " "
123 90 1009

  #6 (permalink)  
Old 03-31-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,945
No need to invoke an external program such as awk, sed or perl!

Code:
#!/usr/bin/ksh93

str="123 abc 90and /[abd] 1009"
echo "$str"

print ${str//[^0-9]/ }

Output:

Code:
$ ./demo
123 abc 90and /[abd] 1009
123 90 1009
$

  #7 (permalink)  
Old 03-31-2009
Srinivas_Hari Srinivas_Hari is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 13
All,

What if i have a huge file with nxn matrix of rows and columns and still i would like to have only numbers from every row ? I mean where no. of rows and columns are unpredictable. Any suggestions for that please.

rgds,

Srini
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 08:35 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