sed的替换可用:斜杠/,竖或|,井号# 等符号, 但是… … 查找只能用斜杠/
替换必须用s开头, 如:s/ , s| , s#
例如:
s/正则/替换内容/s/正则/替换内容/gs|正则|替换内容|s|正则|替换内容|gs#正则#替换内容#s#正则#替换内容#g
当内容包含斜杠/时, (例如路径) , 使用 竖或|,井号# 比较方便, 可以不用转义路径分隔符斜杠/
与替换相比, 查找只能用斜杠 / , sed '/hello/ 不能写成 sed ‘|hello|’ 或 sed ‘#hello#’
ip addr|sed '/inet /' 效果类似 ip addr|grep 'inet '
sed的查找和替换可以一起用
sed的查找和替换可以一起用, 先用查找过滤一部分内容, 再在剩余的内容中执行替换.
查找只能用/ , 例如:
将所有包含"hello"的行中的"world"替换成"世界" , 可写成:
/hello/s/world/世界//hello/s/world/世界/g/hello/s|world|世界|/hello/s|world|世界|g/hello/s#world#世界#/hello/s#world#世界#g
实测:
tempStringVar="$(echo -e "
hello world world world
world world world world
hello world world world
world world world world
hello world world world
world world world world
")"
echo "${tempStringVar}" | sed '/hello/s/world/世界/'
echo "${tempStringVar}" | sed '/hello/s/world/世界/g'
echo "${tempStringVar}" | sed '/hello/s|world|世界|'
echo "${tempStringVar}" | sed '/hello/s|world|世界|g'
echo "${tempStringVar}" | sed '/hello/s#world#世界#'
echo "${tempStringVar}" | sed '/hello/s#world#世界#g'
结果:
[root@1235vm-c69w yum.repos.d]# tempStringVar="$(echo -e "
> hello world world world
> world world world world
> hello world world world
> world world world world
> hello world world world
> world world world world
> ")"
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s/world/世界/'hello 世界 world world
world world world world
hello 世界 world world
world world world world
hello 世界 world world
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s/world/世界/g'hello 世界 世界 世界
world world world world
hello 世界 世界 世界
world world world world
hello 世界 世界 世界
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s|world|世界|'hello 世界 world world
world world world world
hello 世界 world world
world world world world
hello 世界 world world
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s|world|世界|g'hello 世界 世界 世界
world world world world
hello 世界 世界 世界
world world world world
hello 世界 世界 世界
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s#world#世界#'hello 世界 world world
world world world world
hello 世界 world world
world world world world
hello 世界 world world
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s#world#世界#g'hello 世界 世界 世界
world world world world
hello 世界 世界 世界
world world world world
hello 世界 世界 世界
world world world world
实测2
tempStringVar="
hello world world world
world world world world "
echo "${tempStringVar}" | sed '/hello/s/world/世界/'
echo "${tempStringVar}" | sed '/hello/s/world/世界/g'
echo "${tempStringVar}" | sed '/hello/s|world|世界|'
echo "${tempStringVar}" | sed '/hello/s|world|世界|g'
echo "${tempStringVar}" | sed '/hello/s#world#世界#'
echo "${tempStringVar}" | sed '/hello/s#world#世界#g'
结果
[root@1235vm-c69w yum.repos.d]# tempStringVar="
> hello world world world
> world world world world "
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s/world/世界/'hello 世界 world world
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s/world/世界/g'hello 世界 世界 世界
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s|world|世界|'hello 世界 world world
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s|world|世界|g'hello 世界 世界 世界
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s#world#世界#'hello 世界 world world
world world world world
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed '/hello/s#world#世界#g'hello 世界 世界 世界
world world world world