LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-17-2008, 05:19 PM   #1
khong1010
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Rep: Reputation: 0
HELP!!! how to use grep to extract a number


Hello,

Could anyone help me to extract a number from following line?

eth1 getRSSI:-90

Thanks
 
Old 04-17-2008, 05:36 PM   #2
ophirg
Member
 
Registered: Jan 2008
Location: Israel
Distribution: Kubuntu 13.10
Posts: 132

Rep: Reputation: 32
i assume u want the last number:

grep -o "[0-9]*$"
 
Old 04-17-2008, 05:38 PM   #3
khong1010
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Original Poster
Rep: Reputation: 0
I just want -90
 
Old 04-17-2008, 06:52 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
Code:
egrep -o '(-)?[0-9]+$'
 
Old 04-17-2008, 06:53 PM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
I'm not sure grep can do this unless by itself. Is this format always going to be the same? If so, can you use sed? Something like this will do it:

Code:
sed s/.*:// <filename>
-twantrd
 
Old 04-17-2008, 07:02 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Code:
# s="eth1 getRSSI:-90"
# set -- $s
# echo $2
-90
# unset IFS
 
Old 04-18-2008, 12:01 AM   #7
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
A few others..
Code:
s="eth1 getRSSI:-90"
echo "${s##*:}"
Code:
expr "eth1 getRSSI:-90" : '.*:\(.*\)'
Code:
cut -d: -f2 <<<"eth1 getRSSI:-90"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract a number from a complicated string in python horacioemilio Programming 2 12-25-2007 09:09 PM
start grep at line number secretlydead Linux - General 2 09-20-2007 06:03 AM
Best/Right way to extract disk serial number? itnaa Linux - Hardware 4 08-21-2007 11:13 AM
php question - how to extract line number ?? antony_csf Programming 5 09-15-2004 08:08 PM
"how do I extract a number from a text file using shell command?" sdandeker Linux - Networking 3 02-12-2004 08:54 AM


All times are GMT -5. The time now is 10:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration