sed
的替換可用:斜杠/
,豎或|
,井號#
等符號, 但是… … 查找只能用斜杠/
替換必須用s開頭, 如:s/
, s|
, s#
例如:
s/正則/替換內容/
s/正則/替換內容/g
s|正則|替換內容|
s|正則|替換內容|g
s#正則#替換內容#
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