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

Removing blank lines from a string

Expand Messages
  • David Fishburn
    Vim 6.2.180 WinXP I am trying to remove blanks lines at the beginning and the end of a string. So assuming you have this file: ******* Two Four ******* If you
    Message 1 of 10 , Jan 31, 2004
      Vim 6.2.180 WinXP

      I am trying to remove blanks lines at the beginning and the end of a string.

      So assuming you have this file:
      *******

      Two

      Four

      *******

      If you yank the entire file (5 lines) in the @a register, I was doing this:
      echo substitute(@a, "[\n]\s*[\n]", "\n", 'g' )
      The above successfully removes the 3rd line.

      So I tried to extend this to include the start of the string (or a newline)
      to get the first line.
      This didn't work.
      echo substitute(@a, "\(\^\|[\n]\)\s*[\n]", "\n", 'g' )

      I tried to do the same thing for the last line of the string:
      echo substitute(@a, "\(\^\|[\n]\)\s*\([\n]\|$\)", "\n", 'g' )

      None of these successfully deal with multiple blanks lines in a row.
      If I was doing this over a buffer it would be easy.
      But when it is in a variable, I can't quite get the syntax right.

      TIA,
      Dave
    • Suresh Govindachar
      David Fishburn On: Saturday, January 31, 2004 7:34 PM [wants to remove blank lines in a file; attempts made were unnecessarily complex; title of post says
      Message 2 of 10 , Jan 31, 2004
        "David Fishburn" On: Saturday, January 31, 2004 7:34 PM

        [wants to remove blank lines in a file; attempts made
        were unnecessarily complex; title of post says remove
        blank lines in a _string_ because stuff in file was being
        copied to a register before applying substitution function
        to it as to a string]

        Use the command below after replacing N and M by
        actual start and end line numbers in file

        :N,Ms+^\ \{}\n^++gci

        --Suresh
      • David Fishburn
        ... Thanks for the response, but this will not work for me. I am using the substitute command over a variable, so I can t use the above command. Dave
        Message 3 of 10 , Jan 31, 2004
          > From: Suresh Govindachar [mailto:sgovindachar@...]
          >
          > [wants to remove blank lines in a file; attempts made
          > were unnecessarily complex; title of post says remove
          > blank lines in a _string_ because stuff in file was being
          > copied to a register before applying substitution function
          > to it as to a string]
          >
          > Use the command below after replacing N and M by
          > actual start and end line numbers in file
          >
          > :N,Ms+^\ \{}\n^++gci


          Thanks for the response, but this will not work for me.

          I am using the substitute command over a variable, so I can't use the above
          command.

          Dave
        • Suresh Govindachar
          Suresh Govindachar On: Saturday, January 31, 2004 7:54 PM ... Very soon after sending the above post, I realized my mistake. OP does indeed want to process
          Message 4 of 10 , Jan 31, 2004
            "Suresh Govindachar" On: Saturday, January 31, 2004 7:54 PM
            >"David Fishburn" On: Saturday, January 31, 2004 7:34 PM
            >
            >[wants to remove blank lines in a file; attempts made
            >were unnecessarily complex; title of post says remove
            >blank lines in a _string_ because stuff in file was being
            >copied to a register before applying substitution function
            >to it as to a string]
            >
            >Use the command below after replacing N and M by
            >actual start and end line numbers in file
            >
            >:N,Ms+^\ \{}\n^++gci
            >

            Very soon after sending the above post, I realized my
            mistake. OP does indeed want to process a string.

            The following works in the test case submitted in OP.
            The test case involved @a containing the yank of the
            five lines:
            -----------------

            two

            four

            ------------------
            The solution is:

            let @b=substitute(@a, '^\n', "", "g")
            let @c=substitute(@b, '\n\n','\n \n', "g")
            let @d=substitute(@c, ' \n','', "g")

            So, does this work in the original general case that
            prompted creating the test case?

            --Suresh
          • Klaus Bosau
            ... Would this solve your problem? substitute(@a, ^ n | n s* n @= , , g ) Klaus
            Message 5 of 10 , Jan 31, 2004
              On Sat, 31 Jan 2004, David Fishburn wrote:

              >> [...]
              >
              > Thanks for the response, but this will not work for me.
              >
              > I am using the substitute command over a variable, so I can't use the
              > above command.

              Would this solve your problem?

              substitute(@a, '^\n\|\n\s*\n\@=', '', 'g')

              Klaus
            • Suresh Govindachar
              David Fishburn On: Saturday, January 31, 2004 7:34 PM I think I finally understand the OP. The problem is to replace blank lines in a string variable using
              Message 6 of 10 , Jan 31, 2004
                "David Fishburn" On: Saturday, January 31, 2004 7:34 PM

                I think I finally understand the OP. The problem is to
                replace blank lines in a string variable using a SINGLE
                substitution.

                My two cents is that this cannot be done in a single
                substitution because the blank at the head needs to
                be replaced by nothing where as the non-head blanks
                need to be replaced by one "\n". A substitution can
                substitute various things by one thing (which one thing
                could be nothing) -- but a substitution cannot replace
                some things by one thing and some other things by
                another thing. Since the problem calls for two different
                replacement things, a single substitution will not do the job.

                Whether or not you agree with the above reasoning, I
                trust I have finally understood the OP.

                --Suresh
              • Suresh Govindachar
                Klaus Bosau On Saturday, January 31, 2004 8:54 PM ... Looks like my argument is going to take a beating, but in any case, what is the modification to handle
                Message 7 of 10 , Jan 31, 2004
                  "Klaus Bosau" On Saturday, January 31, 2004 8:54 PM

                  >>On Sat, 31 Jan 2004, David Fishburn wrote:
                  >>> [...]
                  >>
                  >> Thanks for the response, but this will not work for me.
                  >>
                  >> I am using the substitute command over a variable, so I can't use the
                  >> above command.
                  >
                  >Would this solve your problem?
                  >
                  > substitute(@a, '^\n\|\n\s*\n\@=', '', 'g')

                  Looks like my argument is going to take a beating, but
                  in any case, what is the modification to handle the case
                  when @a has more than one blank line at the beginning?

                  --Suresh
                • Suresh Govindachar
                  Klaus Bosau On: Saturday, January 31, 2004 8:54 PM ... The following modification handles multiple blank lines at the beginning: substitute(@a,
                  Message 8 of 10 , Jan 31, 2004
                    "Klaus Bosau" On: Saturday, January 31, 2004 8:54 PM

                    >On Sat, 31 Jan 2004, David Fishburn wrote:
                    >
                    >>> [...]
                    >>
                    >> Thanks for the response, but this will not work for me.
                    >>
                    >> I am using the substitute command over a variable, so I can't use the
                    >> above command.
                    >
                    >Would this solve your problem?
                    >
                    > substitute(@a, '^\n\|\n\s*\n\@=', '', 'g')

                    The following modification handles multiple blank lines at the beginning:

                    substitute(@a, '^\s*\n*\s*\n\|\n\s*\n\@=', '', 'g')

                    So my argument was bs.

                    --Suresh
                  • Suresh Govindachar
                    Klaus Bosau On: Saturday, January 31, 2004 8:54 PM ... [My previous solution for handling multiple blank lines at the beginning only handled up to two blank
                    Message 9 of 10 , Jan 31, 2004
                      "Klaus Bosau" On: Saturday, January 31, 2004 8:54 PM
                      >
                      >Would this solve your problem?
                      >
                      > substitute(@a, '^\n\|\n\s*\n\@=', '', 'g')

                      [My previous solution for handling multiple blank lines at the
                      beginning only handled up to two blank lines at the beginning]

                      I think the following is the solution:

                      substitute(@a, '^\(\s*\n\)*\|\n\s*\n\@=', '', 'g')

                      I have tested it with 0 or more blank lines at the beginning,
                      at the end and in between two non-trivial lines; blank lines
                      with and without white space; non-trivial lines starting and
                      ending with and without blank space.

                      --Suresh
                    • David Fishburn
                      ... That is brilliant, thanks. I have run my own tests, and it does exactly what I need. I understand this part to get rid of empty lines at the start of the
                      Message 10 of 10 , Feb 2, 2004
                        > -----Original Message-----
                        > From: Suresh Govindachar [mailto:sgovindachar@...]
                        > I think the following is the solution:
                        >
                        > substitute(@a, '^\(\s*\n\)*\|\n\s*\n\@=', '', 'g')
                        >
                        > I have tested it with 0 or more blank lines at the beginning,
                        > at the end and in between two non-trivial lines; blank lines
                        > with and without white space; non-trivial lines starting and
                        > ending with and without blank space.


                        That is brilliant, thanks.

                        I have run my own tests, and it does exactly what I need.

                        I understand this part to get rid of empty lines at the start of the string.
                        '^\(\s*\n\)*

                        And this part to get blanks lines in the middle of the string:
                        \n\s*\n

                        But I don't understand this part to get rid of the empty lines up to the end
                        of the string:
                        \n\s*\n\@=

                        Ahh, I think I understand. That matches the last \n with zero width, so it
                        does not
                        include it in the pattern. Therefore, a blank substitution can be used.

                        Thanks!
                        Dave
                      Your message has been successfully submitted and would be delivered to recipients shortly.