mercredi 6 mai 2015

How can I use vim regex to replace text when math divide is involved in the expression

I am using vim to process text like the following

0x8000   INDEX1 ....
0x8080   INDEX2 ....
....
0x8800   INDEXn ....

I want to use regular expression to get the index number of each line. that is

0x8000 ~ 0
0x8080 ~ 1
....
0x8800 ~ n

The math evaluation should be (hex - 0x8000) / 0x80. I am trying to using vim regular expression substitution to get the result in line

%s/^\(\x\+\)/\=printf("%d", submatch(1) - 0x8000)

This will yield

0     INDEX0
128   INDEX1
....
2048  INDEXn

What I want to do is to further change it to

0     INDEX0
1     INDEX1
...
20    INDEXn

That is, I want to further divide the first column with an 0x80. Here is when I get the problem.

The original argument is "submatch(1) - 0x8000". I now add an "/ 0x80" to it, which forms

%s/^\(\x\+\)/\=printf("%d", (submatch(1) - 0x8000)\/0x80)

Now Vim report error

Invalid expression: printf("%d", (submatch(1) - 0x8000)\/0x80))

It looks like vim meet problem when processing "/". I also tried with a single "/" (without escape), but still fails.

Can anyone help me on this?

Aucun commentaire:

Enregistrer un commentaire