script to test a file.

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default script to test a file.

    Hi,

    I want to write a script that will test the contents of a file.

    The file being tested will pass only if it contains nothing more than an
    ip address on one line. Does anyone have a sample of a simple regex to
    accomplish this?

    Thanks
    Rick

    Rick Bragg Guest

  2. Similar Questions and Discussions

    1. can anybody test this file? (PC MXa)
      http://www.hot.ee/allvee/Abloy_s2.FH11 is the file i was working on last night. when trying to open it today i´ve got an ´could not draw the doc...´...
    2. Test a zip file for validity
      Hello all, How do I go about checking if a file is a zip file? Wrapping the zipinfo utility in php did not help since it does not return an...
    3. Bitmaps are displaced when opening FH9 file-I need a test file
      Would someone who has reported opening FH 9/10 files in FH MX containing bitmaps that were being displaced please send me a file that displays this...
    4. Test for file existence
      "Brian Berneker" <brianberneker@hotmail.com> wrote in message news:bf5e3v$v7j$1@news1.mountaincable.net... Good. What does it say on page 2 of...
    5. Best way to test for existence of a file
      Hello, I was wondering what is the most bullet proof way to test for a file. I am concerned, that a program could hang when trying to test for...
  3. #2

    Default Re: script to test a file.

    Rick Bragg wrote:
    > Hi,
    >
    > I want to write a script that will test the contents of a file.
    >
    > The file being tested will pass only if it contains nothing more than an
    > ip address on one line. Does anyone have a sample of a simple regex to
    > accomplish this?
    /\d+\.\d+\.\d+\.\d+\n?/s

    --
    Andrew Gaffney

    Andrew Gaffney Guest

  4. #3

    Default Re: script to test a file.


    > Rick Bragg wrote:
    > > Hi,
    > >
    > > I want to write a script that will test the contents of a file.
    > >
    > > The file being tested will pass only if it contains nothing more
    than an
    > > ip address on one line. Does anyone have a sample of a simple regex to
    > > accomplish this?
    >
    > /\d+\.\d+\.\d+\.\d+\n?/s
    >
    Beware of insufficient regexes in regex clothing. The above will
    certainly find one or more digits followed by a dot, followed by one or
    more digits followed by a dot, etc. But it does *NOT* match an IP
    address...

    /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/

    Is at least closer, but it is still not sufficient, as IP addresses are
    bounded, I believe, at 255 so 336.47.894.0, will match the above, but is
    not a valid IP address.

    How strict do you need to be, how sure are you about the data in the files?

    [url]http://danconia.org[/url]
    Wiggins D Anconia Guest

  5. #4

    Default Re: script to test a file.


    On Wednesday, Oct 29, 2003, at 19:43 Europe/Brussels, Wiggins d Anconia
    wrote:
    >
    >
    >> Rick Bragg wrote:
    >>> Hi,
    >>>
    >>> I want to write a script that will test the contents of a file.
    >>>
    >>> The file being tested will pass only if it contains nothing more
    > than an
    >>> ip address on one line. Does anyone have a sample of a simple regex
    >>> to
    >>> accomplish this?
    >>
    >> /\d+\.\d+\.\d+\.\d+\n?/s
    >>
    >
    > Beware of insufficient regexes in regex clothing. The above will
    > certainly find one or more digits followed by a dot, followed by one or
    > more digits followed by a dot, etc. But it does *NOT* match an IP
    > address...
    >
    > /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
    >
    > Is at least closer, but it is still not sufficient, as IP addresses are
    > bounded, I believe, at 255 so 336.47.894.0, will match the above, but
    > is
    > not a valid IP address.
    >
    > How strict do you need to be, how sure are you about the data in the
    > files?
    >
    Doing it with plain regexp won't work in one go, you could put the
    above between (),(),() and check with $1,$2,$3 etc that you don't have
    an 894..

    However, you could also use Regexp::Common


    Jerry

    Jerry Rocteur Guest

  6. #5

    Default Re: script to test a file.


    At 11:43 AM 10/29/2003 -0600, Andrew Gaffney wrote:
    >Rick Bragg wrote:
    >>Hi,
    >>I want to write a script that will test the contents of a file.
    >>The file being tested will pass only if it contains nothing more than an
    >>ip address on one line. Does anyone have a sample of a simple regex to
    >>accomplish this?
    >
    >/\d+\.\d+\.\d+\.\d+\n?/s
    Slightly more complex and slightly more robust:

    cat <<EOF | perl -lne "print if /^(\d{1,3}\.){3}\d{1,3}$/"
    hello
    1.2.3.4.5
    1.2.3
    1.2.3.4
    1.2.3.4 hello
    hello 1.2.3.4
    123.123.123.123
    1234.123.123.123
    EOF

    Regards,
    - Robert

    Robert Citek Guest

  7. #6

    Default Re: script to test a file.

    And the clouds parted, and Wiggins d Anconia said...
    >
    >
    > > Rick Bragg wrote:
    > > > Hi,
    > > >
    > > > I want to write a script that will test the contents of a file.
    > > >
    > > > The file being tested will pass only if it contains nothing more
    > than an
    > > > ip address on one line. Does anyone have a sample of a simple regex to
    > > > accomplish this?
    > >
    > > /\d+\.\d+\.\d+\.\d+\n?/s
    > >
    >
    > Beware of insufficient regexes in regex clothing. The above will
    > certainly find one or more digits followed by a dot, followed by one or
    > more digits followed by a dot, etc. But it does *NOT* match an IP
    > address...
    >
    > /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
    >
    > Is at least closer, but it is still not sufficient, as IP addresses are
    > bounded, I believe, at 255 so 336.47.894.0, will match the above, but is
    > not a valid IP address.
    >
    > How strict do you need to be, how sure are you about the data in the files?
    >
    Good point.

    At it's most basic, to match any valid IPv4 address alone on a line, I would
    use
    /\A((1?\d{1,2}|2([0-4]\d|5[0-5]))\.){3}(1?\d{1,2}|2([0-4]\d|5[0-5]))\z/

    Which will match a string containing only a dotted quad from 0.0.0.0 to
    255.255.255.255, inclusive. Note that I'm assuming the string in
    question contains the _entire_ contents of the file.

    A couple of things to note, though:
    * A valid IP is actually any 32 bit integer from 0 to 4294967295 (try
    putting 3639555427 in Mozilla (or supposedly any other well-behaved
    browser... in other words, _not_ IE)... it will take you to Google
    because that number is actually 216.239.53.99 - the address for
    [url]www.google.com[/url] - in base-10).
    * This pattern doesn't exclude RFC1918 reserved addresses (10/8,
    172.16/12, and 192.168/16), or multicasts.
    * It doesn't take netmasks into account. 1.2.3.255/16 is a valid
    host address, while 1.2.3.255/24 is not (it's a broadcast).

    Taking these sorts of things into account could make for a bit hairier
    solution. ;) Caveat user.



    /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~\
    | Brian Gerard I'm writing an unauthorized autobiography. |
    | First initial + 'lists' |
    | at technobrat dot com |
    \_________________________________________________ _____________________/
    Brian Gerard Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139