linux sed 常用用法举例01 博客分类: unixlinux命令 linuxsedsed命令就地写入
判断是否以. 开头
echo .abc |grep "^\." ;echo $?
判断是否以/ 开头
echo /abc |grep "^/" ;echo $?
判断是否以\ 开头
echo "\abc" |grep "^\\\\" ;echo $?
sed 多次修改
两种方式:可以使用-e ,也可以使用分号。
范例01:
sed 's/abc/000/g;s/000/uuu/g' abc.txt |
范例02:
[whuang@localhost test]$ echo The tiger cubs will meet on Tuesday after school | sed 's/tiger/wolf/; s/after/before/' The wolf cubs will meet on Tuesday before school |
sed 当$ 与#相遇
以下是错误的:
find $kingbase_own_home/.local/share/applications -type f -name "*.desktop" |xargs -i sed -i "s#^\(Icon=\)\([^ ]\+\)$#\1${install_dir}\2.png#g" {}
正确的应该是:
find $kingbase_own_home/.local/share/applications -type f -name "*.desktop" |xargs -i sed -i "s#^\(Icon=\)\([^ ]\+\)\$#\1${install_dir}\2.png#g" {}
点评:linux 会把$# 解析成为0.
范例01:
脚本名称:sed_#.sh
脚本内容:
#!/bin/sh echo Icon=KDB_ISQL|sed "s#^\(Icon=\)\(KDB_ISQL\)$#\1CCC\2.png#g" |
脚本运行结果:
[root@localhost sed_study]# sh sed_#.sh sed:-e 表达式 #1,字符 38:unterminated `s' command |
原因:sh把$#解析成为0 了。
以debug 方式运行:
[root@localhost sed_study]# sh sed_#.sh + echo Icon=KDB_ISQL + sed 's#^\(Icon=\)\(KDB_ISQL\)0\1CCC\2.png#g' sed:-e 表达式 #1,字符 38:unterminated `s' command |
解决方法:
对$进行转义:
#!/bin/sh echo Icon=KDB_ISQL|sed "s#^\(Icon=\)\(KDB_ISQL\)\$#\1CCC\2.png#g" |
去掉两边的引号
方式一:
myn=$(echo $myname |sed -e "s/\"\(.*\)\"/\1/g")
或
myn=$(echo $myname |sed -e "s/\"\([^ ]*\)\"/\1/g")
方式二:
myname2= ${myname#\"}
echo ${myname2%\"}
双重条件
多重条件 多个条件
范例01
sed '64,${/PATH/d}' av.txt
范例02:
[root@localhost ~]# echo -e "abc1\nccc1\nbbb1\nddd1"|sed '/bbb/ s/1/2/' abc1 ccc1 bbb2 ddd1 [root@localhost ~]# echo -e "abc1\nccc1\nbbb1\nddd1" abc1 ccc1 bbb1 ddd1 |
范例03:
abc.txt 的内容:
abcxxx abcddd abc222 abc444 abc555 abc666 abc777 |
需求:仅仅把2-3行的abc 替换成为000
[root@localhost test]# sed '2,3{s/abc/ooo/;}' abc.txt abcxxx oooddd ooo222 abc444 abc555 abc666 abc777 |
范例04:
删除指定范围内的行
abc.txt 的内容如下:
111 abc222 333 444 abc444 |
需求只删除1-3行中匹配abc的所有行
sed '1,3{/abc/d;}' abc.txt
范例05:
abc.txt 文件的内容:
test aa est bbc aa cccc aa |
sed '/test/{ n;n; s/aa/bb/; }' abc.txt
执行结果:
test aa est bbc aa cccc bb
|
就地写入文件:
sed_i()
{
scripts="$1"
targetFile=$2
sed -e "$scripts" "$targetFile" >"$targetFile.bak"
rm -f "$targetFile"
mv "$targetFile.bak" "$targetFile"
}
实例:
把inte-workbench.chanjet.com替换成workbench.chanjet.com
echo "修改工作台的地址" sed -i 's/inte-workbench\.chanjet\.com/workbench\.chanjet\.com/g' ./page/order/list.js
把https替换成为http:
sed -i 's/"https:\/\/" + APP.Constant.STORE_ROOT/"http:\/\/" + APP.Constant.STORE_ROOT/g' ./page/order/detail.js
sed -i 's/callback = URLEncoder.encode("https:\/\//callback = URLEncoder.encode("http:\/\//' ./com/chanjet/gov/controller/PayOrderController.java sed -i 's/= URLEncoder.encode("https:\/\//= URLEncoder.encode("http:\/\//' ./com/chanjet/gov/service/AuthInterceptor.java sed -i 's/String payUrl = "https:\/\/"/String payUrl = "http:\/\/"/' ./com/chanjet/gov/controller/PayOrderController.java sed -i 's/window.location.href = "https:\/\/"/window.location.href = "http:\/\/"/' ../webapp/release/js/page/order/list.js echo "测试环境把https 改为http"
将空格替换为Tab
# cat a.txt | tr '[:space:]' '\t'