开发者

How to delete the text in a file except the words I needed using VIM

开发者 https://www.devze.com 2023-01-10 07:35 出处:网络
I have an XML file like this: <text> <A>12</A> <B>13</B> </text> <text>

I have an XML file like this:

<text>
<A>12</A>
<B>13</B>
</text>

<text>
<A>14</开发者_开发技巧A>
<B>15</B>
</text>

Now I want delete all the text in the file except the words in tag A. That is, the file should contain:

12
14

How can I achieve this?


You can do it in two commands (on one line if you like)

:g!/.*<A>[^<]*<\/A>.*/d
:%s/<A>\([^<]*\)<\/A>/\1/g

one line: (separate commands with a vertical bar |)

:g!/.*<A>[^<]*<\/A>.*/d | :%s/<A>\([^<]*\)<\/A>/\1/g

This will remove the blank lines...


:%s/^.\{-}\(<A>\(.*\)<\/A>\)\?.*$/\2/g

That assumes you have the same magic mode as me of course ;) It doesn't remove the blank lines.


:%s/\_.\{-}<A>\([^<]*\)<\/A>\_.\{-}>$/\1\r/

Gets everything but the final /text tag in one fell swoop :-) Fun stuff!!

0

精彩评论

暂无评论...
验证码 换一张
取 消