Find the answer to your Linux question:
Results 1 to 7 of 7
Hi all, Im having a problem with my regular expression to determine IP addresses in a file. At the mo, im using grep '[0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9]' which returns ip addresses fine, however ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    4

    grep regular expression

    Hi all,
    Im having a problem with my regular expression to determine IP addresses in a file. At the mo, im using

    grep '[0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9]'

    which returns ip addresses fine, however its also returning ip addresses which have something attached on the end (or before), such as:

    127.0.0.1.random

    i tried using grep -w, and also

    grep '\<[0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9]\>'

    but they dont seem to work, can anybody help?
    Cheers
    Mootroot

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    Grep will return the entire string that contains the search terms. Are the returned products the strings contained in the source data? Also you seem to be sarching for only to the subnet stage of the IP address - 127.0.0 for example.

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Specify that nothing can be before or after the string with the anchors '^' - beginning of line and '$' - end of line. Your string should look like:

    grep '^[0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9]$'

    You can reduce your string down to this and it'll work:

    grep '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$'

    Vic

  4. #4
    Just Joined!
    Join Date
    Jun 2007
    Posts
    4
    thanks for your replies.

    i didnt spot that id only put 3 octets!

    I dont think i can use the beginning of line and end of line thing, cos the IP address will be somewhere in the middle. What i have is a file with a load of lines, where a line could be like one of the following:

    hello a 10.10.10.10
    test b 192.168.2.1.test.test

    but i only want to return the first line not the one with test.test. Then i will use awk to obtain just the ip.

    What i would really like is an expression where i could grep for each line with an IP address (which is by itself) no matter where it is in the line

    Thanks again

  5. #5
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    OK, then I'll assume there's spaces and/or TABs delimiting the IP address. I'll also take into account that the IP address may be either at the beginning or end of the line. This command should work:

    grep -E '(^|[[:space:]])[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*([[:space:]]|$)'

    The -E option means to use extended regular expressions. This is needed to search to beginning of line or whitespace ''(^|[[:space:]])" or whitespace or end-of-line "([[:space:]]|$)".

    Vic

  6. #6
    Just Joined!
    Join Date
    Jun 2007
    Posts
    4
    thanks mate, that seems to have done the job

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    with GNU awk,
    Code:
    awk --re-interval '{
            for(i=1;i<=NF;i++) {             
                 if ( $i ~ /^([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}$/ ) {
                    print $0
                 }
            }
          }' "file"

Posting Permissions

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