今天用sed命令向文件中插入一行内容后,结果发现每行内容后多了^M,很是困惑,百度后发现,原来是xml文件是windows上的文件,移到linux上,所以格式是dos的,然后编码格式与unix不太相同。
可以通过命令查看差别。
dos编码:
</beans>[root@Nictalk-8 classes]# cat -A hessian-server.xml
<?xml version="1.0" encoding="UTF-8"?>^M$
<beans xmlns="http://www.springframework.org/schema/beans"^M$
^Ixmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"^M$
^Ixmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"^M$
^Ixsi:schemaLocation="http://www.springframework.org/schema/beans^M$
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd^M$
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd^M$
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd^M$
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"^M$
^Idefault-lazy-init="false">^M$
^M$
^I<!-- Hessian Server -->^M$
^I<bean name="/CootalkAddrbookListService" class="org.springframework.remoting.caucho.HessianServiceExporter">^M$
^I^I<property name="serviceInterface" value="com.xinwei.camtalkgateway.contact.service.CootalkAddrbookListService" />^M$
^I^I<property name="service" ref="cootalkAddrbookListService" />^M$
^I</bean>^M$
^I^M$
^I<bean name="/GroupManagermentFacade" class="org.springframework.remoting.caucho.HessianServiceExporter">^M$
^I^I<property name="serviceInterface" value="com.xinwei.camtalkgateway.group.facade.GroupManagermentFacade" />^M$
^I^I<property name="service" ref="groupManagermentFacade" />^M$
^I</bean>^M$
^I^M$
^I<bean name="/RemoteFriendService" class="org.springframework.remoting.caucho.HessianServiceExporter">^M$
^I^I<property name="serviceInterface" value="com.xinwei.camtalkgateway.user.service.RemoteFriendService" />^M$
^I^I<property name="service" ref="remoteFriendService" />^M$
^I</bean>^M$
^I^M$
</beans>
unxi编码:
<?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"$
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"$
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$
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd$
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"$
default-lazy-init="false">$
$
<!-- Hessian Client -->$
<bean id="authService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">$
<property name="serviceInterface" value="com.xinwei.crm.authcenter.service.AuthService"/>$
<property name="serviceUrl" value="${authcenter.url}AuthService"/>$
<property name="readTimeout" value="20000" />$
</bean>$
$
<bean id="aliDayeSendMsgService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">$
<property name="serviceInterface" value="com.xinwei.cootel.alidaye.service.AliDayeSendMsgService" />$
<property name="serviceUrl" value="${alidayu.url}AliDayeSendMsgService" />$
<property name="readTimeout" value="20000" />$
</bean>$
$
<bean id="groupManagerService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">$
<property name="serviceInterface" value="com.xinwei.camtalk.notify.service.GroupManagerService" />$
<property name="serviceUrl" value="${notify.url}GroupManagerService" />$
<property name="readTimeout" value="20000" />$
</bean>$
$
<bean id="redisDBService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">$
<property name="serviceInterface" value="com.xinwei.redisdb.service.RedisDBService" />$
<property name="serviceUrl" value="${gps.redis.service}RedisDBService" />$
<property name="readTimeout" value="20000" />$
</bean>$
$
<bean id="redisFaceToFaceService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">$
<property name="serviceInterface" value="com.xinwei.redisdb.service.RedisFaceToFaceService" />$
<property name="serviceUrl" value="${gps.redis.service}RedisFaceToFaceService" />$
<property name="readTimeout" value="20000" />$
</bean>$
$
</beans>$
可以看出,两种不同编码的格式,是不同的
可以使用 dos2unix将dos格式的文件转换成unix格式。
centos可以直接yum 安装
,然后可以执行想要的操作。
find . -name hessian-client.xml | xargs dos2unix
find . -name hessian-client.xml | xargs sed -i '/<\/bean>/i\<property name="readTimeout" value="20000" />'
找到当前路径下的所有hessian-client.xml,将其格式转换成unix,以免替换后出现^M的东西,
然后,执行在的上一行,添加一条内容。
原文件:
[root@xiaowu shell]# cat -n file
1 aaaa
2 bbbb
3 cccc
4 dddd
现在要在第二行即“bbbb”行的下面添加一行,内容为“xiaowu”
[root@xiaowu shell]# sed ‘/bbbb/a\xiaowu’ file
aaaa
bbbb
xiaowu
cccc
dddd
如果要加两行“xiaowu”可以用一下语句,注意用“\n”换行
[root@xiaowu shell]# sed ‘/bbbb/a\xiaowu\nxiaowu’ file
aaaa
bbbb
xiaowu
xiaowu
cccc
dddd
如果要在第二行即“bbbb”行的上添加一行,内容为“xiaowu”,可以把参数“a”换成“i”
[root@xiaowu shell]# sed ‘/b/i\xiaowu’ file
aaaa
xiaowu
bbbb
cccc
dddd
以上文件中只有一行匹配,如果文件中有两行或者多行匹配,结果有是如何呢?
[root@xiaowu shell]# cat -n file
1 aaaa
2 bbbb
3 cccc
4 bbbb
5 dddd