主要介绍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/'