Wednesday, October 17, 2012

How to use %03d on existing data

Okay I have something that should be achievable but I can't figure it
out. I have a lot of files with data for books. Each file is a book
and is in chapters with lines of text.

sample of existing data:

<h2>1</h2><p>
1 line of text.<br>
2 another line of text.<br>
3 lines of text.<br>
...
10 More lines of text.<br>
...
100 Even more lines of text.<br>
</p><h2>2</h2><p>
1 line of text.<br>
2 another line of text.<br>
3 lines of text.<br>
...
10 More lines of text.<br>
...
100 Even more lines of text.<br>
</p>

What I want is:

<h2 id="b001c001">1</h2><p>
<span id="b001c001v001">1</span> line of text.<br />
<span id="b001c001v002">2</span> another line of text.<br />
<span id="b001c001v003">3</span> lines of text.<br />
...
<span id="b001c001v010">10</span> More lines of text.<br />
...
<span id="b001c001v100">100</span> Even more lines of text.
</p><h2 id="b001c002">2</h2><p>
<span id="b001c002v001">1</span> line of text.<br />
<span id="b001c002v002">2</span> another line of text.<br />
<span id="b001c002v003">3</span> lines of text.<br />
...
<span id="b001c002v010">10</span> More lines of text.<br />
...
<span id="b001c002v100">100</span> Even more lines of text.
</p>

What I am doing presently.

I have this mapped:

:%s@<h2\zs\ze>\([0-9]\+\)@ id="b001c0\1"@<CR>:%s@<h2
id="b001c0\zs\ze[1-9]\{1}"@0@<CR>gg<CR>

The above finds <h2>1 and changes it to <h2 id="b001c01">1 then
changes it again to <h2 id="b001c001">1

I have this mapped:

:'y,'zs@\zs\([0-9]\+\)\ze.*$@<span id="b001c001v0\1"
class="verse">\1</span>@<CR>:'y,'zs@<br\zs\ze>@
/@<CR>:'y,'zs@v0\zs\ze[1-9]\{1}"@0@<CR>'z0

The above has to be reset at every chapter. It finds "1 line of
text.<br>" and converts it to:
<span id="b001c001v01">1</span> line of text.<br /> and then to
<span id="b001c001v001">1</span> line of text.<br />

Then I use this to remove the <br /> immediately before the </p>:

:%s@^[ ]\?<span.*\zs<br />\ze$\n[ ]\?</p@@

I found these:

http://vim.wikia.com/wiki/index.php?search=add+leading+0+to+a+number&fulltext=Search

http://vim.wikia.com/wiki/Insert_line_numbers

http://vim.wikia.com/wiki/Making_a_list_of_numbers

I can't figure out how to use %03d to change my numbers from 1 to 001
I also would like to figure out how to use setreg() to increase this
c001 part of the id label so it changes to c002 when it is supposed to
so I don't have to reset my map at every chapter. I need to figure out
the first part before I figure out the second part. The examples in
the VIM tips aren't helping me figure this out. Maybe I am looking in
the wrong place.

I tried all sorts of things but nothing worked. I don't remember all
the things I tried but here are a few attempts I made that didn't
work:

:%s@\([0-9]\+\)@%03d\1@
:%s@\([0-9]\+\)@\=printf("%03d"\1)@
:%s@\([0-9]\+\)@\=printf("%03d\1")@

I don't understand how to use that number format in my search and
replace. The examples on the pages linked above don't give me any
clues. It has been a very long time since I have asked for help with
VIM. I usually have been able to find answers in the tips. But not
this time.

Thanks for any help that I get to understand how to do this.

Jack

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: