Loading ...
Sorry, an error occurred while loading the content.

Delete lines not starting by an IP

Expand Messages
  • Steve
    Hi, I have a text file with more than 300K lines in it which I would like to clean. Each line is of the form: IP mm/dd/yy hh:mm with IP being an standard IP
    Message 1 of 5 , Sep 15, 2013
    • 0 Attachment
      Hi,

      I have a text file with more than 300K lines in it which I would like
      to clean. Each line is of the form:

      IP mm/dd/yy hh:mm

      with IP being an standard IP address. This file is being populated by a
      script which basically gets the IP and writes it at the end of the file.
      But sometimes, this fails and I get a line with only the date and time.
      My goal is to rid of these lines.

      I tried something like:

      :g!/^([0-9]{1,3}.)3.([0-9]{1,3})/d

      but it erases the whole file. I know that this regexp is not perfect
      (it will also catch 999.999.999.999, which isn't a valid IP address),
      but that doesn't seem to be the problem here.

      What's wrong?

      Thanks for any tips.

      Best,
      steve


      --
      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Nikolay Pavlov
      ... In vim this catches opening parenthesis at the start of the line followed by a digit followed by an opening figure brace, 1, comma, 3, closing figure
      Message 2 of 5 , Sep 15, 2013
      • 0 Attachment


        On Sep 15, 2013 4:23 PM, "Steve" <dlist@...> wrote:
        >
        >   Hi,
        >
        >   I have a text file with more than 300K lines in it which I would like
        > to clean. Each line is of the form:
        >
        > IP mm/dd/yy hh:mm
        >
        > with IP being an standard IP address. This file is being populated by a
        > script which basically gets the IP and writes it at the end of the file.
        > But sometimes, this fails and I get a line with only the date and time.
        > My goal is to rid of these lines.
        >
        >   I tried something like:
        >
        >   :g!/^([0-9]{1,3}.)3.([0-9]{1,3})/d
        >
        >   but it erases the whole file. I know that this regexp is not perfect
        > (it will also catch 999.999.999.999, which isn't a valid IP address),
        > but that doesn't seem to be the problem here.

        In vim this catches opening parenthesis at the start of the line followed by a digit followed by an opening figure brace, 1, comma, 3, closing figure brace, any character, closing parenthesis, 3, any character, opening parenthesis, digit, opening figure brace, 1, comma, 3, closing figure brace, closing parenthesis. Obviously you do not have such lines in a file.

        Note that it is not a regex for IPv4 address in any regex engine I know about: e.g. (...)3 is capturing group followed by 3 in PCRE, dot represents any character, not only the dot itself, in all of PCRE, BRE, ERE, vim.

        Check out :h regex for the correct syntax.

        >   What's wrong?
        >
        >   Thanks for any tips.
        >
        >   Best,
        >   steve
        >
        >
        > --
        > --
        > You received this message from the "vim_use" maillist.
        > Do not top-post! Type your reply below the text you are replying to.
        > For more information, visit http://www.vim.org/maillist.php
        >
        > ---
        > You received this message because you are subscribed to the Google Groups "vim_use" group.
        > To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        > For more options, visit https://groups.google.com/groups/opt_out.

        --
        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
         
        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • Tim Chase
        ... As long as you re okay with the 999.999.999.999 being considered kosher, and skipping IPv6 addresses, you should be able to use ... that would be ... ^
        Message 3 of 5 , Sep 15, 2013
        • 0 Attachment
          On 2013-09-15 14:23, Steve wrote:
          > IP mm/dd/yy hh:mm
          >
          > with IP being an standard IP address. This file is being populated
          > by a script which basically gets the IP and writes it at the end of
          > the file. But sometimes, this fails and I get a line with only the
          > date and time. My goal is to rid of these lines.
          >
          > I tried something like:
          >
          > :g!/^([0-9]{1,3}.)3.([0-9]{1,3})/d
          >
          > but it erases the whole file. I know that this regexp is not
          > perfect (it will also catch 999.999.999.999, which isn't a valid IP
          > address), but that doesn't seem to be the problem here.

          As long as you're okay with the 999.999.999.999 being considered
          kosher, and skipping IPv6 addresses, you should be able to use

          :v/^\%(\d\{1,3\}\.\)\{3\}\d\{1,3\}/d

          that would be

          :v/ On every line that doesn't match the following
          ^ at the start-of-line
          \%( a non-capturing group
          \d\{1,3\} 1-3 digits
          \. followed by a literal period
          \) the close of the group
          \{3\} that group 3x (the first 3 quads)
          \d\{1,3\} another set of 1-3 digits (the last quad)
          /d delete them

          -tim




          --
          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Steve
          ... Works perfectly, thanks! ... And thanks for the explanation. Did you format this by hand or do you have a special plugin that formats the break-up? It s
          Message 4 of 5 , Sep 15, 2013
          • 0 Attachment
            Le 15-09-2013, à 08:14:07 -0500, Tim Chase a écrit :

            > On 2013-09-15 14:23, Steve wrote:
            > > IP mm/dd/yy hh:mm
            > >
            > > with IP being an standard IP address. This file is being populated
            > > by a script which basically gets the IP and writes it at the end of
            > > the file. But sometimes, this fails and I get a line with only the
            > > date and time. My goal is to rid of these lines.
            > >
            > > I tried something like:
            > >
            > > :g!/^([0-9]{1,3}.)3.([0-9]{1,3})/d
            > >
            > > but it erases the whole file. I know that this regexp is not
            > > perfect (it will also catch 999.999.999.999, which isn't a valid IP
            > > address), but that doesn't seem to be the problem here.
            >
            > As long as you're okay with the 999.999.999.999 being considered
            > kosher, and skipping IPv6 addresses, you should be able to use
            >
            > :v/^\%(\d\{1,3\}\.\)\{3\}\d\{1,3\}/d

            Works perfectly, thanks!

            > that would be
            >
            > :v/ On every line that doesn't match the following
            > ^ at the start-of-line
            > \%( a non-capturing group
            > \d\{1,3\} 1-3 digits
            > \. followed by a literal period
            > \) the close of the group
            > \{3\} that group 3x (the first 3 quads)
            > \d\{1,3\} another set of 1-3 digits (the last quad)
            > /d delete them

            And thanks for the explanation. Did you format this by hand or do you
            have a special plugin that formats the break-up? It's awesome!

            Steve

            --
            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Tim Chase
            ... That was done by hand, but there is Vimsplain[1] which should automatically produce similar output. -tim [1] https://github.com/pafcu/Vimsplain -- -- You
            Message 5 of 5 , Sep 15, 2013
            • 0 Attachment
              On 2013-09-15 15:22, Steve wrote:
              > Le 15-09-2013, à 08:14:07 -0500, Tim Chase a écrit :
              > > that would be
              > >
              > > :v/ On every line that doesn't match the following
              > > ^ at the start-of-line
              > > \%( a non-capturing group
              > > \d\{1,3\} 1-3 digits
              > > \. followed by a literal period
              > > \) the close of the group
              > > \{3\} that group 3x (the first 3 quads)
              > > \d\{1,3\} another set of 1-3 digits (the last quad)
              > > /d delete them
              >
              > And thanks for the explanation. Did you format this by hand or do
              > you have a special plugin that formats the break-up? It's awesome!

              That was done by hand, but there is Vimsplain[1] which should
              automatically produce similar output.

              -tim

              [1] https://github.com/pafcu/Vimsplain

              --
              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              ---
              You received this message because you are subscribed to the Google Groups "vim_use" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            Your message has been successfully submitted and would be delivered to recipients shortly.