LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-19-2008, 11:55 AM   #1
gawain
Member
 
Registered: Dec 2006
Location: Italy -Rome
Distribution: Slackware 11.0
Posts: 55

Rep: Reputation: 15
grep and digits


Hi everybody. The problem should be easy to overcome, but as a matter of fact, I'm still failing.

I'd like to match a fixed sequence of numbers, for instance how can I match the "12" without matching all the sequence of numbers?

text text 12 text text

text text 123 text text

text text 1245 text text

I tried like this: egrep "([\b0-9\b]{2})" myfile.txt

([\b0-9\b]{2})
([\b0-9\b]{2}?)
\b[0-9]{2}\b
\b\d{2}\b

but none of them works. But shouldn't the quantifier {2} limit the sequence to two numbers?

Thanks for any hint
 
Old 02-19-2008, 12:01 PM   #2
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
What output are you trying get? How should it be formatted?
 
Old 02-19-2008, 12:14 PM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
grep -w 12 myfile.txt
 
Old 02-19-2008, 12:14 PM   #4
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Your second to last version should actually work (if what you want to do is match any two-digit number):
Code:
rw@rodion:~/t$ egrep '\b[0-9]{2}\b' myfile.txt
text text 12 text text

Quote:
Originally Posted by gawain View Post
But shouldn't the quantifier {2} limit the sequence to two numbers?
Yes and no. While it will match two digits, additional digits won't keep it from matching the first two it found.
 
Old 02-19-2008, 12:31 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
egrep '12$|12[^0-9]' filename

Matches any occurrence of "12" at the end of a line, or not followed by a digit.
 
Old 02-19-2008, 02:32 PM   #6
gawain
Member
 
Registered: Dec 2006
Location: Italy -Rome
Distribution: Slackware 11.0
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks for answering.
What I want to match is the sequence of two numbers: in the example I wrote that's 12, but it could be 34, ... The problem is that the regexes I wrote match every sequence of two numbers, even those which are made up of four numbers

rupertwh at the moment I cannot have a shell ... at disposal: maybe my regex didn't work because I used double quotation marks instead of the 'single ones'?

Thanks again for the help
 
Old 02-19-2008, 03:34 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
egrep -o '\b[0-9]{2}\b' num

matches any combo of 2 numbers within a word boundary. The -o option makes it return only the matched patterns, rather than the whole line.

If the 2-digit pattern can be embedded in other characters, then you need something similar to what I showed earlier.

eg:
[^0-9][0-9]{2}[^0-9] (extended regex syntax)
matches any two digits between non-digits. Does not work if the two digits are at beginning or end of the line.
 
Old 02-20-2008, 03:13 AM   #8
gawain
Member
 
Registered: Dec 2006
Location: Italy -Rome
Distribution: Slackware 11.0
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks a lot, Pixellany
 
  


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
my cd contents file names not displaying morethan 8 digits. AMajeed AIX 5 08-08-2007 10:56 AM
regular expressions in perl-get amount of matching digits baddah Programming 3 06-27-2007 10:18 PM
How to format a long double number in C++ to display as many digits as possible. TruongAn Programming 4 11-09-2005 11:11 AM
input single integer value, output the value as separate digits hubabuba Programming 2 03-02-2005 11:42 AM
Adding username containing only Digits yrraja Linux - General 3 06-25-2003 11:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:33 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration