Dilip Malipeddi
Dilip Malipeddi

Reputation: 61

How to delete values between xml tags in Vi editor

I want to replace the value between below xml tags in VI for all the columns ending with AM

<property name="EQ_BNKCRD_30PL_DPD_BAL_AM" desc="EQ_BNKCRD_30PL_DPD_BAL_AM">+000026928134473.000</property>

simply +000026928134473.000 should be removed in the above tags

I have tried :%s/_AM">*.*</_AM"></g

but the output is this

<property name="EQ_RVLV_TRD_OPN_HI_LC_AM"><property>

from the above the desc part is removed and in property tag / is removed

Upvotes: 6

Views: 1705

Answers (5)

Arithran
Arithran

Reputation: 1289

dit 

in normal mode, which means delete inside tag. If you do 'dat' delete around tag it would delete the tag too.

Upvotes: 7

BenjaminRH
BenjaminRH

Reputation: 12172

Why not record a macro that searches for the next occurrence of AM">, move the cursor a few characters to the right into the tag, and then dit for "Delete In Tag" -- then just play it back a ton of times

I often find it faster in cases like this to record a simple macro rather than taking the time to find a regex that works.

Edit: full sequence, separated logically

/AM">
qa
llll
dit
n
q
500@a

Upvotes: 0

FDinoff
FDinoff

Reputation: 31439

It might be easier to use a macro in this case. To delete the contents in between tags you can use dit. The following sets the search register to _AM> and just looks for the next match types dit every time it finds a match.

let @/='_AM">' | let @a='ndit@a' | set nowrapscan

Then go to the top of the file gg and run the macro that was stored in register a with @a. set nowrapscan is necessary to make sure the macro never goes on forever.

Upvotes: 0

Kent
Kent

Reputation: 195169

how about :

%s/_AM">\zs[^<]*//

Upvotes: 0

MichaelJones
MichaelJones

Reputation: 1406

You want to match everything from _AM"> up to the next <. So this should work:

:%s/_AM">[^<]*</_AM"></g

The important change is from >*.* (which doesn't actually make much sense as it matches any number of > symbols and then anything after) to >[^<]* which means match one > and then anything that isn't a <.

Upvotes: 0

Related Questions