LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-23-2010, 05:15 AM   #1
somupl86
LQ Newbie
 
Registered: Nov 2010
Posts: 13

Rep: Reputation: 0
Regular expression to Grep for a n digit number


Hi,

I just want to grep for a n digit number followed by M alphabet.

Generally for a three digit number i can give

grep [0-9][0-9][0-9]M , but if the digits are increasing it is tough to represent them.

Any help in this regard.

Thanks in Advance,

Soma Sekhar Kalla
 
Old 11-23-2010, 05:30 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Look at "grep -E" (nee egrep) - you can specify a specific number, or repetition. I also prefer classes, as in [[:digit:]] - doesn't really matter here, but may in other classes/uses.
See the manpage.
 
Old 11-23-2010, 05:58 AM   #3
somupl86
LQ Newbie
 
Registered: Nov 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Actually my question is not with grep, i need a regular expression that mataches 9 digit number followed by a letter M.

Thanks,
Soma
 
Old 11-23-2010, 06:17 AM   #4
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Code:
[0-9]{9}M
 
Old 11-23-2010, 06:39 AM   #5
somupl86
LQ Newbie
 
Registered: Nov 2010
Posts: 13

Original Poster
Rep: Reputation: 0
No that is not working.
 
Old 11-23-2010, 06:58 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
What about:

Code:
ls | grep -E '\<[0-9]{9}M\>'

Last edited by sycamorex; 11-23-2010 at 06:59 AM.
 
Old 11-24-2010, 06:03 AM   #7
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
You have to use egrep and surround with quotes.

Code:
$ echo "123M" | egrep '[0-9]{3}M'
123M
 
Old 11-24-2010, 06:11 AM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by devnull10 View Post
You have to use egrep and surround with quotes.

Code:
$ echo "123M" | egrep '[0-9]{3}M'
123M
The problem with it is that it will also match:
Code:
$ ls | egrep '[0-9]{3}M'
23456M
333M
3453454524554M
3456M
4444M
456M
55555M
666666M
777777M
945454555555M
999999999M
I think this is what the OP is after:
Code:
$ ls | egrep '\<[0-9]{3}M\>'
333M
456M
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to use regular expression in grep? sagarkha Linux - Newbie 6 01-23-2010 08:15 AM
grep regular expression casperdaghost Linux - Newbie 2 08-15-2009 11:01 AM
grep Regular Expression lamar_air Programming 2 07-24-2006 03:40 PM
Help with grep regular expression lamar_air Programming 3 07-21-2006 11:26 AM
Regular Expression + not Working with grep scottwmackey Linux - General 2 06-21-2006 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:11 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration