天天看點

sed 常見用法

sed 常見用法

(1)添加注釋

sed 常見用法

sed -i 's/^\(77\)/# \1/' /tmp/abc/test.txt  

注釋掉指定行:

sed 常見用法

sed -e '2,3{s/^/#/}' test.txt  

 說明:注釋掉第2行和第三行

删除c語言的注釋(//)

sed 常見用法

sed -e 's/\/\/\(.*\)/\1/g' fenzhifa.c  

(2)删除注釋

sed 常見用法

sed -i 's/^#[[:space:]]*//'  /tmp/abc/test.txt  

(3)擷取腳本所在目錄

shell腳本檔案名稱:loc.sh

内容:

sed 常見用法

#!/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 常見用法

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"

sed 常見用法

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

上一篇: 安裝lessc

繼續閱讀