在其他人中找到了这个帖子,我同意它包含最完整的答案,所以我也添加了我的:
1)sed和ed非常有用......手工制作!看看@Johnny的代码:
sed -i -e 's/abc/XYZ/g' /tmp/file.txt
2)当我的限制是通过shell脚本使用它时,没有变量可以用来代替abc或XYZ! This似乎同意我至少理解的内容 . 所以,我不能用:
x='abc'
y='XYZ'
sed -i -e 's/$x/$y/g' /tmp/file.txt
#or,
sed -i -e "s/$x/$y/g" /tmp/file.txt
但是,我们能做什么?至于@Johnny说使用'while read ...'但不幸的是,这不是故事的结尾 . 以下与我合作很好:
#edit user's virtual domain
result=
#if nullglob is set then, unset it temporarily
is_nullglob=$( shopt -s | egrep -i '*nullglob' )
if [[ is_nullglob ]]; then
shopt -u nullglob
fi
while IFS= read -r line; do
line="${line//''/$server}"
line="${line//''/$alias}"
line="${line//''/$user}"
line="${line//''/$group}"
result="$result""$line"'\n'
done < $tmp
echo -e $result > $tmp
#if nullglob was set then, re-enable it
if [[ is_nullglob ]]; then
shopt -s nullglob
fi
#move user's virtual domain to Apache 2 domain directory
......
3)因为可以看到是否设置了nullglob,当有一个包含*的字符串时它会表现得很奇怪
ServerName www.example.com
变成了
没有结束角括号,Apache2甚至无法加载!
4)这种解析应该比单击搜索和替换慢,但是,正如您已经看到的,4个不同的搜索模式有4个变量在一个解析周期中运行!
在给定的问题假设下,我能想到最合适的解决方案 .