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

substitude with part of file name

Expand Messages
  • Dirk Zimmermann
    Hello, I have files with names like: track49089.F, track49050.F, track49061.F and so on and there are different suffixes *.F, *.cards and *.job. These files
    Message 1 of 4 , Sep 2, 2002
    • 0 Attachment
      Hello,

      I have files with names like: track49089.F, track49050.F, track49061.F
      and so on and there are different suffixes *.F, *.cards and *.job.
      These files contain lines like:

      OPEN(20,file='track8.log',status='unknown')

      or:

      FILI '/*****/**/****/jul_49088.EPIO | EPIO'

      or:

      rm -f track7 track7.log track7.dat


      What I want to do is the following (may I use the last line as an
      example): This comes from files like "track49050.job" and I want to
      substitude all occurencies of track7 (i.e. track7, track7.log and
      track7.dat) with track49050 and if the file name is "track49061.job" I
      want to substituede with track49061. I would prefer to do this
      automatically, i.e. not to change the substitution line for every new
      file by hand.

      So, the question is how to get a part of the current file/buffer name
      and use this in an substitution command?


      Thanks in advance,
      Dirk
    • Piet Delport
      ... See :h expand() for a starting point. More specifically, if you want the current file s base name (i.e. no path or extension), you can use expand( %:t:h ).
      Message 2 of 4 , Sep 2, 2002
      • 0 Attachment
        On Mon, 02 Sep 2002 at 15:02:03 +0200, Dirk Zimmermann wrote:
        >
        > I have files with names like: track49089.F, track49050.F, track49061.F
        > and so on and there are different suffixes *.F, *.cards and *.job.
        > These files contain lines like:
        >
        > OPEN(20,file='track8.log',status='unknown')
        >
        > or:
        >
        > FILI '/*****/**/****/jul_49088.EPIO | EPIO'
        >
        > or:
        >
        > rm -f track7 track7.log track7.dat
        >
        >
        > What I want to do is the following (may I use the last line as an
        > example): This comes from files like "track49050.job" and I want to
        > substitude all occurencies of track7 (i.e. track7, track7.log and
        > track7.dat) with track49050 and if the file name is "track49061.job" I
        > want to substituede with track49061. I would prefer to do this
        > automatically, i.e. not to change the substitution line for every new
        > file by hand.
        >
        > So, the question is how to get a part of the current file/buffer name
        > and use this in an substitution command?

        See :h expand() for a starting point.

        More specifically, if you want the current file's base name (i.e. no
        path or extension), you can use expand('%:t:h'). To use it as part of a
        substitute, you can combine that with :exec, like so:

        :exec '%s/track7/' . expand('%:t:h') . '/g'

        --
        Piet Delport
        Today's subliminal thought is:
      • Colin Keith
        ... expand( %:r ) will give you the root filename of the current file. That is the filename without the extension. So: x.txt = x That gives you what you
        Message 3 of 4 , Sep 2, 2002
        • 0 Attachment
          On Mon, Sep 02, 2002 at 03:02:03PM +0200, Dirk Zimmermann wrote:
          > substitude all occurencies of track7 (i.e. track7, track7.log and
          > track7.dat) with track49050 and if the file name is "track49061.job" I
          > want to substituede with track49061. I would prefer to do this

          expand('%:r') will give you the 'root' filename of the current file.
          That is the filename without the extension. So:

          x.txt => x

          That gives you what you want to replace your matches with. I.e. track49061
          if the filename is "track49061.job".

          You probably know how to use substitutes but with a normal substitute, you
          can't insert variables or use code. But you can use the notation \= in the
          replace part to indicate you want to use a special replacement:
          (":help sub-replace-special")

          > So, the question is how to get a part of the current file/buffer name
          > and use this in an substitution command?

          To bring it all together in one command for you:

          :%s/track7/\= expand('%:r') /g


          Obviously you might be able to make this more generic with

          track\d\+

          In which case you might be able to fix all of your files at once with:

          vim -c":bufdo %s/track\d\+/\= expand('%:r')/g|wn" *


          Which breaks down into:

          %s/track\d\+/\= expand('%:r') /g
          - Search for all track[0-9] and replace with the filename, as above

          |wn
          - Save and move onto the 'next' file in the argument list

          bufdo
          - Perform the following commands on all files in the buffer list

          vim -c'<cmd>'
          - Execute the following command once vim has loaded.


          Hope that helps - oh and if you're going to try the bufdo, *test* first :)
          There's nothing worse than finding out you typoed and screwed up hundreds
          of live files. (Not that I'd admit to having ever done that :)

          Colin.
        • Dirk Zimmermann
          Hello Colin, Thanks a lot for your help. I think the rest I can do by myself, hopefully. ... This looks very cool! I will try it (with caution) as well! Thank
          Message 4 of 4 , Sep 2, 2002
          • 0 Attachment
            Hello Colin,


            Thanks a lot for your help. I think the rest I can do by myself,
            hopefully.

            * Colin Keith <vim@...> [2002-09-02 16:29]:
            > In which case you might be able to fix all of your files at once with:

            > vim -c":bufdo %s/track\d\+/\= expand('%:r')/g|wn" *

            This looks very cool! I will try it (with caution) as well!


            Thank you (and Piet) very much again,
            Di "Vim rules" rk
          Your message has been successfully submitted and would be delivered to recipients shortly.