sed 常见用法 博客分类: linux命令 sedsed常见用法
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
(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常见用法
-
[转] Linux中find常见用法示例 博客分类: linux linuxfind
-
linux find命令鲜为人知的用法(多目录查找) 博客分类: linux_app
-
linux sed 常用用法举例01 博客分类: unixlinux命令 linuxsedsed命令就地写入
-
linux sed 常用用法举例01 博客分类: unixlinux命令 linuxsedsed命令就地写入
-
PowerMock常见用法 博客分类: Junit4 PowerMockPowerMock常用方法
-
Linux下命令行cURL的10种常见用法示例
-
Linux常见基本命令与用法大全
-
Linux系统中ls命令的常见用法整理
-
Linux shell命令用法及常见用例之tar命令