I want to manipulate a text-file. It contains many blocks of the form
CLASS
...some stuff ...
END
I want to duplicate each of these blocks and add and remove a line of it's content. Can I script that?
|
I want to manipulate a text-file. It contains many blocks of the form
I want to duplicate each of these blocks and add and remove a line of it's content. Can I script that? |
|||
|
|
|
The natural tools for this are awk and Perl (assuming you want to script: for a once-off, the natural tool is an interactive editor). Here's an awk script that duplicates all
Here's a sed solution; don't take it too seriously. Don't expect it to behave if the
|
|||
|
|
|
Well, you can script anything. However, if I were doing this, I would use A few basic pointers: q -- start recording a macro qx -- stop recording a macro and name it 'x' @x -- run the macro named 'x' A macro will immediately re-run whatever commands you typed during the recording session. So if you wanted a macro to duplicate a block like that type:
There are plenty of good resources and examples out there for vim usages and vim macros. For what it's worth, sed is a sort of a command line version of vim, so you might be able to adapt what I said to sed, but I'm less familiar with it, so I can't vouch for it. ~~ edit ~~ Another thing that makes this more useful is To run a macro on multiple buffers, first open up all the files at the same time. Then, use Note, you must have
Also, see http://stackoverflow.com/questions/1291962/replay-a-vim-macro-until-end-of-buffer for how to run a macro for each line. Mix and match :) |
||||
|
|
|
The exact tool to use will depend on the specifics of the job. If the job is only going to happen once, you might consider using a macro in
Again, the exact way to do this is will depend on your situation, but to add a line after a line starting with PATTERN you can do this:
To add a line before a line starting with PATTERN you could do something like this:
To remove a line starting with PATTERN
The trick will be to find the correct PATTERN that will catch only the lines you want. You can combine these by doing soemthing like this:
|
|||||
|
|
And Python solution from me:
I think that it is easiest to understand and modify than awk version (if you don't know awk)... Btw awk is next language on my list of languages that I want to learn... ;) |
||||
|
|