sed 常见用法
(1)添加注释
sed -i 's/^\(77\)/# \1/' /tmp/abc/test.txt
注释掉指定行:
sed -e '2,3{s/^/#/}' test.txt
说明:注释掉第2行和第三行
删除c语言的注释(//)
sed -e 's/\/\/\(.*\)/\1/g' fenzhifa.c
(2)删除注释
sed -i 's/^#[[:space:]]*//' /tmp/abc/test.txt
(3)获取脚本所在目录
shell脚本文件名称:loc.sh
内容:
#!/bin/sh
#---------------------------- locate this_dir ----------------start
## this file path
this_dir=`pwd`
dirname $0|grep "^/" >/dev/null
if [ $? -eq 0 ];then
this_dir=`dirname $0`
else
dirname $0 | grep "^\.$" >/dev/null
if [ $? -ne 0 ];then
this_dir=`dirname $0|sed "s#^#${this_dir}/#"`
fi
fi
echo $this_dir
#---------------------------- locate this_dir ----------------end
执行:
ctier@allinone-yunyingyong-2-v-o:/tmp/abc/ccc$ ./loc.sh
/tmp/abc/ccc
(4)在键值对后面增加export
sed -e "s#\(.*\)=.*#&\nexport \1#" prop.txt
prop.txt的内容如下:
name=whuang
age=27
运行结果:
ctier@allinone-yunyingyong-chanjet02-v-o:/tmp/abc$ sed -e "s#\(.*\)=.*#&\nexport \1#" prop.txt
export name
export age
对于path变量
ctier@allinone-yunyingyong-chanjet02-v-o:/tmp/abc$ sed "s#\(.*\)=\(.*\)#if [ x\"$\1\" = x ];then\n\t&\nelse\n\t&:\"$\1\"\nfi\nexport \1#" prop.txt
if [ x"$name" = x ];then
else
name=whuang:"$name"
fi
if [ x"$age" = x ];then
age=27:"$age"
(5)解决中标麒麟注册服务失败的问题
#!/bin/sh
if [ `id -u` -ne 0 ];then
echo "please rerun this script as root ."
exit 2
if [ -z "$1" ];then
echo "please specify patch path:"
#-------------------------------- function start ------------------------------------------
delete_required_start()
{
filepath="$1"
if [ -f "$filepath" ];then
sed -i '2,10{/required-start/d;}' "$filepath"
sed -i '2,10{/required-stop/d;}' "$filepath"
fi
}
#-------------------------------- function end ------------------------------------------
this_dir=`pwd`
patch_path="$1"
ls "$patch_path/patch.sh" >/dev/null 2>&1
if [ $? -eq 0 ];then
patch_path="$patch_path/patch"
server_bin="$patch_path/patch/build/server/bin"
tomcat_bin="$patch_path/patch/build/stools/tomcat/bin"
cd "$server_bin"
for ii in `ls *7d`;do
delete_required_start "$ii"
done
cd "$tomcat_bin"
delete_required_start "$ii"
sed -i 's/log_warning_message\([ ]*(\)/log_warning_msg\1/' /lib/lsb/init-functions
cd "$this_dir"