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

Re: quick question

Expand Messages
  • Pete Johns
    ... See ... Hope this helps; --paj -- Pete Johns Tel/Fax numbers and IM information:
    Message 1 of 17 , Dec 1, 2005
    • 0 Attachment
      On Thu, 2005-12-01 at 10:01:35 -0600, Chris Hudson sent:
      >How would I replace every line in a file that starts with
      >certain text, for example every line that begins with 22.
      >
      :s/^22.*$/Some Text/

      See

      :help :subst

      Hope this helps;


      --paj
      --
      Pete Johns <http://johnsy.com/>
      Tel/Fax numbers and IM information: <http://johnsy.com/contact/>
      Thank You For Th...<http://johnsy.com/blog/?entry=20051130191827>
    • Tim Chase
      ... Should be in the tutorial or in the FAQ, but the short answer would be ... If you want lines that begin with 22 , but not 222 , then you can use ... or
      Message 2 of 17 , Dec 1, 2005
      • 0 Attachment
        > How would I replace every line in a file that starts with
        > certain text, for example every line that begins with 22.

        Should be in the tutorial or in the FAQ, but the short answer
        would be

        :%s/^22.*/certain text

        If you want lines that begin with "22", but not "222", then you
        can use

        :%s/^22\>.*/certain text

        or

        :%s/^22[^2].*/certain text

        If you want to replace arbitrary text only on lines beginning
        with "22", you can use

        :g/^22/s/text to change/certain text

        more info can be found at

        :help :s
        :help ordinary-atom
        :help :g

        -tim
      • 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 3 of 17 , Dec 1, 2005
        • 0 Attachment
          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
        • Alan G Isaac
          ... hth, Alan Isaac
          Message 4 of 17 , Dec 1, 2005
          • 0 Attachment
            On Thu, 1 Dec 2005, Chris Hudson apparently wrote:
            > How would I replace every line in a file that starts with certain text, for
            > example every line that begins with 22.

            :g/^22/s/.*/newtext

            hth,
            Alan Isaac
          • Gareth Oakes
            ... -G
            Message 5 of 17 , Dec 1, 2005
            • 0 Attachment
              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 6 of 17 , Dec 1, 2005
              • 0 Attachment
                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 7 of 17 , Dec 1, 2005
                • 0 Attachment
                  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 8 of 17 , Dec 1, 2005
                  • 0 Attachment
                    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 9 of 17 , Dec 1, 2005
                    • 0 Attachment
                      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.