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

Re: quick question

Expand Messages
  • Chris Allen
    ... The substitute command. For example: %s/^22.*$/my replacement text/ That would replace anywhere in the whole file (%) a string which started (^) with 22
    Message 1 of 17 , Dec 1, 2005
      On 01/12/05, Chris Hudson <chudson@...> wrote:
      > How would I replace every line in a file that starts with certain text, for
      > example every line that begins with 22.

      The 'substitute' command. For example:

      %s/^22.*$/my replacement text/

      That would replace anywhere in the whole file (%) a string which
      started (^) with 22 (22) and then continued on with anything (or even
      nothing) (.*) to the end of the line ($). See also :help :s

      You can also delete any line starting with a certain string using the
      :global command, if you decide you'd rather do that. Something like:

      :g/^22/ d

      HTH,
      Chris Allen
    • Gareth Oakes
      ... -G
      Message 2 of 17 , Dec 1, 2005
        To do a regex search/replace:

        :%s/^22.*/replacement

        To do some command with each line:

        :g/^22/some-ex-command


        -G

        Chris Hudson wrote:
        > How would I replace every line in a file that starts with certain text, for
        > example every line that begins with 22.
        >
        > Thanks,
        > Chris
        >
      • Bob Hiestand
        Message 3 of 17 , Dec 1, 2005
          On 12/1/05, Chris Hudson <chudson@...> wrote:
          > How would I replace every line in a file that starts with certain text, for
          > example every line that begins with 22.
          >
          > Thanks,
          > Chris
          >

          :g/^22/s/.*/replacement
        • A. S. Budden
          ... To read this, find all the files that have a start of line (^) immediately followed by two 2 s and delete them (d) ... Al
          Message 4 of 17 , Dec 1, 2005
            On 01/12/05, Chris Hudson <chudson@...> wrote:
            > Sorry, I should have said 'delete' instead of replace. So I guess using your
            > example I could just replace the line with nothing, like this :$s/^22.*//g

            A better way of doing that is to use:

            :g/^22/d

            To read this, find all the files that have a start of line (^)
            immediately followed by two '2's and delete them (d)

            :help :g

            Al
          • Wolfgang Schmidt
            ... if e.g your file lookss as follows ===== LINESTART this is the rest of the first line LINESTART this is the rest of another line etc. ... will: -replace
            Message 5 of 17 , Dec 1, 2005
              Chris Hudson schrieb:

              >How would I replace every line in a file that starts with certain text, for
              >example every line that begins with 22.
              >
              >Thanks,
              >Chris
              >
              >
              if e.g your file lookss as follows

              =====
              LINESTART this is the rest of the first line
              LINESTART this is the rest of another line
              etc.
              =====

              :%s/^LINESTART.*/replacement/gc

              will:

              -replace every line starting with "LINESTART" with the text
              "replacement" and prompt for

              In detail:

              :%

              means "work on all lines of the file"

              s/...//gc

              means "search and replace globally (g) with confirmation (c)"

              ^LINESTART

              means "match every occurence of string LINESTART at the beginning of a
              line (^)"

              .*

              means "followed by any character (.) 0 or any times (*)"

              Cheers,

              Wolfgang
            • Frans Grotepass
              ... Hi Chris and others, This is one of the first decent answers I ve seen on this list. I ve been using (g)Vim now for a number of years sporadically.
              Message 6 of 17 , Dec 1, 2005
                On Thursday 01 December 2005 17:30, Chris Allen wrote:
                > On 01/12/05, Chris Hudson <chudson@...> wrote:
                > > How would I replace every line in a file that starts with certain text,
                > > for example every line that begins with 22.
                >
                > The 'substitute' command. For example:
                >
                > %s/^22.*$/my replacement text/
                >
                > That would replace anywhere in the whole file (%) a string which
                > started (^) with 22 (22) and then continued on with anything (or even
                > nothing) (.*) to the end of the line ($). See also :help :s
                >
                > You can also delete any line starting with a certain string using the
                >
                > :global command, if you decide you'd rather do that. Something like:
                > :g/^22/ d
                >
                > HTH,
                > Chris Allen


                Hi Chris and others,

                This is one of the first decent answers I've seen on this list. I've been
                using (g)Vim now for a number of years sporadically. Recently I've changed
                jobs and I now have a development job in embedded linux, so I am using Vim
                more often now. The last few months I've made a concerted effort to improve
                my Vim skills. Using the Vim Regular expressions is obviously one of the
                powerful tools in the search/replace functions. They are explained in the
                help.

                This response is however a good example of how one can learn via example. Most
                of the answers on questions on this list is simply a post of the commandline
                and it is assumed that the person asking the question knows what the symbols
                mean in the search replace. Your response does however show exactly what each
                expression does and makes it easier for the reader helped by your response to
                achieve fluency in regular expressions faster.

                Best regards,

                Frans Grotepass
              Your message has been successfully submitted and would be delivered to recipients shortly.