问题:因涉及多个脚本,且各个脚本关键字较多,因此想用sed 做替换
经测试如下:
echo “aaTIME _Time time ,timE time_ 'time' ”| sed -r -e 's/([^a-zA-Z_"'\''"])(time)([^a-zA-Z_"'\''"])/\1`\2`\3/ig'
aaTIME _Time `time` ,`timE` time_ 'time'
cat test_sed.txt
aaTIME _Time time ,timE time_ 'time'
sed -i.amenity_bak -r -e 's/([^a-zA-Z_"'\''"])(time)([^a-zA-Z_"'\''"])/\1`\2`\3/ig' test_sed.txt
ll test_sed.txt
test_sed.txt
test_sed.txt.amenity_bak
cat test_sed.txt
aaTIME _Time time ,timE time_ 'time'
解释说明:
sed
-r 正则匹配需要增加的标识
-e 多个替换选项,后续可以继续增加-e ’s/xx/ss/ig‘ 额外的规则
-i 直接修改文件 -i.bak 的方式可以增加备份文件
[^a-zA-Z]
该方式为匹配非大小写字母
“’’’” sed中单引号显示问题: 双引号" 单引号’ 转义符号\ 单引号’ 单引号’ 双引号"
\1 \2 \3 代表前面小括号匹配的内容 分别代表 第一个括号的内容 第二个括号的内容 第三个括号的内容