vi應用:使用v和s命令兩步提取出想要的內容

本文以提出spring中配置文件applicationContext.xml中所有bean的id爲例,講解如何使用vi的v和s命令兩步提取出想要的內容。

applicationContext.xml文件內容如下:

<?xml version = "1.0" encoding = "UTF-8" ?>

<beans xmlns ="http://www.springframework.org/schema/beans"

xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:context ="http://www.springframework.org/schema/context"

xsi:schemaLocation ="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd" >

<bean id="bookService" class = "BookServiceImpl">

<property name ="appProfile" ref= "myProfile">

</property>

</bean>

<bean id="libService" class = "LibServiceImpl">

<property name ="triggers" >

<list>

< ref local ="bookService" />

</list>

</property>

<property name="threadCountProperties" >

<props>

< prop key ="threadCount" > 1</ prop >

</props>

</property>

</bean>

</beans>

目標是將bean id(bookService和libService)提取出來。步驟如下:

1)使用v命令把不包含<bean\ 的行給刪掉

:v/<bean\ /d

applicationContext.xml內容變成如下:

       <bean id="bookService" class = "BookServiceImpl">
       <bean id="libService" class = "LibServiceImpl">

2)通過s命令使用正則表達式把每一行替換成我們想提出的內容

:%s/.\{-\}id="\(.\{-\}\)".*$/\1

applicationContext.xml內容變成如下:

bookService
libService

至此,兩個bean id(bookService和libService)被成功提取出來。

該方法會修改原來文本內容,爲了不影響原來的文本,請copy一份專門用來提取信息。另外這個只是個人用的比較順手的方法,如果大家有更好的方法可以一起討論一下。

發佈了28 篇原創文章 · 獲贊 16 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章