加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 使用文件的内容使用SED替换字符串

发布时间:2020-12-15 18:55:06 所属栏目:安全 来源:网络整理
导读:什么是用于mac shell脚本的sed命令,它将用myFile.txt的整个字符串内容替换字符串“fox”的所有迭代. myFile.txt将是具有换行符和各种字符的html内容.一个例子是 /div /div br div id="container2" div class="question" onclick="javascript:show('answer2')
什么是用于mac shell脚本的sed命令,它将用myFile.txt的整个字符串内容替换字符串“fox”的所有迭代.

myFile.txt将是具有换行符和各种字符的html内容.一个例子是

</div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>

谢谢!

编辑1

这是我的实际代码:

sed -i.bkp  '/Q/{
s/Q//g
r /Users/ericbrotto/Desktop/question.txt
}' $file

当我运行它我得到:

sed in place editing only works for regular files.

在我的文件中,Q被一大堆汉字(!)替换.离奇!

你可以使用r命令.当您在输入中找到“狐狸”时
/fox/{

…替换它没有什么…

s/fox//g

…并阅读输入文件:

r f.html
}

如果你有一个文件,如:

$cat file.txt
the
quick
brown
fox
jumps
over
the lazy dog
fox dog

结果是:

$sed '/fox/{
    s/fox//g
    r f.html
}' file.txt
the
quick
brown

    </div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>
jumps
over
the lazy dog
 dog
    </div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>

编辑:要更改正在处理的文件,只需将-i标志传递给sed:

sed -i '/fox/{
    s/fox//g
    r f.html
}' file.txt

一些sed版本(如我自己的)需要将扩展??名传递给-i标志,这将是使用该文件的旧内容的备份文件的扩展名:

sed -i.bkp '/fox/{
    s/fox//g
    r f.html
}' file.txt

这里和一个班轮一样,这也兼容Makefile

sed -i -e '/fox/{r f.html' -e 'd}'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读