利用 sed 完成 shell 中的字符串替换
shell 中经常会要用到字符串替换操作,这里的是用 sed 命令实现
# 仅替换一次
echo "hello, how are you?" | sed 's/?/!/'; # hello, how are you!
# 替换多次
echo "hello? how are you?" | sed 's/?/!/g'; # hello! how are you!
# 仅替换结尾的字符串,类似于 rtrim
echo "hello? how are you?" | sed 's/?$/!/'; # hello? how are you!
# 仅替换开头的字符串,类似于 ltrim
echo "?hello how are you?" | sed 's/^?/!/'; # !hello how are you?