On 30/01/09 08:27, Spencer Collyer wrote:
> Hi,
>
> The problem I am trying to solve is as follows:
>
> I have a file consisting of a load of filenames, one per line. These files
> need to be moved into sequentially ordered subdirectories, so I want to edit
> the file so it goes from:
>
> ----------
> aaa.txt
> bbb.txt
> ccc.txt
> ----------
>
> to
>
> ----------
> mv aaa.txt sub001
> mv bbb.txt sub002
> mv ccc.txt sub003
> ----------
>
> What I originally tried was something like the following on the VIM command
> line:
>
> ----------
> :1,$s/.*/\=printf("mv& sub%03s", line("."))
> ----------
>
> but then I discovered that '&' does not get expanded to the search string in
> functions. Checking the help confirms this fact.
>
> What I've resorted to doing for the moment is using the following substitute
> to get the subdirectories on the end:
>
> ----------
> :1,$s/$/\=printf(" sub%03s", line("."))
> ----------
>
> Followed by the following to get the 'mv' commands at the beginning:
>
> ----------
> :1,$s/^/mv /
> ----------
>
> I'm currently thinking about writing a function to do the replacements, but
> is there a simpler way of doing this in VIM?
>
> Spencer Collyer
(The following is untested.)
If you know that the necessary subdirectories exist:
:%s/^.*$/\='mv ' . submatch(0) . ' sub' . printf('%03d',line('.'))/
If you are not sure that the directories exist:
:%s/^.*$/\='mkdir -p sub' . printf('%03d',line('.')) .
\ ' && mv ' . submatch(0) . ' sub' . printf('%03d',line('.'))/
In both cases we assume that there are no more than 999 lines.
Best regards,
Tony.
--
Die, v.:
To stop sinning suddenly.
-- Elbert Hubbard
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---