Copy a block from a file into another file

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Copy a block from a file into another file

Yogesh Sharma-2

> Hi,
> I m working on a huge file. The file is in format
>
> START HEADER_1
> ----.
> ----
> ----
> END
> START HEADER_2
> ----.
> ----
> ----
> END
> START HEADER_3
> ----.
> ----
> ----
> END
>
> Suppose i want to copy section starting from
> START HEADER2 to corresponding END in to another file
>
> I have tried somecode, which works if I run these commands manually,
> Now how to put them into script as i have to run these commands again
> & again
>
> ?^START (Searches backward for START, Assuming I m inside HEADER_2)
> V (Selection)
> /END (Search corresponding END)
> y (Yank)
> e! newfile (open newfile)
> p (paste)
>
> I tried to put these commands in a file & sourcing it but its not working
>
> Regards
> Yogesh
>
>==================================
>  
>
Reply | Threaded
Open this post in threaded view
|

RE: Copy a block from a file into another file

vishnu-3
Hi Yogesh,

Use the following in your script file. You should differentiate the
normal and command line commands in your script file.


"Script starts
normal ?^START<CR>
normal V
normal /END<CR>
normal y
e! newfile
normal p
"Script ends


~VIshnu

-----Original Message-----
From: Yogesh Sharma [mailto:[hidden email]]
Sent: Wednesday, July 06, 2005 3:26 PM
To: [hidden email]
Subject: Copy a block from a file into another file


> Hi,
> I m working on a huge file. The file is in format
>
> START HEADER_1
> ----.
> ----
> ----
> END
> START HEADER_2
> ----.
> ----
> ----
> END
> START HEADER_3
> ----.
> ----
> ----
> END
>
> Suppose i want to copy section starting from
> START HEADER2 to corresponding END in to another file
>
> I have tried somecode, which works if I run these commands manually,
> Now how to put them into script as i have to run these commands again
> & again
>
> ?^START (Searches backward for START, Assuming I m inside HEADER_2)
> V (Selection)
> /END (Search corresponding END)
> y (Yank)
> e! newfile (open newfile)
> p (paste)
>
> I tried to put these commands in a file & sourcing it but its not
working
>
> Regards
> Yogesh
>
>==================================
>  
>

Reply | Threaded
Open this post in threaded view
|

Re: Copy a block from a file into another file

Tim Chase-2
In reply to this post by Yogesh Sharma-2
> START HEADER_1
> ----.
> ----
> ----
> END
> START HEADER_2
> ----.
> ----
> ----
> END
>
> Suppose i want to copy section starting from
> START HEADER2 to corresponding END in to another file

While you can use Vishnu's solution of using a script, if you
want each header to be the name of the file and want to do the
whole file in one pass, you may be able to do something like

g/^START/exec ".,/^END/w ".substitute(getline("."),'START\s*','','')

This will write from "START HEADER_n"  through the associated
"END" line (presuming neither "START" nor "END" appear validly at
the beginning of any line in between them)

You can tweak the substitute() function call if there are several
pieces in the HEADER_n and you want to extract one of them as the
file name.

Additionally, if you don't want the header & footer lines in the
actual resulting file, you can adjust the range of the form

g/^START/exec "+,/^END/-w ".substitute(getline("."),'START\s*','','')

which will start writing the range from the line *after* "START"
(the "+") through the line before ("-") the line containing "END".

Alternatively, if you want to have manual control over the file
names, you could do something like

nnoremap <f4> :+?^BEGIN?,/^END/w

(where there's a trailing space after that last "w") which will
allow you to press <f4> from within the desired range.  This will
pre-populate the command-line with a range and a write command
that will write the desired range to the filename you then
specify by hand.

Just a few ideas.

HTH,

-tim



Reply | Threaded
Open this post in threaded view
|

Re: Copy a block from a file into another file

Yogesh Sharma-2
Hello Tim,Vishnu,

Thanx for all the help, it so easy with Vim.

Regards
Yogesh


==================================
Yogesh Sharma

5F267,Extn 2606
Texas Instrument
Bangalore, INDIA
E - [hidden email]
O - +91 80 2504 8606
M -                
==================================



Tim Chase wrote:

>> START HEADER_1
>> ----.
>> ----
>> ----
>> END
>> START HEADER_2
>> ----.
>> ----
>> ----
>> END
>>
>> Suppose i want to copy section starting from
>> START HEADER2 to corresponding END in to another file
>
>
> While you can use Vishnu's solution of using a script, if you want
> each header to be the name of the file and want to do the whole file
> in one pass, you may be able to do something like
>
> g/^START/exec ".,/^END/w ".substitute(getline("."),'START\s*','','')
>
> This will write from "START HEADER_n"  through the associated "END"
> line (presuming neither "START" nor "END" appear validly at the
> beginning of any line in between them)
>
> You can tweak the substitute() function call if there are several
> pieces in the HEADER_n and you want to extract one of them as the file
> name.
>
> Additionally, if you don't want the header & footer lines in the
> actual resulting file, you can adjust the range of the form
>
> g/^START/exec "+,/^END/-w ".substitute(getline("."),'START\s*','','')
>
> which will start writing the range from the line *after* "START" (the
> "+") through the line before ("-") the line containing "END".
>
> Alternatively, if you want to have manual control over the file names,
> you could do something like
>
> nnoremap <f4> :+?^BEGIN?,/^END/w
>
> (where there's a trailing space after that last "w") which will allow
> you to press <f4> from within the desired range.  This will
> pre-populate the command-line with a range and a write command that
> will write the desired range to the filename you then specify by hand.
>
> Just a few ideas.
>
> HTH,
>
> -tim
>
>
>
>
>