java文本查找与替换,从Bash命令查找和替换文本文件

在其他人中找到了这个帖子,我同意它包含最完整的答案,所以我也添加了我的:

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个变量在一个解析周期中运行!

在给定的问题假设下,我能想到最合适的解决方案 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值