Results 1 to 4 of 4

Thread: grep numbers

  1. #1
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question grep numbers

    Hi All,
    Im reading the book Grep Handbook published from Oriely. There is an example that says something like to find all odd numbers using [13579].
    Is that really the best way to do it? I was thinking that the only position we care about is at the end of a string of numbers, but the \> regex only applies to words. Am I over thinking this? Is there a better way to search for odd numbers with a regular expression?
    ~Conradin~

  2. #2
    Join Date
    Jan 2008
    Location
    Lausanne, Switzerland
    Beans
    341
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: grep numbers

    I would try something like: \d+[13579]\b
    \d+ indicates zero or more digits
    [13579] means one of these digits must be present before \b which defines a word boundary

    It will return '125' but also 'k125'

    If you don't want the second type, try \b\d+[13579]\b

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: grep numbers

    \d+ indicates zero or more digits
    1 or more
    0 or more belongs to *

  4. #4
    Join Date
    Jan 2008
    Location
    Lausanne, Switzerland
    Beans
    341
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: grep numbers

    Thank you for spotting my mistake Vaphell! I should have added that I didn't thoroughly test the expression and it should be done before use!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •