sed 命令二

今天用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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值