Shell 文件操作 第二季 ...

主要介绍shell对文件的 操作

1、替换文件内容

2、xml文件 的操作

 

1、替换文件内容

 参考:http://www.cnblogs.com/88999660/articles/1581524.html vi命令大全

          http://www.cnblogs.com/edwardlost/archive/2010/09/17/1829145.html  sed 命令详解

vi命令 ---- :%s/string1/string2/g 在整个文件中替换“string1”成“string2”。

 

 cat >s1
i love ${chinaaaa}
i love ${chinaaaa}
i love ${chinaaaa}
i love ${chinaaaa}
i love ${chinaaaa}
sed "s/\${chinaaaa}/china/g" "s1" | sed 's/|/\//g' > "s2"

vi s2
i love china
i love china
i love china
i love china
i love china

 

2、修改xml文件

 参考:http://bbs.weiphone.com/read-htm-tid-1137089.html 

 

cat >a.xml
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<id>124578</id>
<name>test_name</name>
</config>


cat >apend.sh
 
    filename=$1
    string=$2
    cpath=.
   
    num=$(cat -n $cpath/$filename | sed -n '/<config>/p' | sed -n '$p' | awk '{print $1}')
    sed -i ''$num' a\ '"$string"'' $cpath/$filename
    [ $? -ne 0 ] && echo "Fail ....." || echo "Success ..."
 
 
 
#增加一个节点内容

 cat a.xml | grep "<attr>adsadas</attr>"

if [ $? -eq 0 ]
    then

#存在

echo "attr Exsit ...."
else

#不存在

append "a.xml"  '<attr>adsadas</attr>'

fi


#获取xml的 节点内容
cat a.xml |grep "<id>" | sed 's/.*<.*>\([^<].*\)<.*>.*/\1/'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值