欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sed 常见用法 博客分类: linux命令 sedsed常见用法 

程序员文章站 2024-03-14 22:39:23
...

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 

name=whuang

export name

age=27

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

name=whuang

else

name=whuang:"$name"

fi

export name

if [ x"$age" = x ];then

age=27

else

age=27:"$age"

fi

export age


sed 常见用法
            
    
    博客分类: linux命令 sedsed常见用法 
 

 

 

(5)解决中标麒麟注册服务失败的问题

#!/bin/sh

if [ `id -u` -ne 0 ];then

         echo "Please rerun this script as root ."

         exit 2

fi

if [ -z "$1"  ];then

    echo "please specify patch path:"

         exit 2

fi

 

#-------------------------------- 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"

fi

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"

for ii in `ls *7d`;do

         delete_Required_Start "$ii"

done

 

sed -i 's/log_warning_message\([ ]*(\)/log_warning_msg\1/' /lib/lsb/init-functions

 

cd "$this_dir"

 

 

  • sed 常见用法
            
    
    博客分类: linux命令 sedsed常见用法 
  • 大小: 26.5 KB
相关标签: sed sed常见用法